Java MCQ
Java Tricky Inheritance Questions
What is the output?
class A {
void print() { System.out.println("A"); }
}
class B extends A {
void print() { System.out.println("B"); }
}
public static void main(String[] args) {
A obj = new B();
obj.print();
}
void print() { System.out.println("A"); }
}
class B extends A {
void print() { System.out.println("B"); }
}
public static void main(String[] args) {
A obj = new B();
obj.print();
}
This demonstrates runtime polymorphism. Even though the reference type is A, the actual object is B, so B's overridden print() method is called.
What will be printed?
class Parent {
Parent() {
System.out.print("Parent ");
}
}
class Child extends Parent {
Child() {
System.out.print("Child ");
}
}
public static void main(String[] args) {
new Child();
}
Parent() {
System.out.print("Parent ");
}
}
class Child extends Parent {
Child() {
System.out.print("Child ");
}
}
public static void main(String[] args) {
new Child();
}
When creating a Child object, the parent constructor is automatically called first (implicit super()), then the child constructor executes.
What is the output?
class X {
int value = 10;
}
class Y extends X {
int value = 20;
}
public static void main(String[] args) {
X obj = new Y();
System.out.println(obj.value);
}
int value = 10;
}
class Y extends X {
int value = 20;
}
public static void main(String[] args) {
X obj = new Y();
System.out.println(obj.value);
}
Fields are resolved at compile-time based on reference type, not runtime object type. Since obj is of type X, it accesses X's value field.
What will be printed?
class Animal {
static void sound() { System.out.print("Animal "); }
}
class Dog extends Animal {
static void sound() { System.out.print("Dog "); }
}
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
static void sound() { System.out.print("Animal "); }
}
class Dog extends Animal {
static void sound() { System.out.print("Dog "); }
}
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
Static methods are resolved at compile-time based on reference type, not runtime object type. This is method hiding, not overriding.
What is the output?
class Base {
Base() {
print();
}
void print() { System.out.print("Base "); }
}
class Derived extends Base {
int value = 10;
void print() { System.out.print(value + " "); }
}
public static void main(String[] args) {
new Derived();
}
Base() {
print();
}
void print() { System.out.print("Base "); }
}
class Derived extends Base {
int value = 10;
void print() { System.out.print(value + " "); }
}
public static void main(String[] args) {
new Derived();
}
When Base constructor calls print(), it invokes Derived's overridden method. At this point, Derived's fields are not initialized yet (value is still 0).
What will be printed?
class P {
private void method() { System.out.print("P "); }
}
class Q extends P {
public void method() { System.out.print("Q "); }
}
public static void main(String[] args) {
P obj = new Q();
obj.method();
}
private void method() { System.out.print("P "); }
}
class Q extends P {
public void method() { System.out.print("Q "); }
}
public static void main(String[] args) {
P obj = new Q();
obj.method();
}
Private methods cannot be overridden. Q's method() is a new method, not an override. Since obj is of type P and method() is private in P, it's not accessible.
What is the output?
class Alpha {
Alpha() { show(); }
void show() { System.out.print("Alpha "); }
}
class Beta extends Alpha {
Beta() { show(); }
void show() { System.out.print("Beta "); }
}
public static void main(String[] args) {
new Beta();
}
Alpha() { show(); }
void show() { System.out.print("Alpha "); }
}
class Beta extends Alpha {
Beta() { show(); }
void show() { System.out.print("Beta "); }
}
public static void main(String[] args) {
new Beta();
}
Both constructor calls invoke the overridden show() method from Beta class due to runtime polymorphism, even when called from Alpha constructor.
What will be printed?
interface I {
default void method() { System.out.print("I "); }
}
class C implements I {
public void method() { System.out.print("C "); }
}
public static void main(String[] args) {
I obj = new C();
obj.method();
}
default void method() { System.out.print("I "); }
}
class C implements I {
public void method() { System.out.print("C "); }
}
public static void main(String[] args) {
I obj = new C();
obj.method();
}
The class overrides the interface's default method. Runtime polymorphism ensures the class's implementation is called.
What is the output?
class Super {
String msg = "Super";
}
class Sub extends Super {
String msg = "Sub";
}
public static void main(String[] args) {
Super sup = new Sub();
System.out.println(sup.msg);
System.out.println(((Sub)sup).msg);
}
String msg = "Super";
}
class Sub extends Super {
String msg = "Sub";
}
public static void main(String[] args) {
Super sup = new Sub();
System.out.println(sup.msg);
System.out.println(((Sub)sup).msg);
}
Sub
Field access is determined by reference type. First access uses Super reference (gets "Super"), second uses cast to Sub (gets "Sub").
What will be printed?
class One {
One() { print(); }
void print() { System.out.print("One "); }
}
class Two extends One {
Two() { print(); }
void print() { System.out.print("Two "); }
}
class Three extends Two {
Three() { print(); }
void print() { System.out.print("Three "); }
}
public static void main(String[] args) {
new Three();
}
One() { print(); }
void print() { System.out.print("One "); }
}
class Two extends One {
Two() { print(); }
void print() { System.out.print("Two "); }
}
class Three extends Two {
Three() { print(); }
void print() { System.out.print("Three "); }
}
public static void main(String[] args) {
new Three();
}
All constructor calls invoke the most overridden print() method from Three class due to runtime polymorphism in multi-level inheritance.