Complete_Java_Interview_Questions
Complete_Java_Interview_Questions
A: The DAO pattern is a structural pattern used to separate persistence logic from
business logic, providing an interface to access data.
Grade: Basic
4. Q: What are the key benefits of using the DAO Pattern?
A: Annotations provide metadata for code without affecting its execution. Built-in
annotations include @Override, @Deprecated, and @SuppressWarnings. Custom
annotations can be created using @interface.
Grade: Intermediate
C2 General
7. Q: What is the difference between Checked and Unchecked exceptions?
A: ArrayList is backed by a dynamic array, making it fast for random access but
slower for insertions/deletions. LinkedList is backed by a doubly-linked list, making
it fast for insertions/deletions but slower for random access.
Grade: Intermediate
9. Q: What is the purpose of the final, finally, and finalize keywords?
A: The super keyword refers to the immediate parent class of the current object. It
can be used to call parent class methods and constructors.
Grade: basic
11.Q: What is the difference between abstract classes and interfaces in Java?
A: Abstract classes can have both abstract and concrete methods, while interfaces
can only have abstract methods (before Java 8). Java 8 allows default methods in
interfaces.
Grade: Basic
12.Q: What is the purpose of this keyword in Java?
A: The this keyword refers to the current instance of the class and is used to refer to
instance variables, methods, and constructors.
Grade: Basic
13.Can a static method be overridden in Java?
C2 General
A: No, a static method cannot be overridden in Java. Overriding applies to instance
methods (methods bound at runtime), Static methods are bound at compile time based
on the class type.
Grade: Advance
14.Q: Explain the concept of final in OOP.
A: public is accessible from anywhere, private is accessible only within the same
class, protected is accessible within the same package and by subclasses, default is
accessible only within the same package. We are allowed to use only “public” or
“default” access modifiers with java classes
Grade: Intermediate
16.Q: What is the significance of instanceof operator in Java?
A: An inner class is a class defined within another class. It can be a member class,
static nested class, or anonymous class.
Grade: Intermediate
What happen if a constructor is private?
A: prevent creation of objects outside the class , used in the Singleton design pattern
where the constructor is private and the class return the current class instance to be
used.
Can this and super be added inside static methods?
A: this cannot be used in static methods because static methods are not tied to an instance.
super can be used in static methods, but it refers to the superclass's static members and not
the instance.
C2 General
Q:Can you achieve Runtime Polymorphism by data members?
A: No, because method overriding is used to achieve runtime polymorphism and
data members cannot be overridden. We can override the member functions but not
the data members.
Grade: Advance
Output-Based Questions
A: 20. because obj1 and obj2 both refer to the same object in memory
19. Q: What is the output of the following code?
C2 General
A: The output is 'Child'. This demonstrates runtime polymorphism where the
overridden method in the subclass is invoked.
Grade: Basic
20. Q: What is the output of the following code
A: The output is '2 3 3'. The array is modified in the loop based on index computation.
C2 General
Explanation: The constructor of the parent class is always called first when creating a
subclass object.
Output: Hello
Explanation: Strings in Java are immutable. The + operation creates a new string, and s2 still
points to the original s1.
C2 General
Output: 2
Explanation: Since the equals method is not overridden, two keys with the same id are
treated as different objects.
The static block is executed first, when the class is loaded by the JVM. This happens even
before the main method starts executing, because the static block is part of the class
initialization process.
The static method staticMethod() is executed next, as it is explicitly called inside the main
method. Static methods belong to the class itself and can be invoked without creating an
object of the class.
C2 General
Regular Method:The regular method regularMethod() is executed last. To call this instance
method, an object of the class (obj) must first be created. Once the object is created, the
instance method is called using the object reference.
C2 General