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: 1If 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: 2In 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: 3Which keyword is used in Java to inherit a class?
Option B
To inherit a class, Java provides the extends keyword, which indicates that the subclass will acquire all non-private (private members are never inherited) fields and methods of the superclass.
Q: 4What will be the output of the following code?
class Vehicle {
public void move() {
System.out.println("Vehicle is moving");
}
}
class Car extends Vehicle {
public void move() {
System.out.println("Car is moving");
}
}
public class Main {
public static void main(String[] args) {
Vehicle v = new Car();
v.move();
}
}
Option D
When a class inherits another class using the extends keyword, it can reuse or modify the methods of the parent class. This concept is called inheritance.
Java also supports Method Overriding, which allows a subclass to provide its own implementation of a method defined in the parent class.
When a method is overridden, the version of the method that belongs to the actual object (not the reference type) is called at runtime, this is known as Runtime Polymorphism.
In the given code, the Vehicle class has a method move() that prints "Vehicle is moving". The Car class extends Vehicle and overrides the move() method to print "Car is moving".
In the main method, a reference of type Vehicle is used to create an object of type Car: Vehicle v = new Car();. When v.move() is called, Java checks the actual object type at runtime, sees that it is a Car, and executes the overridden move() method in the Car class. Therefore, the program prints "Car is moving".
You have reached the end of this topic. Continue learning with the next topic below.
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.