0
0
Javaprogramming~10 mins

Constructor execution flow in Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to call the parent class constructor from the child class.

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

class Child extends Parent {
    Child() {
        [1];
        System.out.println("Child constructor");
    }
}
Drag options to blanks, or click blank then click option'
Asuper()
Bthis()
CParent()
Dparent()
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using this() instead of super()
Calling Parent() like a method
Forgetting to call the parent constructor
2fill in blank
medium

Complete the code to call another constructor in the same class.

Java
class Example {
    Example() {
        this(10);
        System.out.println("Default constructor");
    }

    Example(int x) {
        System.out.println("Constructor with value: " + [1]);
    }
}
Drag options to blanks, or click blank then click option'
A10
Bthis
Cx
Dvalue
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using this instead of the parameter name
Using the literal 10 instead of the parameter
Using an undefined variable
3fill in blank
hard

Fix the error in the constructor chaining code.

Java
class Test {
    Test() {
        [1];
        System.out.println("No-arg constructor");
    }

    Test(int a) {
        System.out.println("Constructor with int: " + a);
    }
}
Drag options to blanks, or click blank then click option'
Athis(5)
Bthis()
CTest(5)
Dsuper(5)
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using super(5) when no parent constructor with int exists
Calling Test(5) like a method
Using this() which calls no-arg constructor causing recursion
4fill in blank
hard

Fill both blanks to complete the constructor chaining and print the correct messages.

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

class Derived extends Base {
    Derived() {
        [1];
        System.out.println("Derived constructor");
    }

    Derived(int x) {
        System.out.println("Derived with int: " + [2]);
    }
}
Drag options to blanks, or click blank then click option'
Athis(5)
Bsuper()
Cx
D5
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using super() instead of this(5) in the no-arg constructor
Printing the literal 5 instead of the parameter x
Not chaining constructors properly
5fill in blank
hard

Fill all three blanks to create a class with constructor chaining and parent constructor call.

Java
class Alpha {
    Alpha() {
        System.out.println("Alpha default");
    }

    Alpha(String msg) {
        System.out.println(msg);
    }
}

class Beta extends Alpha {
    Beta() {
        [1];
        System.out.println("Beta default");
    }

    Beta(String msg) {
        [2];
        System.out.println("Beta with message: " + [3]);
    }
}
Drag options to blanks, or click blank then click option'
Athis("Hello")
Bsuper(msg)
Cmsg
Dsuper()
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Calling super() instead of this() in the no-arg constructor
Not calling super(msg) in the string constructor
Printing a variable not defined or the wrong variable