ProgrammingHardTCS NQT Digital · Accenture Advanced

What is printed?

What is printed?
class A {} class B extends A {}
static void f(A a) { System.out.print("A"); }
static void f(B b) { System.out.print("B"); }
// main:
A x = new B();
f(x); f(new B()); f((A) new B());
  1. A. BBB
  2. B. ABB
  3. C. ABA
  4. D. AAA

Correct answer: C. ABA

Overload resolution uses compile-time types: x is an A, new B() is a B, and the cast makes the third argument an A.

More OOP with C++ and Java questions

Exams that ask this topic