This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1Which of the following type of inheritance is not directly implemented in Java?
Option B
Java supports inheritance, which allows one class to acquire the properties and methods of another class. Java directly supports single, multilevel, and hierarchical inheritance through classes. However, multiple inheritance is not directly supported through classes.
In multiple inheritance, one class would inherit from two or more classes. Java does not allow this because it can create ambiguity when both parent classes contain methods with the same name. For example, Java does not allow:
class A
{
void display()
{
System.out.println("Class A");
}
}
class B
{
void display()
{
System.out.println("Class B");
}
}
class C extends A, B // Not Allowed
{
}
Java can achieve a form of multiple inheritance through interfaces.
interface A
{
void displayA();
}
interface B
{
void displayB();
}
class C implements A, B
{
public void displayA()
{
System.out.println("A display()");
}
public void displayB()
{
System.out.println("B display()");
}
}
class Example
{
public static void main(String args[])
{
C cobj = new C();
cobj.displayA();
cobj.displayB();
}
}
OUTPUT
A display()
B display()
Q: 2Choose the incorrect statement—
Option D
In Java, the final keyword is used to restrict modification and ensure immutability where needed. A method declared as final cannot be overridden in subclasses, which helps in maintaining consistent behavior.
class Parent {
final void show() {
System.out.println("This is a final method");
}
}
class Child extends Parent {
void show() {} // Compile-Time Error.
}A class declared as final cannot be inherited, which is useful for creating immutable classes or preventing extension.
final class MyClass {
int value = 100;
}
class NewClass extends MyClass { } //Compile-Time Error.If a variable is declared final, its value cannot be changed, regardless of whether it is inside a final method.
final int p = 33;
final void modifyValue() {
p = 20; //Compile-Time Error.
}The finalize() method in Java is a special method of the Object class that is called by the garbage collector before an object is removed from memory. It can be used to perform cleanup tasks, such as closing file streams, releasing network connections, or freeing other system resources held by the object.
class MyClass {
protected void finalize()throws Throwable {
System.out.println("Object is being Garbage Collected");
}
}
Q: 3In Java, which of the following cannot be inherited by subclass from its superclass?
Option D
In Java, when a subclass inherits from a superclass, it can inherit accessible fields, methods, and nested classes. However, constructors are not inherited by subclasses.
A constructor is used to initialize an object of the class in which it is declared. When a subclass object is created, the superclass constructor is invoked through super(), but the constructor itself does not become a member of the subclass.
E.g.:
class Parent
{
int number = 100;
Parent()
{
System.out.println("Parent Constructor Called");
}
void display()
{
System.out.println("Parent display() Method Called");
}
class Inner
{
void show()
{
System.out.println("Nested Class show() Method Called");
}
}
}
class Child extends Parent
{
Child()
{
System.out.println("Child Constructor Called");
}
}
class Example
{
public static void main(String args[])
{
Child obj = new Child();
System.out.println("Inherited Field: " + obj.number);
obj.display();
Parent.Inner inner = obj.new Inner();
inner.show();
// Constructor is Not Inherited.
// obj.Parent(); // Compilation Error
}
}
OUTPUT
Parent Constructor Called
Child Constructor Called
Inherited Field: 100
Parent display() Method Called
Nested Class show() Method Called
Q: 4If A is a superclass and B is a subclass of it and C is subclass of B, in what order the constructors that make up the classes be called?
Option B
In Java, constructors are special methods used to initialize objects when they are created. When dealing with inheritance, constructors follow a specific order to ensure that the parent class is properly initialized before the child class.
If a class A is a superclass, B is a subclass of A, and C is a subclass of B, then creating an object of class C triggers constructor calls in a Top-Down order, starting from the highest superclass down to the actual subclass.
This happens because the subclass constructor implicitly calls the constructor of its parent class using the super().
class A {
A() {
System.out.println("Constructor of Class A.");
}
}
class B extends A {
B() {
System.out.println("Constructor of Class B.");
}
}
class C extends B {
C() {
System.out.println("Constructor of Class C.");
}
}
public class Main {
public static void main(String args[]) {
C obj = new C();
}
}
OUTPUT
Constructor of Class A.
Constructor of Class B.
Constructor of Class C.
Q: 5In Java, which keyword is used to implement inheritance between classes?
Option C
Inheritance is an important feature of Object-Oriented Programming (OOP) in Java. It allows one class, called the subclass or child class, to acquire the properties and methods of another class, called the superclass or parent class. This helps in code reusability and allows a class to extend the functionality of an existing class.
In Java, the extends keyword is used when one class inherits from another class. For example, if class Student inherits from class Person, we write:
class Student extends Person
{
}
Here, Student is the subclass, and Person is the superclass. The Student class can access the accessible members of the Person class.
Q: 6In Java, what is a Base Class?
Option B
In Java, a Base Class (Parent Class or Super Class) is a class whose properties and methods are inherited by other classes. It serves as a foundation from which new classes (derived classes or subclasses) can extend functionality. This helps in reusing common code, reducing duplication, and implementing object-oriented feature like inheritance.
Syntax:
class BaseClass
{
……………..;
……………..;
}
class DerivedClass extends BaseClass
{
……………..;
……………..;
}
Q: 7If a subclass constructor does not explicitly invoke a superclass constructor, what happens?
Option A
In Java, when a subclass constructor does not explicitly call a superclass constructor using super(), the compiler automatically inserts super() as the first statement of the subclass constructor. This means Java attempts to call the accessible no-argument constructor of the superclass.
E.g.:
class A
{
A()
{
System.out.println("A Constructor");
}
}
class B extends A
{
B()
{
System.out.println("B Constructor");
}
}
class TestExample
{
public static void main(String args[])
{
B obj = new B();
}
}
OUTPUT
A Constructor
B Constructor
The compiler effectively treats the constructor of B as if it were:
B()
{
super();
System.out.println("B Constructor");
}
Therefore, when new B() is executed, the superclass constructor runs first, followed by the subclass constructor.
Note:
The important point is that the compiler does not automatically call every superclass constructor. It inserts a call to the no-argument constructor only.
Q: 8Which member cannot be inherited in the usual sense?
Option C
In Java, a constructor is not inherited by a subclass. Constructors are used to initialize objects of the class in which they are declared.
Although a subclass constructor can call a superclass constructor using super(), the superclass constructor itself does not become a member of the subclass.
E.g.:
class A
{
A()
{
System.out.println("A Constructor");
}
void show()
{
System.out.println("A Method");
}
}
class B extends A
{
B()
{
System.out.println("B Constructor");
}
}
class TestExample
{
public static void main(String args[])
{
B obj = new B();
obj.show();
}
}
OUTPUT
A Constructor
B Constructor
A Method
Here, B inherits the show() method from A, so obj.show() is valid. However, B does not inherit the constructor A().
The A() constructor is executed because the constructor of B implicitly calls super() as its first statement.
Q: 9Which inheritance relationship is represented by class B extends A?
Option B
In Java, the extends keyword represents an inheritance relationship between two classes. When we write class B extends A, it means that B is an A. Therefore, this relationship is called an IS-A relationship.
On the other hand, a HAS-A relationship represents composition or aggregation, where one class contains an object of another class.
class Engine
{
}
class Car
{
Engine en = new Engine();
}
Q: 10What is the output?
class A
{
void show()
{
System.out.print("A");
}
}
class B extends A
{
void show()
{
System.out.print("B");
}
}
A obj=new B();
obj.show();
Option B
This is an example of Method Overriding and Dynamic Method Dispatch in Java. The superclass A defines a method named show(), and subclass B overrides the same method with its own implementation.
The important statement is A obj = new B(); Here, obj is a reference of type A, but the actual object created is of type B.
Therefore, when obj.show() is called, Java executes the overridden show() method belonging to the actual object, which is B.
| Expression | Actual Object | Method Executed | Output |
|---|---|---|---|
| A obj = new A(); | A | A.show() | A |
| A obj = new B(); | B | B.show() | B |
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.