ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
class P { static int v = 5; static { System.out.print("P"); } }
class C extends P { static { System.out.print("C"); } }
// in main:
System.out.print(C.v);
  1. A. P5
  2. B. PC5
  3. C. CP5
  4. D. 5

Correct answer: A. P5

A static field reference initialises only the class that DECLARES the field. v belongs to P, so C's static block never runs even though the code says C.v.

More OOP with C++ and Java questions

Exams that ask this topic