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();- A. starting anon-moving
- B. Compilation error: cannot instantiate abstract class Vehicle
- C. starting
- 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
- What is printed? cpp class Base { public: Base(int x) { cout << "Base(" << x << ") "; } }; class Derived : public Base { public: Derived()…
- What is printed? java class Outer { static class Nested { void show() { System.out.print("Nested.show"); } } } // in main: Outer.Nested n…
- What is printed? java interface Shape { double area(); static Shape unitSquare() { return () -> 1.0; } } // in main: Shape s =…
- What is printed? java class Shape { protected String name = "Shape"; Shape() { System.out.print(name + " "); } } class Circle extends…
- What is printed? cpp class Account { double balance; public: explicit Account(double b) : balance(b) {} double get() const { return…
- What is printed? java class Base { protected Object clone2() { return new Base(); } } class Sub extends Base { @Override public Object…