menu

Inheritance in Java


1. What is the purpose of the 'super' keyword in Java?

To call a method or constructor of a superclass

To declare a superclass

To access a private field of a superclass

To override a method of a superclass


2.

What is the output of the following program?

class A {
int x = 10;
}
class B extends A {
int x = 20;
}
public class Main {
public static void main(String[] args) {
A obj1 = new A();
B obj2 = new B();
System.out.println(obj1.x);
System.out.println(obj2.x);
}
}

10, 20

20, 20

10, 10

Compilation error


3. Which access modifier allows a subclass to access the protected members of its parent class?

private

public

protected

default


4. Which keyword is used to implement an abstract method in a subclass in Java?

abstract

final

static

override


5. Which method of the Object class is called when an object is no longer referenced?

finalize()

delete()

destroy()

dispose()


6. Which type of inheritance allows a class to inherit properties and behavior from multiple parent classes?

Single inheritance

Multiple inheritance

Multilevel inheritance

Hybrid inheritance


7. Which of the following is an example of multilevel inheritance?

Class A extends Class B, Class C extends Class A

Class A extends Class B, Class B extends Class C

Class A extends Class B, Class A extends Class C

Class A extends Class B, Class B extends Class A


8. Which type of inheritance is a combination of single and multiple inheritance?

Hybrid inheritance

Multiple inheritance

Hierarchical inheritance

Multilevel inheritance


9. Which keyword is used to create an abstract class in Java?

abstract

final

static

synchronized


10. Which keyword is used to create a subclass in Java?

abstract

final

static

extends