Learn AP Comp Sci

Problem of the Day

Tuesday, February 17, 2026


Problem:

The class DefaultDemo is shown here.

public class DefaultDemo
{
// instance variables
private int p;
private String q;

public DefaultDemo()
{
// instance variables use default values
// from declaration above
System.out.println(p + "," + q);
}

public void aMethod()
{
// local variables
int r;
String s;
// local variables use default values
// from declaration above
System.out.println(r + "," + s);
}
}

What compilation issues(s), if any, are present in this code?

  1. Variables p, q, r, and s haven't been initialized
  2. Variables p and q haven't been initialized
  3. Variables r and s haven't been initialized
  4. There are no compilation errors in this code.

Show solution:

The correct answer is c. Instance variables such as p and q in this example, have default values assigned if they aren't explicitly initialized with another value. Local variables such as r and s require explicit initialization before they can be used. When this code is compiled, those variables cause a variable might not have been initialized error.