ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
class Animal {
void makeSound() { System.out.print("..."); }
}
class Dog extends Animal {
@Override
void makeSound() { System.out.print("Woof"); }
}
class Puppy extends Dog {
@Override
void makeSound() { super.super.makeSound(); }
}What happens when compiling Puppy?- A. It compiles and Puppy.makeSound() calls Animal's version directly
- B. Compilation error: Java has no `super.super` syntax to skip an intermediate class
- C. It compiles and calls Dog's version
- D. It compiles only if Dog is declared abstract
Correct answer: B. Compilation error: Java has no `super.super` syntax to skip an intermediate class
Unlike some languages, Java provides no mechanism to reach a grandparent's method directly — `super.super.method()` is simply invalid syntax and fails to compile; you can only call the immediate super's version with `super.method()`.
More OOP with C++ and Java questions
- What is printed? java class Base { Base() { System.out.print("Base "); } } class Mid extends Base { Mid() { System.out.print("Mid "); } }…
- What is printed? cpp struct Base { virtual void render() const { cout << "Base::render "; } }; struct Derived : Base { void render() const…
- What is printed? java class Shape { double scale = 1.0; double area() { return 0; } } class Circle extends Shape { double r; Circle(double…
- What is printed? java class Base { protected int code() { return 1; } } class Mid extends Base { @Override protected int code() { return…
- What is printed? cpp struct Logger { static int instances; Logger() { instances++; } ~Logger() { instances--; } }; int Logger::instances =…
- What is printed? java interface Taxed { default double afterTax(double amt) { return amt 0.9; } } class Product implements Taxed { double…