menu

Inheritance in Java

Inheritance in Java - Important Points


26. Which type of inheritance is used in Java for classes?

A. Single inheritance

B. Multiple inheritance

C. Hybrid inheritance

D. Hierarchical inheritance

Discuss Work Space

Answer: option a

Explanation:

Single inheritance is used in Java for classes.

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

A. Code reusability

B. Polymorphism

C. Encapsulation

D. Modularity

Discuss Work Space

Answer: option c

Explanation:

Encapsulation is not a benefit of inheritance in Java, as it is a separate concept.

28. What is the access modifier of a method or field that is inherited by a subclass in Java?

A. public

B. private

C. protected

D. default

Discuss Work Space

Answer: option c

Explanation:

The access modifier of a method or field that is inherited by a subclass in Java is 'protected'.

29.

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();
}
}

A. Class A

B. Class B

C. Compilation error

D. Runtime error

Discuss Work Space

Answer: option b

Explanation:

The output of the above code will be 'Class B', as the 'display()' method in class B overrides the method in class A.

30.

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);
}
}

A. 10, 20

B. 20, 20

C. 10, 10

D. Compilation error

Discuss Work Space

Answer: option a

Explanation:

The output of the above code will be '10, 20', as the variable 'x' in class A and class B have different values.


Subscribe for Latest Career Trends
Subscribe Now
Use AI and ChatGPT for Career Guidance

Unlock Your Future

Join Now
Worried for Placements in 2024?

Join FAST TRACK Course

Join Now
Supercharge Your SUCCESS

Join All in One Placement Mock Tests-2024

Join Now