What is the relationship between an abstract class and an interface in Java 8+?
Answer: B
From Java 8, interfaces can have default and static methods, but abstract classes can have instance variables, constructors, and private methods which interfaces cannot have.
Q.82Medium
Which of the following is true about interface default methods?
Answer: B
Default methods in interfaces (Java 8+) provide a default implementation. Implementing classes can use this default implementation or override it as needed.
Q.83Medium
Which of the following correctly demonstrates composition over inheritance?
Answer: B
Composition (HAS-A relationship) is often preferred over inheritance (IS-A relationship) because it's more flexible and avoids tight coupling.
Q.84Medium
Which of the following demonstrates proper method overloading in Java?
Answer: C
Method overloading requires different parameter types or number of parameters. Options A and D have different return types (not valid for overloading), and option B has the same signature with different parameter names (not overloading).
Q.85Medium
What will happen if you try to override a final method in a subclass?
Answer: B
A final method cannot be overridden. Attempting to override it results in a compilation error: 'cannot override final method'.
Advertisement
Q.86Medium
In Java, which of the following is true about abstract classes?
Answer: A
An abstract class can have zero or more abstract methods. It can also have concrete methods, constructors, and instance variables.
Q.87Medium
What is the correct way to call a parent class constructor from a child class?
Answer: B
The super() keyword is used to call the parent class constructor. It must be the first statement in the child class constructor.
Q.88Medium
Which statement about Java interfaces is INCORRECT according to 2024 specifications?
Answer: D
Interfaces cannot be instantiated directly. Since Java 8, interfaces can have static and default methods, and since Java 9, they can have private methods.
Q.89Medium
Consider a class hierarchy: Animal -> Dog -> Puppy. A Puppy reference can access which of the following?
Answer: C
Through inheritance, a Puppy reference can access all non-private methods from Puppy, Dog, and Animal classes through the inheritance chain.
Q.90Medium
What is the output of this code?
class Test {
static int x = 10;
public static void main(String[] args) {
System.out.println(x++);
}
}
Answer: A
The post-increment operator (x++) returns the value before incrementing. So it prints 10, and then x becomes 11.
Q.91Medium
In Java, if a parent class has a method void display() and a child class overrides it as void display(int x), what is this called?
Answer: B
This is method overloading, not overriding, because the method signature is different (different parameters). Overriding requires the same signature.
Q.92Medium
Consider a scenario where you have an interface Shape with a method calculateArea(). You implement this in classes Circle and Rectangle. Which OOP concept is being demonstrated?
Answer: D
The interface provides abstraction by hiding implementation details. Multiple classes implementing the same interface method demonstrates polymorphism (many forms).
Q.93Medium
What will be printed when this code executes?
class A { int x = 5; }
class B extends A { int x = 10; }
public class Test {
public static void main(String[] args) {
A a = new B();
System.out.println(a.x);
}
}
Answer: A
Variable shadowing occurs here. 'a' is of type A, so a.x refers to A's variable x which is 5. Variables are not overridden, only methods are.
Q.94Medium
Which of the following statements about 'this' keyword in Java is INCORRECT?
Answer: C
'this' cannot be used in static methods because static methods are class-level and don't have an instance context. 'this' always refers to an instance.
Q.95Medium
In Java, a class can implement multiple interfaces but can extend only one class. This design choice primarily supports which principle?
Answer: C
Multiple inheritance of implementation can cause the diamond problem. Java avoids this by allowing single class inheritance but multiple interface implementation.
Q.96Medium
In Java, when you override a method from a parent class, which of the following is NOT a valid reason to use the @Override annotation?
Answer: C
The @Override annotation helps verify that you are correctly overriding a parent method and improves code clarity, but it does NOT automatically invoke the parent class method. You must use super.methodName() for that. @Override is purely a compile-time marker for verification purposes.
Q.97Medium
Which method is used to remove an element from ArrayList while iterating?
Answer: B
Using remove() directly in loop causes ConcurrentModificationException. Iterator.remove() is the safe way to remove elements while iterating.
Q.98Medium
What is the load factor in HashMap? What is its default value?
Answer: B
Load factor is the ratio of size to capacity. Default load factor in HashMap is 0.75, which provides good balance between time and space complexity.
Q.99Medium
What is the time complexity of add() operation in TreeSet?
Answer: B
TreeSet is backed by a TreeMap which uses Red-Black tree. Add operation requires O(log n) time for tree balancing.
Q.100Medium
Which of the following is thread-safe?
Answer: C
ConcurrentHashMap is thread-safe and uses bucket-level locking. HashMap, TreeMap, and LinkedHashMap are not thread-safe.