AP Computer Science A · Topic 9.4
super Keyword Practice
Part of Inheritance.
Practice questions
12
Sample questions
5 of 12 — sign in to practice the rest with adaptive difficulty and mastery tracking.
Sample 1difficulty 3/5
<code>super(arg)</code> calls
- A
Compile error
- B
A static method
- C
The current class's other constructor
- Dcheck_circle
The parent class constructor
Why
Use <code>super(args)</code> as first statement of subclass constructor.
- A
Sample 2difficulty 3/5
public class A { public String hello() { return "Hi from A"; } } public class B extends A { public String hello() { return super.hello() + " and B"; } } System.out.println(new B().hello());What is printed?
- A
Stack overflow error
- B
and B
- Ccheck_circle
Hi from A and B
- D
Hi from A
Why
<code>super.hello()</code> calls A's version, returning "Hi from A". Then " and B" is appended.
- A
Sample 3difficulty 3/5
public class A { public String describe() { return "A"; } } public class B extends A { public String describe() { return super.describe() + "B"; } } public class C extends B { public String describe() { return super.describe() + "C"; } } // Usage: A obj = new C(); System.out.print(obj.describe());What is printed?
- A
AC
- B
C
- Ccheck_circle
ABC
- D
CBA
Why
C calls super.describe (B's), which calls super.describe (A's = "A"), then appends "B" → "AB", then appends "C" → "ABC".
- A
Sample 4difficulty 3/5
public class A { public String label() { return "A"; } public String tag() { return label(); } } public class B extends A { public String label() { return "B"; } } // Usage: B b = new B(); System.out.print(b.tag());What is printed?
- A
Compile error: tag missing in B
- B
AB
- C
A
- Dcheck_circle
B
Why
tag() is inherited and calls label() (this.label()). Dynamic dispatch invokes B's label(), printing "B".
- A
Sample 5difficulty 3/5
public class A { public String greet() { return "hello"; } } public class B extends A { public String greet() { return super.greet() + " world"; } } // Usage: B b = new B(); System.out.print(b.greet());What is printed?
- A
world
- B
hello hello world
- Ccheck_circle
hello world
- D
Stack overflow
Why
super.greet() calls A's greet returning "hello". B's greet appends " world", so "hello world" is printed.
- A