Java MCQ

Previous Java Multiple Choice Questions Next

JAVA TRICKY polymorphism MCQ

1

What is the output of: class A { void show() { System.out.print("A"); } } class B extends A { void show() { System.out.print("B"); } } A obj = new B(); obj.show();

Correct Answer: B) B

Runtime polymorphism - the method called depends on the actual object type (B), not the reference type (A).

2

What is the output of: class A { static void show() { System.out.print("A"); } } class B extends A { static void show() { System.out.print("B"); } } A obj = new B(); obj.show();

Correct Answer: A) A

Static methods don't support runtime polymorphism. Method called depends on reference type, not object type.

3

What is the output of: class A { int x = 10; } class B extends A { int x = 20; } A obj = new B(); System.out.println(obj.x);

Correct Answer: A) 10

Variables don't support polymorphism in Java. Variable access depends on reference type, not object type.

4

What is the output of: class A { void show() { System.out.print("A"); } } class B extends A { void show() { super.show(); System.out.print("B"); } } A obj = new B(); obj.show();

Correct Answer: C) AB

B's show() method calls super.show() first (printing A), then prints B.

5

What is the output of: class A { private void show() { System.out.print("A"); } } class B extends A { public void show() { System.out.print("B"); } } A obj = new B(); obj.show();

Correct Answer: C) Compilation error

Private methods cannot be overridden. B's show() is a new method, not an override. obj.show() tries to call A's private method which is not accessible.

6

What is the output of: class A { void show(int x) { System.out.print("A"); } } class B extends A { void show(String s) { System.out.print("B"); } } A obj = new B(); obj.show(10);

Correct Answer: A) A

This is method overloading, not overriding. B's show(String) doesn't override A's show(int). So A's method is called.

7

What is the output of: class A { A() { show(); } void show() { System.out.print("A"); } } class B extends A { void show() { System.out.print("B"); } } new B();

Correct Answer: B) B

When A's constructor calls show(), it invokes the overridden version in B, even though B's constructor hasn't run yet.

8

What is the output of: class A { final void show() { System.out.print("A"); } } class B extends A { void show() { System.out.print("B"); } } B obj = new B(); obj.show();

Correct Answer: C) Compilation error

Final methods cannot be overridden. B cannot override A's final show() method.

9

What is the output of: class A { void show() throws Exception { System.out.print("A"); } } class B extends A { void show() { System.out.print("B"); } } A obj = new B(); obj.show();

Correct Answer: C) Compilation error

Overridden method cannot throw broader checked exception. B's show() should declare Exception or subclass.

10

What is the output of: interface I { default void show() { System.out.print("I"); } } class A implements I { public void show() { System.out.print("A"); } } I obj = new A(); obj.show();

Correct Answer: B) A

Default interface methods support polymorphism. A's overridden show() method is called based on actual object type.

Previous Java Multiple Choice Questions Next