Multiple choice technology programming languages

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

  1. Exception

  2. Compiler Error

  3. p = 0

  4. p=10

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

The parameterized constructor calls this() to invoke the no-arg constructor, but places this() after the assignment 'this.p = p'. In Java, constructor chaining with this() must be the FIRST statement in a constructor. Placing it after other code causes a compilation error.