Learn AP Comp Sci

Problem of the Day

Monday, January 12, 2026


Problem:

The String Array students holds the names of a series of students who are seated in order in a single line in a classroom.

String[] students = {"Aaron", "Bobby", "Caroline", "Darlene"};

... for example, represents four students seated left-to-right in the classroom.

What does the following code segment do?

while (true)
{
int j = (int) (Math.random() * students.length);
System.out.println("Work with " + students[j]);
}
  1. Chooses a random student from the list each time, possibly selecting the same student twice in a row
  2. Chooses a random student from the list each time, with no student selected twice before all students have been selected once
  3. Repeatedly goes through the list of students from left-to-right
  4. Repeatedly goes through the list of students from right-to-left

Show solution:

The correct answer is a. A random index is chosen every time in the loop, leaving open the possibility that the teacher make work with the same student two, three, five times in a row. Over time, it is expected that each student would ultimately have an equal amount of time with the teacher.