menu

Inheritance in Java


1. What is the first method that is executed when an object of a class is created?

finalize()

constructor

main()

start()


2. Which keyword is used to create an interface in Java?

abstract

interface

implements

extends


3.

What is the output of the following program?

class A {
void display() {
System.out.println("Class A");
}
}
class B extends A {
void display() {
super.display();
System.out.println("Class B");
}
}
public class Main {
public static void main(String[] args) {
B obj = new B();
obj.display();
}
}

Class A, Class B

Class B, Class A

Class A

Class B


4. Which keyword is used to implement method overriding in Java?

final

static

abstract

override


5. Which keyword is used to implement method overloading in Java?

override

final

abstract

none of the above


6. What is the name of the process of creating a new class by extending an existing class?

Abstraction

Polymorphism

Inheritance

Encapsulation


7.

What is the output of the following program?

class A {
void display() {
System.out.println("Class A");
}
}
class B extends A {
void display() {
System.out.println("Class B");
}
}
public class Main {
public static void main(String[] args) {
A obj = new B();
obj.display();
}
}

Class A

Class B

Compilation error

Runtime error


8. Which of the following is not a benefit of inheritance in Java?

Code reusability

Polymorphism

Encapsulation

Modularity


9. Which type of inheritance is used in Java for interfaces?

Single inheritance

Multiple inheritance

Hybrid inheritance

Hierarchical inheritance


10. Which keyword is used to prevent a class from being inherited in Java?

final

static

abstract

private