Java basic interview questions answers.
1. Why is Java not a pure object-oriented language?
Answer:-
Java is not a pure object-oriented language due to primitive data types like boolean, byte, char, int, float, double, and short.
To make Java object-oriented we have to use Wrapper classes to make the primitive data type into an object of that class. Some Wrapper classes are Byte, Boolean, Char, Integer, Short etc.
2. What is Object Oriented Programming (OOPs)?
Answer:-
Object Oriented Programming (OOPs) is a programming model that allows to organise the complete software design around the objects talking to each other rather than functions and logic.
3. What is a class?
Answer:-
A class denotes a category of objects and acts as a blueprint for creating such objects having common properties and methods. It is a user-defined data type that contains the data members and fields that operate on the data.
4. What is an Object?
Answer:-
An object is an instance of a class. The Object is constructed using the class as a blueprint and is a concrete instance of the abstraction that the class represents. The process of creating objects is called instantiation. In simple terms, they are the actual world entities that have a state and behaviour.
5. Explain about static keywords in java.
Answer:-
the
static
keyword is used to declare members (variables and methods) that belong to the class itself, rather than to instances of the class.Static Variables (Class Variables):
- When a variable is declared as
static
within a class, it's called a static variable or class variable. - Static variables are shared among all instances of the class. There is only one copy of a static variable regardless of how many instances of the class are created.
- Static variables are initialized only once, at the start of the program's execution, and they maintain their value throughout the program's lifetime.
- They can be accessed directly using the class name (e.g.,
ClassName.staticVariable
), without the need to create an instance of the class.
public class StaticTest {
public static int staticVariable = 20;
}
- Static Methods:
- When a method is declared as
static
within a class, it's called a static method. - Static methods belong to the class rather than to instances of the class. They can be called without creating an instance of the class.
- They cannot directly access instance variables or instance methods, as they are not associated with any particular instance.
- Static methods can access other static variables and methods directly.
- Common examples include utility methods, factory methods, and methods for performing common operations that don't require access to instance-specific data.
- public class StaticTest { public static void staticMethod() { System.out.println("This is a static method"); } }
- Static Blocks:
- Static blocks are used to initialize static variables or perform any other initialization tasks that should be done only once when the class is loaded.
- They are executed when the class is first loaded into memory before any static variables are initialized or static methods are called.
- Static blocks are particularly useful for initializing static variables with complex initialization logic or for performing resource allocation or setup tasks.
- public class StaticTest { static { System.out.println("Static block executed"); } }
- It's important to note that static members belong to the class itself, rather than to instances of the class. Therefore, they are shared among all instances and can be accessed without creating an instance of the class. However, excessive use of static members can lead to code that is harder to maintain and test, as it can introduce a global state and tight coupling between classes.
6. What advantage does overloading and overriding add?
Answer:-
Method Overloading:
- Improves Code Readability: Overloading allows you to define multiple methods with the same name but different parameter lists. This makes your code more readable because related functionality is grouped together.
- Code Reusability: By overloading methods, you can reuse the same method name for different scenarios. For example, you can have an
add
method that works with integers, doubles, or even strings. - Consistency and Readability: Overloading helps maintain consistency in method names. You can have a single method name for similar operations, which makes it easier to understand and remember.
- Memory Space Optimization: Overloaded methods share the same name, so you don’t need to create distinct method names for similar functionality. This saves memory space.
- Speeds Up Execution: Since overloaded methods are resolved at compile time (static polymorphism), there’s no runtime overhead. This leads to faster execution.
- Easy Code Maintenance: When you need to make changes, you only need to update one method instead of multiple methods with different names.
Method Overriding:
- Customization and Extensibility: Overriding allows a subclass to provide its own implementation of a method inherited from a superclass. This customization enables you to adapt behaviour to specific requirements.
- Run-Time Polymorphism: Overriding is a form of run-time polymorphism. When you call an overridden method on an object, the actual implementation is determined dynamically based on the object’s type.
- Same Method Signature: The overridden method must have the same method signature (name, parameters, and return type) as the method in the superclass.
- Co-Variant Return Types: In method overriding, the return type can be the same or a subtype (co-variant) of the return type in the superclass.
In summary, overloading provides flexibility and convenience, while overriding enables customization and extensibility. Both concepts play essential roles in achieving polymorphism in Java programs.
7. What advantage does Dynamic Polymorphism add?
Answer:-