Learn AP Comp Sci

Problem of the Day

Monday, March 16, 2026


Problem:

The complete listing of the superclass Alpha is given here.

public class Alpha
{
private String a;

public Alpha()
{
a = "a";
}

public String getValue()
{
return a;
}
}

The complete listing of the subclass Beta which inherits from Alpha is given here.

public class Beta extends Alpha
{
private String a;

public Beta()
{
a = "b"
}

public String getValue()
{
return a;
}
}

What is the result of executing the following instructions?

Beta b = new Beta();
System.out.println(b.getValue());
  1. The String "a" is printed
  2. The String "b" is printed
  3. The code compiles but there is a run-time error due to a name collision of the variable a
  4. The code won't compile

(Note: This question includes topics that are not currently part of the AP Computer Science A core curriculum.)

Show solution:

The correct answer is b. Here, the getValue() method of the subclass overrides the method of the same name in the superclass. Objects of the class Beta will display the results of their own private variable a.