Java-Interviw-Questions
Java-Interviw-Questions
Java has several features that make it a popular programming language. Some of these
features include platform independence, object-oriented programming, automatic memory
management, robustness, and security.
Control structures are statements that are used to control the flow of a program. They
include if-else statements, switch statements, loops (for, while, do-while), and break and
continue statements.
What is JVM?
JVM stands for Java Virtual Machine. It is an abstract machine that provides the runtime
environment in which Java programs are executed. The JVM interprets Java bytecode and
translates it into machine-specific code.
JDK stands for Java Development Kit. It is a software development kit that includes tools for
developing, compiling, and debugging Java programs. JRE stands for Java Runtime
Environment. It is a software environment that provides the necessary runtime libraries
and components for running Java programs. JVM is the virtual machine that executes the
Java bytecode.
2. 2. Why OOPs?
Advantages of OOPs:
- Better Code Reusability: Through inheritance and polymorphism.
- Easier Maintenance: Encapsulation allows easy updates.
- Improved Data Security: Data hiding restricts unauthorized access.
- Better for Large Software: Logical organization with classes and objects.
3. 3. What is a Class?
A class is a blueprint or template for creating objects. It defines data members (variables)
and methods (functions).
4. 4. What is an Object?
- Encapsulation
- Abstraction
- Polymorphism
- Inheritance
6. 6. What is Encapsulation?
Encapsulation is the bundling of data and methods into a single unit (class) and restricting
access using access modifiers like private, protected, and public.
7. 7. What is Abstraction?
Abstraction is hiding unnecessary details and showing only the relevant features to the
user. Achieved using abstract classes and interfaces.
8. 8. What is Polymorphism?
Polymorphism means 'many forms'. It allows methods to perform different tasks based on
the context.
- Compile-time Polymorphism: Method Overloading.
- Runtime Polymorphism: Method Overriding.
9. 9. What is Inheritance?
Inheritance allows a class (child) to use the properties and methods of another class
(parent). It promotes code reusability.
-default: by default
- Public: Accessible everywhere.
- Private: Accessible only within the class.
- Protected: Accessible within the class and subclasses.
Disadvantages:
- Requires skilled programmers.
- Not suitable for all problems.
- Larger program size compared to procedural programming.
- Java
- C++
- Python
- C#
- Ruby
- Overloading: Multiple methods with the same name but different parameters.
- Overriding: Redefining a method in a child class.
A class doesn’t occupy memory. Only objects created from the class use memory.
No. Static methods can be called directly using the class name without creating objects.
A constructor initializes objects. It has the same name as the class and no return type.
An abstract class cannot be instantiated and may contain both abstract and non-abstract
methods.
Example:
```java
abstract class Animal {
abstract void sound();
void eat() { System.out.println("Eating..."); }
}
```