Multiple choice technology programming languages

package constructors; class P { P(String a) { System.out.println("I am in P "+a); } } class Q extends P { Q() { System.out.println("I am in Q"); } } public class TestConstructor2 { Q q = new Q(); }

  1. I am in P null I am in Q

  2. Compiler Error

  3. I am in P I am in Q

  4. I am in Q

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

Class Q extends P, and P has only a parameterized constructor P(String a). When Q() is called, it needs to call super() implicitly, but class P has no no-argument constructor. The compiler error occurs because the default constructor in Q attempts to call a nonexistent no-argument constructor in P. To fix this, Q() must explicitly call super(String) with an argument, or P needs to provide a no-argument constructor. This tests understanding that constructors aren't inherited and that subclass constructors must match available parent constructors.