menu

Inheritance in Java


1. Which of the following is not a type of inheritance in Java?

Single inheritance

Multiple inheritance

Multilevel inheritance

Hybrid inheritance


2. Which of the following is NOT a benefit of inheritance?

Reusability of code

Increased complexity of code

Polymorphism

Reduced development time


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

override

final

abstract

none of the above


4. Which of the following is not a superclass of the String class in Java?

Object

CharSequence

Number

Comparable


5.

What is the output of the following program?

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

30

20

10

Compilation error


6. Which keyword is used to prevent a class from being inherited by other classes?

abstract

final

static

synchronized


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

final

static

abstract

override


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

Code reusability

Polymorphism

Encapsulation

Modularity


9.

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


10. Which method of the Object class is used to return a hash code value for an object?

hashCode()

equals()

toString()

getClass()