Learn AP Comp Sci

Problem of the Day

Thursday, February 5, 2026


Problem:

Consider the following class declaration, which describes a solid polygon that can be "rolled" to get a random result for playing games of chance.

public class Die
{
private int result;
private int sides;

public Die()
{
sides = 6;
roll();
result = getResult();
}

public Die(int sides)
{
this.sides = sides;
roll();
result = getResult();
}

public void roll()
{
result = (int) (Math.random() * sides + 1);
}

public int getResult()
{
return result;
}
}

Which of these are considered accessor methods?

  1. public Die()
  2. public Die(int sides)
  3. public void roll()
  4. public int getResult()
  1. I only
  2. I and II only
  3. III only
  4. IV only
  5. III and IV only

Show solution:

The correct answer is d. An accessor method gets information from the object and returns it as a result. Because accessor methods often have the word "get" as part of their method name, they are sometimes referred to colloquially as "getters."