Code Snippets-based MCQs

Inheritance

Table of contents
  1. Easy Level MCQs
  2. Slightly harder MCQs
  3. Tricky MCQs

Easy Level MCQs

  1. 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);
         }
     }
    
    Parent Constructor Child Constructor Child
    Parent Constructor Parent Constructor Child
    Child Constructor Parent Constructor Child
    Child Constructor Child Constructor Child
    Answer

    Parent Constructor Child Constructor Child

  2. 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();
        }
    }
    
    10
    20
    Compile-time error
    No output
    Answer

    20

  3. 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();
        }
    }
    
    A
    B
    Compile-time error
    No output
    Answer

    A

  4. 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();
        }
    }
    
    No-arg constructor
    Parameterized constructor
    Parameterized constructor No-arg constructor
    No-arg constructor Parameterized constructor
    Answer

    Parameterized constructor No-arg constructor

  5. 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?

    0
    5
    Compile-time error
    No output
    Answer

    5

  6. 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);
        }
    }
    
    int
    double
    Compile-time error
    No output
    Answer

    double

  7. 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();
         }
     }
    
    Test Method
    Compile-time error
    Runtime exception
    No output
    Answer

    Test Method

  8. 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();
         }
     }
    
    Animal sound
    Dog sound
    Compile-time error
    No output
    Answer

    Animal sound

  9. 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();
         }
     }
    
    Animal sound
    Meow
    Compile-time error
    No output
    Answer

    Meow

  10. 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();
        }
    }
    
    Parent class
    Child class
    Compile-time error
    No output
    Answer

    Parent class

  11. 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();
        }
    }
    
    Parent Constructor Child Constructor
    Child Constructor Parent Constructor
    Compile-time error
    No output
    Answer

    Parent Constructor Child Constructor

  12. 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();
        }
    }
    
    Parent Child
    Child Parent
    Compile-time error
    No output
    Answer

    Parent Child

  13. 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();
        }
    }
    
    Parent message
    Child message
    Compile-time error
    No output
    Answer

    Parent message

  14. 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();
        }
    }
    
    5
    0
    Compile-time error
    No output
    Answer

    5

  15. 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);
        }
    }
    
    Parent class
    Child class
    Compile-time error
    No output
    Answer

    Parent class

  16. 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();
        }
    }
    
    Vehicle started
    Car started
    Compile-time error
    No output
    Answer

    Car started

  17. 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();
        }
    }
    
    10
    0
    Compile-time error
    No output
    Answer

    10

Back to Top

Slightly Harder MCQs

  1. 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());
        }
    }
    
    1
    2.0
    Compile-time error
    Runtime error
    Answer

    Compile-time error

  2. 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();
         }
     }
    
    5 5
    10 5
    5 10
    10 10
    Answer

    10 5

  3. 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();
         }
     }
    
    Parent Constructor Child Constructor
    Child Constructor Parent Constructor
    Compile-time error
    No output
    Answer

    Parent Constructor Child Constructor

  4. 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");
         }
     }
    
    Number: 10 String: Hello
    String: Hello Number: 10
    Compile-time error
    No output
    Answer

    Number: 10 String: Hello

  5. 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);
         }
     }
    
    Parent Constructor with value: 5 Child Constructor with value: 5
    Child Constructor with value: 5 Parent Constructor with value: 5
    Compile-time error
    No output
    Answer

    Parent Constructor with value: 5 Child Constructor with value: 5

Tricky MCQs

  1. 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);
         }
     }
    
    true true
    false true
    true false
    false false
     <summary>Answer</summary>
     true false
    
  2. 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();
        }
    }
    
    Class A
    Compile-time error
    NullPointerException
    No output
    <summary>Answer</summary>
    NullPointerException
    
  3. 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();
         }
     }
    
    Parent show
    Child show
    Compile-time error
    Runtime error
    Answer

    Child show

  4. 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();
         }
     }
    
    5
    10
    Compile-time error
    Runtime error
    Answer

    Compile-time error

  5. 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();
         }
     }
    
    Parent Constructor Child Constructor
    Parent Constructor Child Constructor with value: 10 Child Constructor
    Compile-time error
    Runtime error
    Answer

    Parent Constructor Child Constructor with value: 10 Child Constructor

  6. 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();
         }
     }
    
    Parent display
    Child display
    Compile-time error
    Runtime error
    Answer

    Child display

  7. 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();
         }
     }
    
    Parent Constructor Parent show Child Constructor
    Parent Constructor Child show Child Constructor
    Child show Child Constructor
    Parent Constructor Parent show Parent show Child Constructor
    Answer

    Parent Constructor Child show Child Constructor

  8. 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");
         }
     }
    
    The `super()` call is missing in the `Child` constructor
    The `Child` constructor should take an argument
    The `Parent` constructor should have a no-argument constructor
    There is no issue in the code
    Answer

    The super() call is missing in the Child constructor

  9. 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);
         }
     }
    
    Method `display` in `Child` should override `display` in `Parent`
    The `display` method in `Child` is not an override, it's an overload
    The `Parent` class should be declared as abstract
    There is no issue in the code
    Answer

    The display method in Child is not an override, it’s an overload

  10. What is the issue in this code?

     class Parent {
         int value() {
             return 10;
         }
     }
    
     class Child extends Parent {
         double value() {
             return 10.0;
         }
     }
    
    The `value` method in `Child` has an incompatible return type
    The `value` method in `Parent` should return `double`
    The `Child` class must use `super.value()`
    There is no issue in the code
    Answer

    The value method in Child has an incompatible return type

  11. 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();
         }
     }
    
    `super()` should be the first statement in the `Child` constructor
    `super()` should not be used in the `Child` constructor
    The `Parent` class must take a parameter
    There is no issue in the code
    Answer

    super() should be the first statement in the Child constructor

  12. 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");
         }
     }
    
    The `show` method in `Child` does not override the `show` method in `Parent`
    The `show` method in `Parent` should be protected
    The `Child` class cannot define a method with the same name
    There is no issue in the code
    Answer

    The show method in Child does not override the show method in Parent

  13. 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");
         }
     }
    
    The `display` method in `Child` cannot override the final method in `Parent`
    The `display` method in `Parent` should not be final
    The `Child` class should extend a different class
    There is no issue in the code
    Answer

    The display method in Child cannot override the final method in Parent

  14. 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");
         }
     }
    
    The `Parent` constructor is private and cannot be accessed by `Child`
    The `Child` constructor should be private
    The `Parent` constructor should be protected
    There is no issue in the code
    Answer

    The Parent constructor is private and cannot be accessed by Child

  15. 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");
         }
     }
    
    Recursive constructor invocation in `Parent` class
    The `Child` class should also have a parameterized constructor
    The `Parent` constructor should not take an argument
    There is no issue in the code
    Answer

    Recursive constructor invocation in Parent class

  16. 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);
         }
     }
    
    Method ``display(int x)`` in `Child` does not override `display()` in `Parent`
    Method `display(int x)` should be private
    The `Parent` class should take an argument
    There is no issue in the code
    Answer

    Method display(int x) in Child does not override display() in Parent