Bird
Raised Fist0
Javaprogramming~20 mins

Constructor execution flow in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
πŸŽ–οΈ
Constructor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this constructor execution?

Consider the following Java code with inheritance and constructors. What will be printed when new Child() is executed?

Java
class Parent {
    Parent() {
        System.out.println("Parent constructor");
    }
}

class Child extends Parent {
    Child() {
        System.out.println("Child constructor");
    }
}

public class Test {
    public static void main(String[] args) {
        new Child();
    }
}
A
Parent constructor
Child constructor
B
Child constructor
Parent constructor
CChild constructor
DParent constructor
Attempts:
2 left
πŸ’‘ Hint

Remember that the parent constructor runs before the child constructor.

❓ Predict Output
intermediate
2:00remaining
What is the output when constructors call each other?

Analyze the constructor calls and determine the output.

Java
class A {
    A() {
        this(5);
        System.out.println("A no-arg");
    }
    A(int x) {
        System.out.println("A int: " + x);
    }
}

public class Test {
    public static void main(String[] args) {
        new A();
    }
}
A
A no-arg
A int: 5
B
A int: 5
A no-arg
CA int: 5
DCompilation error
Attempts:
2 left
πŸ’‘ Hint

Look at how this() calls another constructor before printing.

πŸ”§ Debug
advanced
2:00remaining
What error occurs in this constructor chaining?

Examine the code and identify the error that occurs when compiling.

Java
class B {
    B() {
        this();
        System.out.println("B no-arg");
    }
}

public class Test {
    public static void main(String[] args) {
        new B();
    }
}
ANullPointerException at runtime
BStackOverflowError at runtime
CNo output, program runs silently
DCompilation error: recursive constructor call
Attempts:
2 left
πŸ’‘ Hint

Check how this() is used inside the constructor.

❓ Predict Output
advanced
2:00remaining
What is the output of this multi-level constructor chain?

Given the classes below, what will be printed when new C() is executed?

Java
class A {
    A() {
        System.out.println("A constructor");
    }
}

class B extends A {
    B() {
        System.out.println("B constructor");
    }
}

class C extends B {
    C() {
        super();
        System.out.println("C constructor");
    }
}

public class Test {
    public static void main(String[] args) {
        new C();
    }
}
A
A constructor
B constructor
C constructor
B
B constructor
A constructor
C constructor
CCompilation error
D
A constructor
C constructor
B constructor
Attempts:
2 left
πŸ’‘ Hint

Remember the order of constructor calls in inheritance.

🧠 Conceptual
expert
2:00remaining
How many times is the constructor of class A called?

Consider the following code. How many times is the constructor of class A executed when new D() is called?

Java
class A {
    A() {
        System.out.println("A constructor");
    }
}

class B extends A {
    B() {
        System.out.println("B constructor");
    }
}

class C extends A {
    C() {
        System.out.println("C constructor");
    }
}

class D extends B {
    D() {
        super();
        System.out.println("D constructor");
    }
}

public class Test {
    public static void main(String[] args) {
        new D();
    }
}
A2
B3
C1
D0
Attempts:
2 left
πŸ’‘ Hint

Trace the inheritance chain and constructor calls carefully.

Practice

(1/5)
1. In Java, when you create an object of a child class, which constructor runs first?
easy
A. The parent class constructor
B. The child class constructor
C. Both constructors run simultaneously
D. No constructor runs automatically

Solution

  1. Step 1: Understand constructor call order

    In Java, when creating an object, the parent class constructor runs before the child class constructor.
  2. Step 2: Reason about object creation

    This ensures the parent part of the object is set up before the child adds its own setup.
  3. Final Answer:

    The parent class constructor -> Option A
  4. Quick Check:

    Parent constructor runs first [OK]
Hint: Parent constructor always runs before child constructor [OK]
Common Mistakes:
  • Thinking child constructor runs first
  • Believing constructors run simultaneously
  • Assuming constructors don't run automatically
2. Which of the following is the correct way to call a parent class constructor from a child class in Java?
easy
A. this();
B. parent();
C. super();
D. base();

Solution

  1. Step 1: Recall Java syntax for parent constructor call

    Java uses the keyword super() to call the parent class constructor explicitly.
  2. Step 2: Check other options

    parent(), this(), and base() are not valid Java syntax for this purpose.
  3. Final Answer:

    super(); -> Option C
  4. Quick Check:

    Use super() to call parent constructor [OK]
Hint: Use super() to call parent constructor explicitly [OK]
Common Mistakes:
  • Using this() instead of super()
  • Trying parent() or base() which don't exist
  • Omitting the call when needed
3. What is the output of the following Java code?
class Parent {
    Parent() {
        System.out.print("P");
    }
}
class Child extends Parent {
    Child() {
        System.out.print("C");
    }
}
public class Test {
    public static void main(String[] args) {
        new Child();
    }
}
medium
A. CP
B. PC
C. C
D. P

Solution

  1. Step 1: Identify constructor calls

    Creating new Child() calls the Child constructor, which implicitly calls the Parent constructor first.
  2. Step 2: Trace output order

    Parent constructor prints "P" first, then Child constructor prints "C".
  3. Final Answer:

    PC -> Option B
  4. Quick Check:

    Parent prints P, then Child prints C [OK]
Hint: Parent constructor output appears before child output [OK]
Common Mistakes:
  • Assuming child prints before parent
  • Ignoring implicit super() call
  • Expecting only one letter output
4. Consider this Java code snippet:
class A {
    A() {
        System.out.print("A");
    }
}
class B extends A {
    B() {
        System.out.print("B");
    }
}
class C extends B {
    C() {
        System.out.print("C");
    }
}
public class Main {
    public static void main(String[] args) {
        new C();
    }
}
What is the output, and if there is an error, how to fix it?
medium
A. ABC
B. Error: No default constructor in A
C. Error: Constructor call missing in B
D. BAC

Solution

  1. Step 1: Check constructor chaining

    Each class has a default constructor that implicitly calls its parent constructor.
  2. Step 2: Trace constructor calls

    Creating new C() calls C(), which calls B(), which calls A(), printing "A", then "B", then "C".
  3. Final Answer:

    ABC -> Option A
  4. Quick Check:

    Constructors chain up and print in order A-B-C [OK]
Hint: Default constructors chain automatically if no args [OK]
Common Mistakes:
  • Expecting error without explicit constructors
  • Thinking constructors don't chain automatically
  • Mixing output order
5. Given these classes:
class Vehicle {
    Vehicle() {
        System.out.print("V");
    }
}
class Car extends Vehicle {
    Car() {
        super();
        System.out.print("C");
    }
}
class SportsCar extends Car {
    SportsCar() {
        System.out.print("S");
    }
}
public class Demo {
    public static void main(String[] args) {
        new SportsCar();
    }
}
What is the output, and why does it appear in that order?
hard
A. CSV
B. VSC
C. SVC
D. VCS

Solution

  1. Step 1: Analyze constructor calls

    Creating new SportsCar() calls SportsCar(), which implicitly calls Car(), which calls Vehicle() via super().
  2. Step 2: Trace output sequence

    Vehicle constructor prints "V", then Car prints "C", then SportsCar prints "S".
  3. Final Answer:

    VCS -> Option D
  4. Quick Check:

    Parent to child constructor output order is V-C-S [OK]
Hint: Parent constructors run before child, print in that order [OK]
Common Mistakes:
  • Assuming SportsCar calls super() explicitly
  • Mixing output order
  • Forgetting implicit super() call in SportsCar