Java MCQ
JAVA TRICKY polymorphism MCQ
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();
Runtime polymorphism - the method called depends on the actual object type (B), not the reference type (A).
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();
Static methods don't support runtime polymorphism. Method called depends on reference type, not object type.
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);
Variables don't support polymorphism in Java. Variable access depends on reference type, not object type.
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();
B's show() method calls super.show() first (printing A), then prints B.
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();
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.
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);
This is method overloading, not overriding. B's show(String) doesn't override A's show(int). So A's method is called.
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();
When A's constructor calls show(), it invokes the overridden version in B, even though B's constructor hasn't run yet.
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();
Final methods cannot be overridden. B cannot override A's final show() method.
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();
Overridden method cannot throw broader checked exception. B's show() should declare Exception or subclass.
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();
Default interface methods support polymorphism. A's overridden show() method is called based on actual object type.