Sample questions
Question 1Easy
Which keyword does Java use to call a parent class's constructor or method?
- parent
- base
- this
- super
Answer: D. super
Explanation
super(...) calls the parent constructor and super.method() calls the parent's version of a method. C# uses base.
Step-by-step solution
- Java uses 'super' to call parent class methods or constructors.
- 'super()' is used for calling parent constructor in Java.
- 'super.method()' calls the parent's method version.
- Other keywords like 'parent' or 'base' are not valid in Java.
- The correct keyword is 'super'.
Why the other options are wrong
- A. parent is not a Java keyword; the parent is reached with super.
- B. base is the C# keyword for the parent class, not a Java keyword.
- C. this refers to the current object, not to the parent class.
Tip: Remember: 'super' is used in Java for parent class methods and constructors.
Question 2Easy
What is the output of this Java program?
class Base {
void show() { System.out.print("Base "); }
}
class Derived extends Base {
void show() {
super.show();
System.out.print("Derived");
}
}
public class Main {
public static void main(String[] args) { new Derived().show(); }
}
- Derived
- Derived Base
- Base
- Base Derived
Answer: D. Base Derived
Explanation
super.show() explicitly calls the parent's version first, then Derived prints its own text.
Step-by-step solution
- super.show() calls Base's show method, printing 'Base'.
- Then Derived's show method prints 'Derived' after.
- So output is 'Base Derived'.
- Final answer: Base Derived.
Why the other options are wrong
- A. The super.show() call runs first and prints Base, so the output must include Base as well as Derived.
- B. The order is reversed, because super.show() runs first and prints Base before Derived is printed.
- C. Base is printed by super.show(), and the Derived method then prints Derived, so Derived is missing here.
Tip: Use super.method() to ensure parent class methods are called before child class overrides.
Question 3Easy
Classes Car, Bike and Truck all inherit directly from Vehicle. This is called:
- Hierarchical inheritance
- Single inheritance
- Multilevel inheritance
- Multiple inheritance
Answer: A. Hierarchical inheritance
Explanation
Several subclasses sharing one direct parent form hierarchical inheritance.
Step-by-step solution
- Car, Bike and Truck all have Vehicle as their parent.
- Multiple children share the same parent in this case.
- This is called hierarchical inheritance.
- Hierarchical inheritance matches the correct answer.
- Final answer: Hierarchical inheritance.
Why the other options are wrong
- B. Single inheritance means one direct parent, but the question is about several subclasses of one parent.
- C. Multilevel inheritance is a chain, where a class inherits from a class that itself inherits.
- D. Multiple inheritance means one class with several direct parents, which Car, Bike and Truck do not have.
Tip: Remember: Multiple children of one parent = hierarchical inheritance.
Question 4Easy
Which relationship does inheritance model?
- PART-OF
- IS-A
- HAS-A
- USES-A
Answer: B. IS-A
Explanation
A Dog IS-A Animal. HAS-A relationships (a Car has an Engine) are modelled with composition.
Step-by-step solution
- Inheritance shows a type relationship between classes.
- A Dog inherits properties from Animal, so it is a type of Animal.
- This is called IS-A relationship.
- HAS-A relationships use composition, not inheritance.
- The correct answer is IS-A.
Why the other options are wrong
- A. PART-OF describes composition or aggregation, where a whole holds parts, and it is not modelled by inheritance.
- C. HAS-A describes composition, where an object holds another object, and it is not the relationship inheritance models.
- D. USES-A describes a dependency between classes, not the type relationship that inheritance creates.
Tip: Remember: IS-A for inheritance, HAS-A for composition.
Question 5Medium
A Java class is declared final. What does this mean?
- All its methods are static
- It cannot be instantiated
- It cannot be subclassed
- Its fields cannot change
Answer: C. It cannot be subclassed
Explanation
final on a class prevents inheritance (String is an example); it doesn't make the fields immutable by itself.
Step-by-step solution
- Final on a class stops other classes from extending it.
- It does not change method behaviour or field values; those are controlled by other modifiers.
- So index 2, it cannot be subclassed, is correct.
Why the other options are wrong
- A. Static is about methods, not the final keyword on a class.
- B. Instances can still be created from a final class unless it has a private constructor.
- D. Field values are controlled by a field's own final modifier, not the class's.
Tip: Final on a class blocks inheritance; final on a method blocks overriding; final on a field blocks reassignment.
Question 6Medium
What is the output of this C++ program?
#include <iostream>
struct A { A() { std::cout << "A"; } };
struct B { B() { std::cout << "B"; } };
struct C : B, A {
C() { std::cout << "C"; }
};
int main() {
C c;
std::cout << "\n";
}
- ABC
- BAC
- CBA
- CAB
Answer: B. BAC
Explanation
Base classes are constructed in the order they are listed in the class declaration (B then A), before the derived constructor body.
Step-by-step solution
- Base classes are built in the order they are listed: B first, then A.
- B prints "B", then A prints "A".
- Then the body of C prints "C"; the output is "BAC", index 1.
Why the other options are wrong
- A. A is not built first; the bases follow the listed order B, A.
- C. CBA would need the body before the bases, but bases are built first.
- D. CAB puts the body first and uses the wrong base order.
Tip: Base classes are constructed in the order they are listed, then the derived body runs.
Question 7Medium
What is the main risk known as the 'fragile base class' problem?
- The base class cannot be compiled separately
- Base classes are slower to instantiate
- Subclasses can no longer override any of the methods that the base class declares as public
- Changes to a base class can silently break subclasses that depend on its internals
Answer: D. Changes to a base class can silently break subclasses that depend on its internals
Explanation
Subclasses are tightly coupled to their parent's implementation, so a seemingly safe change in the parent can alter behaviour in every subclass.
Step-by-step solution
- Changes in base class affect subclasses silently.
- Subclasses rely on internal workings of base class.
- Modifying base class internals breaks subclasses unexpectedly.
- This is called 'fragile base class' problem.
- Final answer: Changes to a base class can silently break subclasses that depend on its internals.
Why the other options are wrong
- A. A base class can be compiled separately; the problem is changes that break subclasses.
- B. Instantiation speed is not what fragile base class means.
- C. Overriding is not blocked by a fragile base class; the trouble is hidden dependence on internals.
Tip: Avoid changing internal details of base classes if subclasses depend on them.
Question 8Medium
What does this Python code print?
class Base:
def __init__(self):
self.x = 1
class Child(Base):
def __init__(self):
self.y = 2
c = Child()
print(hasattr(c, "x"), hasattr(c, "y"))
- True False
- True True
- False False
- False True
Answer: D. False True
Explanation
Child overrides __init__ without calling super().__init__(), so Base's initialisation never runs and x is never set.
Step-by-step solution
Child.__init__ does not call super().__init__(), so x is never set.hasattr(c, "x") is False.y is set in Child.__init__, so hasattr(c, "y") is True; the output is "False True", index 3.
Why the other options are wrong
- A.
x is only set by Base.__init__, which never runs here. - B.
x is missing, so the first result is False. - C.
y is set directly in Child.__init__, so the second result is True.
Tip: A subclass __init__ that skips super().__init__() leaves the parent's attributes unset.
Question 9Hard
In C++, how is the diamond problem (B and C both inherit from A, and D inherits from both B and C) resolved so D has only one A subobject?
- It cannot be resolved in C++
- By declaring D as abstract so it never contains an A subobject
- By making A's methods static
- By using virtual inheritance for B and C
Answer: D. By using virtual inheritance for B and C
Explanation
class B: virtual public A and class C: virtual public A make B and C share a single A base inside D.
Step-by-step solution
- Virtual inheritance ensures B and C share the same A base class.
- This prevents multiple copies of A in D's hierarchy.
- D inherits A once through both B and C due to virtual inheritance.
- Virtual inheritance resolves the diamond problem correctly.
- Only option D uses virtual inheritance for B and C.
- Final answer: By using virtual inheritance for B and C.
Why the other options are wrong
- A. C++ resolves the diamond problem with virtual inheritance, so the claim that it cannot be resolved is false.
- B. Making D abstract does not remove the A subobject from D's hierarchy, so it does not solve the duplicate base.
- C. Making A's methods static does not remove the duplicate A subobject, because the problem is the data inherited twice.
Tip: Use virtual inheritance when a class inherits from multiple classes with a common base to avoid the diamond problem.
Question 10Hard
What is the output of this C++ program?
#include <iostream>
struct Base {
void f(int) { std::cout << "Base::f(int)\n"; }
};
struct Derived : Base {
void f(double) { std::cout << "Derived::f(double)\n"; }
};
int main() {
Derived d;
d.f(5);
}
Derived::f(double)- Compilation error: ambiguous call
- Runtime error
Base::f(int)
Answer: A. Derived::f(double)
Explanation
Declaring f in Derived hides every Base::f overload. Name lookup stops at Derived, and 5 converts to double. A using Base::f; declaration would bring the int version back.
Step-by-step solution
- d.f(5) calls f with int argument 5.
- Derived has f(double), which matches the argument type double.
- Base's f(int) is hidden by Derived's f(double).
- No using declaration brings Base::f(int) back.
- So output is
Derived::f(double).
Why the other options are wrong
- B. There is no ambiguity, because Derived::f hides Base::f by name, so Base::f is never a candidate for the call.
- C. The call is resolved at compile time and the program runs normally, so there is no runtime error.
- D. Derived's f hides Base's f by name, so Base::f(int) is never considered and this output cannot happen.
Tip: In C++, derived class overloads hide base class functions unless explicitly brought into scope.
Practise all 28 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →