Difficulty: Easy
Correct Answer: Compilation fails.
Explanation:
Introduction / Context:
The example validates understanding of constructor chaining in Java. If a superclass defines no no-argument constructor, every subclass constructor must explicitly invoke one of the existing superclass constructors using super(...). Otherwise, the compiler inserts an implicit super() that does not exist, causing a compile-time error.
Given Data / Assumptions:
Concept / Approach:
Because Super lacks a no-arg constructor, the implicit super() in Sub’s constructor is invalid. The compiler emits an error such as "constructor Super in class Super cannot be applied to given types". Therefore, the code does not compile, and no runtime behavior is observed.
Step-by-Step Solution:
Verification / Alternative check:
Fix by writing Sub(String text) { super(text); i = 2; } which compiles and prints 2.
Why Other Options Are Wrong:
Common Pitfalls:
Forgetting to chain to an available superclass constructor; assuming a hidden no-arg constructor exists even when another constructor is declared explicitly.
Final Answer:
Compilation fails.
Discussion & Comments