Java MCQ

Previous Java Multiple Choice Questions Next

Java Tricky Inheritance Questions

1

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();
}

Correct Answer: B) B

This demonstrates runtime polymorphism. Even though the reference type is A, the actual object is B, so B's overridden print() method is called.

2

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();
}

Correct Answer: A) Parent Child

When creating a Child object, the parent constructor is automatically called first (implicit super()), then the child constructor executes.

3

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);
}

Correct Answer: A) 10

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.

4

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();
}

Correct Answer: A) Animal

Static methods are resolved at compile-time based on reference type, not runtime object type. This is method hiding, not overriding.

5

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();
}

Correct Answer: C) 0

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).

6

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();
}

Correct Answer: C) Compilation Error

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.

7

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();
}

Correct Answer: B) Beta Beta

Both constructor calls invoke the overridden show() method from Beta class due to runtime polymorphism, even when called from Alpha constructor.

8

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();
}

Correct Answer: B) C

The class overrides the interface's default method. Runtime polymorphism ensures the class's implementation is called.

9

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);
}

Correct Answer: C) Super
Sub

Field access is determined by reference type. First access uses Super reference (gets "Super"), second uses cast to Sub (gets "Sub").

10

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();
}

Correct Answer: B) Three Three Three

All constructor calls invoke the most overridden print() method from Three class due to runtime polymorphism in multi-level inheritance.

Previous Java Multiple Choice Questions Next