ProgrammingHardProduct-company placement · Java

What happens when this class is compiled?

What happens when this class is compiled?
class A {
    A()      { this(1); }
    A(int x) { this(); }
}
  1. A. It compiles; new A() throws StackOverflowError
  2. B. It compiles; new A() creates an object normally
  3. C. Compile-time error: recursive constructor invocation
  4. D. Compile-time error: this() must be the last statement

Correct answer: C. Compile-time error: recursive constructor invocation

The compiler checks this(...) chains and rejects a cycle among constructors, so the class does not compile at all.

More OOP with C++ and Java questions

Exams that ask this topic