Multiple choice technology programming languages

package simplejava; public class TestConstructor2 { int p; TestConstructor2() { System.out.print(" I am in Constructor"); } TestConstructor2(int p) { this(); this.p = p; System.out.print(" p = " + p); } public static void main(String[] args) { TestConstructor2 tp1 = new TestConstructor2(10); } }

  1. I am in Constructor p = 10

  2. I am in Constructor p = 0

  3. Compiler Error

  4. Exception

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

When TestConstructor2(10) is called, it first executes this() which calls the no-arg constructor, printing 'I am in Constructor'. Then execution returns to complete the parameterized constructor, which sets p=10 and prints 'p = 10'. The output combines both print statements in order.

AI explanation

The parameterized constructor TestConstructor2(int p) first calls this(), invoking the no-arg constructor, which prints " I am in Constructor". Control then returns to the parameterized constructor, which sets this.p = p (p=10) and prints " p = 10". Concatenated, the output is "I am in Constructor p = 10", matching the correct answer. "p = 0" is wrong because p is explicitly set to 10 before printing. "Compiler Error" is wrong — chaining constructors with this() as the first statement is valid Java. "Exception" is wrong since nothing here can throw at runtime; it's straightforward constructor chaining and print statements.