ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
class A { static int v = 1; static int get() { return v; } }
class B extends A { static int v = 2; }
// in main:
B.v++;
A.v += 10;
System.out.print(B.get() + " " + B.v + " " + A.v);
  1. A. 11 3 11
  2. B. 3 3 11
  3. C. 12 2 11
  4. D. 11 3 12

Correct answer: A. 11 3 11

B declares its own static v that HIDES A.v, so B.v++ makes B.v = 3 while A.v becomes 11. B.get() is A's static method and reads A.v, i.e. 11.

More OOP with C++ and Java questions

Exams that ask this topic