'this' is a reference variable that refers to the current object instance. It is commonly used to distinguish instance variables from parameters with the same name.
Q.22Medium
Consider:
interface I1 { void method(); }
interface I2 { void method(); }
class C implements I1, I2 { public void method() { System.out.println("Method"); } }
What will be the behavior?
Answer: B
When a class implements multiple interfaces with the same method signature, a single method implementation satisfies all interfaces. This is resolved at compile time.
Q.23Medium
What is method overloading?
Answer: A
Method overloading allows multiple methods with the same name but different parameters (number, type, or order). It is a compile-time (static) polymorphism.
Q.24Medium
Which of the following is true about abstract classes?
Answer: A
Abstract classes can have constructors to initialize instance variables. They can contain both abstract and concrete methods, and they cannot be directly instantiated.
Q.25Medium
Consider the code:
abstract class Animal { abstract void sound(); void sleep() { System.out.println("Zzz"); } }
class Dog extends Animal { void sound() { System.out.println("Bark"); } }
What can Dog objects do?
Answer: C
Dog inherits the concrete method sleep() from Animal and provides implementation for the abstract method sound(). Therefore, Dog objects can call both methods.
Advertisement
Q.26Medium
Which statement is true about the default access modifier (package-private) in Java?
Answer: B
The default (package-private) access modifier allows access to members from any class within the same package, but not from classes in other packages.
Q.27Medium
What is the output of the following code?
class Parent { void show() { System.out.println("Parent"); } }
class Child extends Parent { void show() { System.out.println("Child"); } }
public class Test { public static void main(String[] args) { Parent p = new Child(); p.show(); } }
Answer: B
This demonstrates runtime polymorphism. Although the reference is of type Parent, the actual object is Child. The overridden show() method in Child class is executed.
Q.28Medium
Which of the following statements about the 'final' keyword is correct?
Answer: C
The 'final' keyword when applied to a variable makes it a constant. When applied to a class, it cannot be extended. When applied to a method, it cannot be overridden.
Q.29Medium
What is the difference between 'this' and 'super' keywords?
Answer: B
'this' is a reference to the current object instance, while 'super' is a reference to the parent class. They serve different purposes in inheritance hierarchy.
Q.30Medium
Consider a scenario where class B extends class A. If class A has a constructor with parameters, what must class B do?
Answer: B
If a parent class has a parameterized constructor and no default constructor, the child class must explicitly call it using super() to initialize parent class members.
Q.31Medium
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.32Medium
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.33Medium
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.34Medium
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.35Medium
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'.
Q.36Medium
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.37Medium
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.38Medium
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.39Medium
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.40Medium
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.