ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
class T {
    int n;
    { n += 5; }
    T()      { this(1); n *= 2; }
    T(int k) { n += k; }
}
// in main:
System.out.print(new T().n);
  1. A. 12
  2. B. 22
  3. C. 7
  4. D. 11

Correct answer: A. 12

Instance initializers run once per object, inside the constructor that calls super() (here T(int)), before its body: n = 5, then + 1 = 6, then T()'s remaining body doubles it to 12.

More OOP with C++ and Java questions

Exams that ask this topic