What is the difference between 'this' and 'super' keywords?
A'this' refers to parent class and 'super' refers to current class
B'this' refers to current class instance and 'super' refers to parent class
CThey are interchangeable
D'super' is used only in abstract classes
Correct Answer:
B. 'this' refers to current class instance and 'super' refers to parent class
EXPLANATION
'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.
Which of the following statements about the 'final' keyword is correct?
AA final class can be extended
BA final method can be overridden
CA final variable's value cannot be changed after initialization
DA final class can implement multiple interfaces
Correct Answer:
C. A final variable's value cannot be changed after initialization
EXPLANATION
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.
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(); } }
AParent
BChild
CCompile-time error
DRuntime error
Correct Answer:
B. Child
EXPLANATION
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.
Which keyword is used to achieve runtime polymorphism in Java?
Afinal
Bstatic
Cvirtual
DNone of the above
Correct Answer:
D. None of the above
EXPLANATION
Java achieves runtime polymorphism through method overriding using inheritance. Unlike C++, Java doesn't use the 'virtual' keyword; it's implicit for all non-final methods.
Which of the following correctly describes encapsulation?
AWrapping data and methods together and hiding internal details
BCreating multiple classes with same functionality
CExtending one class from another
DImplementing an interface
Correct Answer:
A. Wrapping data and methods together and hiding internal details
EXPLANATION
Encapsulation is the bundling of data (variables) and methods into a single unit (class) while hiding the internal implementation details from the outside world. It is achieved using access modifiers.
What is the main difference between method overriding and method overloading?
AOverriding is for same class, overloading is for different classes
BOverriding changes method signature, overloading keeps it same
COverriding is runtime polymorphism, overloading is compile-time polymorphism
DOverriding requires final keyword, overloading doesn't
Correct Answer:
C. Overriding is runtime polymorphism, overloading is compile-time polymorphism
EXPLANATION
Method overloading is compile-time (static) polymorphism with same method name but different parameters in the same class. Method overriding is runtime (dynamic) polymorphism where a child class provides a specific implementation of a parent method.
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?
AOnly call sound()
BOnly call sleep()
CCall both sound() and sleep()
DNeither sound() nor sleep()
Correct Answer:
C. Call both sound() and sleep()
EXPLANATION
Dog inherits the concrete method sleep() from Animal and provides implementation for the abstract method sound(). Therefore, Dog objects can call both methods.