ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
abstract class Vehicle {
    abstract void move();
    void start() { System.out.print("starting "); move(); }
}
// in main:
Vehicle v = new Vehicle() {
    @Override
    void move() { System.out.print("anon-moving"); }
};
v.start();
  1. A. starting anon-moving
  2. B. Compilation error: cannot instantiate abstract class Vehicle
  3. C. starting
  4. D. anon-moving starting

Correct answer: A. starting anon-moving

An anonymous class expression `new Vehicle() { ... }` creates a concrete subclass on the fly that supplies the missing move() implementation, so instantiation is legal. start() calls move() virtually, dispatching to the anonymous subclass's override: "starting anon-moving".

More OOP with C++ and Java questions

Exams that ask this topic