Saturday, August 3, 2024

Inheritance MCQs

 Inheritance MCQs

What is inheritance in Java?


A. A way to provide default values to variables

B. A mechanism to create new classes using existing classes

C. A method to define multiple classes in a single file

D. A way to handle exceptions

Which keyword is used to inherit a class in Java?


A. extends

B. implements

C. inherits

D. super

What is the main purpose of inheritance?


A. To reuse code and establish a hierarchical relationship

B. To handle runtime errors

C. To create multiple instances of a class

D. To make a class final

Which of the following statements about inheritance is true?


A. A subclass can inherit both methods and variables from a superclass

B. A superclass can inherit methods and variables from a subclass

C. Inheritance allows a subclass to access private members of the superclass

D. A class can inherit multiple classes in Java

Which type of inheritance is not supported in Java?


A. Single inheritance

B. Multiple inheritance (through classes)

C. Hierarchical inheritance

D. Multilevel inheritance

What will be the output of the following code?


java

Copy code

class Parent {

    void show() {

        System.out.println("Parent");

    }

}


class Child extends Parent {

    void show() {

        System.out.println("Child");

    }

}


public class Main {

    public static void main(String[] args) {

        Parent p = new Child();

        p.show();

    }

}

A. Parent

B. Child

C. Compilation error

D. Runtime exception

What is the purpose of the super keyword in Java?


A. To call a method in the superclass

B. To create a new instance of the superclass

C. To access private members of the superclass

D. To define a subclass

Which of the following cannot be inherited?


A. Instance variables

B. Methods

C. Static variables

D. Constructors

What happens if you do not explicitly define a constructor in a subclass?


A. The subclass will automatically have a default constructor

B. The subclass will not compile

C. The superclass constructor is not called

D. The subclass will use the constructor of the superclass

Which of the following is true about method overriding in Java?


A. The method in the subclass must have the same name, return type, and parameters as the method in the superclass

B. The method in the subclass can have a different return type

C. The method in the subclass must be static

D. The method in the subclass must have a different name

What is the output of the following code?


java

Copy code

class Animal {

    void sound() {

        System.out.println("Animal sound");

    }

}


class Dog extends Animal {

    void sound() {

        System.out.println("Woof");

    }

}


public class Test {

    public static void main(String[] args) {

        Animal a = new Dog();

        a.sound();

    }

}

A. Animal sound

B. Woof

C. Compilation error

D. Runtime exception

What is the role of the final keyword in inheritance?


A. It prevents a class from being subclassed

B. It allows a class to be subclassed

C. It prevents methods from being overridden

D. It allows methods to be overridden

Which of the following is true about Java's multiple inheritance support?


A. Java supports multiple inheritance through interfaces

B. Java supports multiple inheritance through classes

C. Java does not support multiple inheritance at all

D. Java supports multiple inheritance with the extends keyword

What will be the output of the following code?


java

Copy code

class Base {

    void display() {

        System.out.println("Base");

    }

}


class Derived extends Base {

    void display() {

        System.out.println("Derived");

    }

    

    void show() {

        super.display();

    }

}


public class Main {

    public static void main(String[] args) {

        Derived d = new Derived();

        d.show();

    }

}

A. Base

B. Derived

C. Base Derived

D. Derived Base

How can you call the superclass constructor from a subclass?


A. Using this()

B. Using super()

C. Using parent()

D. Using super keyword directly

Which keyword is used to refer to the superclass instance members?


A. this

B. super

C. parent

D. instance

What will be the output of the following code?


java

Copy code

class Parent {

    int value = 10;

}


class Child extends Parent {

    int value = 20;

    

    void display() {

        System.out.println(super.value);

    }

}


public class Main {

    public static void main(String[] args) {

        Child c = new Child();

        c.display();

    }

}

A. 10

B. 20

C. Compilation error

D. Runtime exception

Which of the following can a subclass access from its superclass?


A. Private members

B. Protected members

C. Default (package-private) members in a different package

D. Static methods only

What is the output of the following code?


java

Copy code

class Parent {

    void method() {

        System.out.println("Parent");

    }

}


class Child extends Parent {

    void method() {

        System.out.println("Child");

    }

    

    void callParentMethod() {

        super.method();

    }

}


public class Main {

    public static void main(String[] args) {

        Child c = new Child();

        c.callParentMethod();

    }

}

A. Parent

B. Child

C. Parent Child

D. Child Parent

In which scenario is method overriding NOT possible?


A. When the method in the subclass has a different return type

B. When the method in the superclass is private

C. When the method in the superclass is static

D. When the method in the superclass is final

Answers:

B. A mechanism to create new classes using existing classes

A. extends

A. To reuse code and establish a hierarchical relationship

A. A subclass can inherit both methods and variables from a superclass

B. Multiple inheritance (through classes)

B. Child

A. To call a method in the superclass

D. Constructors

A. The subclass will automatically have a default constructor

A. The method in the subclass must have the same name, return type, and parameters as the method in the superclass

B. Woof

A. It prevents a class from being subclassed

A. Java supports multiple inheritance through interfaces

A. Base

B. Using super()

B. super

A. 10

B. Protected members

A. Parent

B. When the method in the superclass is private

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...