Here are 10 essential multiple-choice questions on Java Inheritance and Abstraction, covering key concepts.
Question 1
Which of the following statements about the Object class in Java is true?
Every Java class extends Object class either directly or indirectly.
Only classes without a superclass extend Object class.
The Object class cannot be extended.
Object class is an abstract class.
Question 2
What will be the output of the following code?
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
Question 3
What is the primary purpose of encapsulation in Java?
To allow direct access to class fields.
To group related methods together.
To restrict access to data and ensure data hiding.
To implement multiple inheritance.
Question 4
Which keyword is used to refer to the immediate parent class in Java?
this
super
extends
parent
Question 5
What will be the output of this program?
abstract class Parent {
abstract void show();
}
class Child extends Parent {
void show() {
System.out.println("Child class method");
}
}
public class Main {
public static void main(String[] args) {
Parent p = new Child();
p.show();
}
}
Compilation Error
Runtime Error
Child class method
No Output
Question 6
What happens if an abstract class does not have any abstract methods?
It will not compile.
The class can still be abstract.
Java will automatically provide an abstract method.
It becomes a concrete class.
Question 7
What will be the output of this code?
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.makeSound();
}
}
Animal makes a sound
Dog barks
Compilation error
Runtime error
Question 8
Which of the following statements about inheritance is false?
Java supports single inheritance.
Java allows multiple class inheritance using extends.
Interfaces can be used to achieve multiple inheritance.
The super keyword can be used to invoke the parent class constructor.
Question 9
Which of the following statements about abstract classes is correct?
Abstract classes cannot have constructors.
Abstract classes cannot have static methods.
An object of an abstract class cannot be instantiated.
Abstract classes cannot have final methods.
Question 10
What is the main advantage of using abstraction in Java?
It increases the execution speed of the program.
It improves code reusability and reduces complexity.
It makes the code shorter.
It forces all methods to be implemented.
There are 10 questions to complete.