Code Snippets-based MCQs
Inheritance
Table of contents
Easy Level MCQs
- What will be the output of this code?
class Parent { String name = "Parent"; Parent() { System.out.println("Parent Constructor"); } } class Child extends Parent { String name = "Child"; Child() { System.out.println("Child Constructor"); } } public class Main { public static void main(String[] args) { Child child = new Child(); System.out.println(child.name); } }
Answer
Parent Constructor Child Constructor Child
- What will be the output of this code?
class Example { static int x = 10; static void print() { System.out.println(x); } } public class Main { public static void main(String[] args) { Example.x = 20; Example.print(); } }
Answer
20
- What will be the output of this code?
class A { static void display() { System.out.println("A"); } } class B extends A { static void display() { System.out.println("B"); } } public class Main { public static void main(String[] args) { A obj = new B(); obj.display(); } }
Answer
A
- What will be the output of this code?
class Test { int x; Test() { this(10); System.out.println("No-arg constructor"); } Test(int x) { this.x = x; System.out.println("Parameterized constructor"); } } public class Main { public static void main(String[] args) { Test t = new Test(); } }
Answer
Parameterized constructor No-arg constructor
- What will be the output of this code?
class Test { final int x; Test(int x) { this.x = x; } void display() { System.out.println(x); } } public class Main { public static void main(String[] args) { Test t = new Test(5); t.display(); } }
What will be the output of this code?
Answer
5
- What will be the output of this code?
class Test { void method(int a) { System.out.println("int"); } void method(double a) { System.out.println("double"); } } public class Main { public static void main(String[] args) { Test t = new Test(); t.method(5.0); } }
Answer
double
- What will be the output of this code?
class Test { void method() { System.out.println("Test Method"); } } public class Main { public static void main(String[] args) { Test t1 = new Test(); Test t2 = t1; t2.method(); } }
Answer
Test Method
-
What will be the output of this code?
class Animal { void makeSound() { System.out.println("Animal sound"); } } class Dog extends Animal { } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.makeSound(); } }
Answer
Animal sound
-
What will be the output of this code?
class Animal { void makeSound() { System.out.println("Animal sound"); } } class Cat extends Animal { void makeSound() { System.out.println("Meow"); } } public class Main { public static void main(String[] args) { Cat cat = new Cat(); cat.makeSound(); } }
Answer
Meow
-
What will be the output of this code?
class Parent { void show() { System.out.println("Parent class"); } } class Child extends Parent { } public class Main { public static void main(String[] args) { Child child = new Child(); child.show(); } }
Answer
Parent class
-
What will be the output of this code?
class Parent { Parent() { System.out.println("Parent Constructor"); } } class Child extends Parent { Child() { System.out.println("Child Constructor"); } } public class Main { public static void main(String[] args) { Child child = new Child(); } }
Answer
Parent Constructor Child Constructor
-
What will be the output of this code?
class Parent { void display() { System.out.println("Parent"); } } class Child extends Parent { void display() { super.display(); System.out.println("Child"); } } public class Main { public static void main(String[] args) { Child child = new Child(); child.display(); } }
Answer
Parent Child
-
What will be the output of this code?
class Parent { void message() { System.out.println("Parent message"); } } class Child extends Parent { void message() { System.out.println("Child message"); } void parentMessage() { super.message(); } } public class Main { public static void main(String[] args) { Child child = new Child(); child.parentMessage(); } }
Answer
Parent message
-
What will be the output of this code?
class Parent { public int number = 5; } class Child extends Parent { void show() { System.out.println(number); } } public class Main { public static void main(String[] args) { Child child = new Child(); child.show(); } }
Answer
5
-
What will be the output of this code?
class Parent { public String toString() { return "Parent class"; } } class Child extends Parent { } public class Main { public static void main(String[] args) { Child child = new Child(); System.out.println(child); } }
Answer
Parent class
-
What will be the output of this code?
class Vehicle { void start() { System.out.println("Vehicle started"); } } class Car extends Vehicle { void start() { System.out.println("Car started"); } } public class Main { public static void main(String[] args) { Car car = new Car(); car.start(); } }
Answer
Car started
-
What will be the output of this code?
class Parent { protected int number = 10; } class Child extends Parent { void display() { System.out.println(number); } } public class Main { public static void main(String[] args) { Child child = new Child(); child.display(); } }
Answer
10
Slightly Harder MCQs
-
What will be the output of this code?
class Parent { int value() { return 1; } } class Child extends Parent { double value() { return 2.0; } } public class Main { public static void main(String[] args) { Parent obj = new Child(); System.out.println(obj.value()); } }
Answer
Compile-time error
-
What will be the output of this code?
class Parent { int number = 5; } class Child extends Parent { int number = 10; void display() { System.out.println(number); System.out.println(super.number); } } public class Main { public static void main(String[] args) { Child child = new Child(); child.display(); } }
Answer
10 5
-
What will be the output of this code?
class Parent { Parent() { System.out.println("Parent Constructor"); } } class Child extends Parent { Child() { super(); System.out.println("Child Constructor"); } } public class Main { public static void main(String[] args) { Child child = new Child(); } }
Answer
Parent Constructor Child Constructor
-
What will be the output of this code?
class Parent { void display(int num) { System.out.println("Number: " + num); } } class Child extends Parent { void display(String str) { System.out.println("String: " + str); } } public class Main { public static void main(String[] args) { Child child = new Child(); child.display(10); child.display("Hello"); } }
Answer
Number: 10 String: Hello
-
What will be the output of this code?
class Parent { Parent(int x) { System.out.println("Parent Constructor with value: " + x); } } class Child extends Parent { Child(int x) { super(x); System.out.println("Child Constructor with value: " + x); } } public class Main { public static void main(String[] args) { Child child = new Child(5); } }
Answer
Parent Constructor with value: 5 Child Constructor with value: 5
Tricky MCQs
-
What will be the output of this code?
public class Main { public static void main(String[] args) { String s1 = "Hello"; String s2 = "Hello"; String s3 = new String("Hello"); System.out.println(s1 == s2); System.out.println(s1 == s3); } }
<summary>Answer</summary> true false
-
What will be the output of this code?
class A { void method() { System.out.println("Class A"); } } public class Main { public static void main(String[] args) { A obj = null; obj.method(); } }
<summary>Answer</summary> NullPointerException
-
What will be the output of this code?
class Parent { void show() { System.out.println("Parent show"); } } class Child extends Parent { void show() { System.out.println("Child show"); } } public class Main { public static void main(String[] args) { Parent parent = new Child(); parent.show(); } }
Answer
Child show
-
What will be the output of this code?
class Parent { int number = 5; } class Child extends Parent { int number = 10; void display() { System.out.println(number); } } public class Main { public static void main(String[] args) { Parent parent = new Child(); parent.display(); } }
Answer
Compile-time error
-
What will be the output of this code?
class Parent { Parent() { System.out.println("Parent Constructor"); } } class Child extends Parent { Child() { this(10); System.out.println("Child Constructor"); } Child(int x) { System.out.println("Child Constructor with value: " + x); } } public class Main { public static void main(String[] args) { Child child = new Child(); } }
Answer
Parent Constructor Child Constructor with value: 10 Child Constructor
-
What will be the output of this code?
class Parent { void display() { System.out.println("Parent display"); } } class Child extends Parent { void display() { System.out.println("Child display"); } } public class Main { public static void main(String[] args) { Parent parent = new Child(); parent.display(); } }
Answer
Child display
-
What will be the output of this code?
class Parent { Parent() { System.out.println("Parent Constructor"); show(); } void show() { System.out.println("Parent show"); } } class Child extends Parent { Child() { System.out.println("Child Constructor"); } void show() { System.out.println("Child show"); } } public class Main { public static void main(String[] args) { Child child = new Child(); } }
Answer
Parent Constructor Child show Child Constructor
-
What is the issue in this code?
class Parent { Parent(int x) { System.out.println("Parent Constructor"); } } class Child extends Parent { Child() { System.out.println("Child Constructor"); } }
Answer
The
super()
call is missing in theChild
constructor -
What is the issue in this code?
class Parent { void display(int x) { System.out.println("Parent: " + x); } } class Child extends Parent { void display(String x) { System.out.println("Child: " + x); } }
Answer
The
display
method inChild
is not an override, it’s an overload -
What is the issue in this code?
class Parent { int value() { return 10; } } class Child extends Parent { double value() { return 10.0; } }
Answer
The
value
method inChild
has an incompatible return type -
What is the issue in this code?
class Parent { Parent() { System.out.println("Parent Constructor"); } } class Child extends Parent { Child() { System.out.println("Child Constructor"); super(); } }
Answer
super()
should be the first statement in theChild
constructor -
What is the issue in this code?
class Parent { private void show() { System.out.println("Parent show"); } } class Child extends Parent { void show() { System.out.println("Child show"); } }
Answer
The
show
method inChild
does not override theshow
method inParent
-
What is the issue in this code?
class Parent { final void display() { System.out.println("Parent display"); } } class Child extends Parent { void display() { System.out.println("Child display"); } }
Answer
The
display
method inChild
cannot override the final method inParent
-
What is the issue in this code?
class Parent { private Parent() { System.out.println("Parent Constructor"); } } class Child extends Parent { Child() { System.out.println("Child Constructor"); } }
Answer
The
Parent
constructor is private and cannot be accessed byChild
-
What is the issue in this code?
class Parent { Parent() { this(10); } Parent(int x) { System.out.println("Parent Constructor with value: " + x); } } class Child extends Parent { Child() { System.out.println("Child Constructor"); } }
Answer
Recursive constructor invocation in
Parent
class -
What is the issue in this code?
class Parent { void display() { System.out.println("Parent display"); } } class Child extends Parent { void display(int x) { System.out.println("Child display: " + x); } }
Answer
Method
display(int x)
inChild
does not overridedisplay()
inParent