Bird
0
0

Find the problem in this code:

medium📝 Debug Q7 of 15
Java - Constructors
Find the problem in this code:
class A {
  A() { System.out.print("A"); }
}
class B extends A {
  B() { this(); System.out.print("B"); }
}
AConstructor B should be static
BMissing super() call in B constructor
CNo problem, code compiles and runs
DRecursive constructor call causes compile error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze constructor call in B

    B's constructor calls this(), which calls itself recursively without end.
  2. Step 2: Understand compiler behavior

    Recursive constructor calls cause compile-time error due to infinite recursion.
  3. Final Answer:

    Recursive constructor call causes compile error -> Option D
  4. Quick Check:

    Constructor recursion = compile error [OK]
Quick Trick: Avoid recursive constructor calls with this() [OK]
Common Mistakes:
  • Thinking missing super() causes error here
  • Assuming code runs fine
  • Confusing this() with super()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes