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?
public Die()public Die(int sides)public void roll()public int getResult()
- I only
- I and II only
- III only
- IV only
- III and IV only
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."