ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
class T {
    static { System.out.print("S"); }
    { System.out.print("I"); }
    T() { System.out.print("C"); }
}
// in main:
new T();
new T();
  1. A. ICSIC
  2. B. SCIC
  3. C. SICIC
  4. D. SICSIC

Correct answer: C. SICIC

The static block runs once, when the class is first initialised. For every object the instance initialiser block runs before the constructor body. So S, then IC twice.

More OOP with C++ and Java questions

Exams that ask this topic