ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
class C { static { System.out.print("init "); } }
// in main:
C[] a = new C[2];
Class<?> k = C.class;
System.out.print("x ");
new C();
  1. A. x init
  2. B. init x
  3. C. init init x
  4. D. init x init

Correct answer: A. x init

Creating an array of C and evaluating the class literal C.class do not trigger class initialisation; the first instance creation does.

More OOP with C++ and Java questions

Exams that ask this topic