0
0
Javaprogramming~20 mins

Super keyword in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Super Keyword Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of super keyword in constructor call
What is the output of this Java program that uses super to call the parent class constructor?
Java
class Parent {
    Parent() {
        System.out.println("Parent constructor");
    }
}

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

public class Test {
    public static void main(String[] args) {
        new Child();
    }
}
AChild constructor
BChild constructor\nParent constructor
CParent constructor
DParent constructor\nChild constructor
Attempts:
2 left
πŸ’‘ Hint
Remember that super() calls the parent constructor before the child constructor body runs.
❓ Predict Output
intermediate
2:00remaining
Using super to access parent method
What is the output of this Java program that uses super to call a parent class method?
Java
class Animal {
    void sound() {
        System.out.println("Animal sound");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
    void printSounds() {
        sound();
        super.sound();
    }
}

public class Test {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.printSounds();
    }
}
AAnimal sound\nDog barks
BDog barks\nAnimal sound
CDog barks\nDog barks
DAnimal sound\nAnimal sound
Attempts:
2 left
πŸ’‘ Hint
The super keyword calls the parent class method, while calling the method normally calls the child class version.
❓ Predict Output
advanced
2:00remaining
Output when super keyword used with variables
What is the output of this Java program that uses super to access a parent class variable?
Java
class Base {
    int x = 10;
}

class Derived extends Base {
    int x = 20;
    void printX() {
        System.out.println(x);
        System.out.println(super.x);
    }
}

public class Test {
    public static void main(String[] args) {
        Derived d = new Derived();
        d.printX();
    }
}
A20\n10
B10\n20
C10\n10
D20\n20
Attempts:
2 left
πŸ’‘ Hint
The variable x in Derived hides the one in Base. Use super.x to access the parent's variable.
❓ Predict Output
advanced
2:00remaining
Output of super in multi-level inheritance
What is the output of this Java program using super in a multi-level inheritance chain?
Java
class A {
    void show() {
        System.out.println("A show");
    }
}

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

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

public class Test {
    public static void main(String[] args) {
        C c = new C();
        c.show();
    }
}
AB show\nC show
BA show\nC show
CC show\nB show
DA show\nB show\nC show
Attempts:
2 left
πŸ’‘ Hint
In class C, super.show() calls the immediate parent class B's show() method.
🧠 Conceptual
expert
2:00remaining
Error caused by incorrect use of super
Which option will cause a compile-time error due to incorrect use of super in Java?
AUsing <code>super</code> to call a parent class method from a child instance method
BUsing <code>super</code> to access a parent class variable when it is hidden by child variable
CUsing <code>super()</code> inside a static method
DUsing <code>super()</code> as the first statement in a child constructor
Attempts:
2 left
πŸ’‘ Hint
Remember that super relates to instance context, not static context.