Learn AP Comp Sci

Problem of the Day

Thursday, May 21, 2026


Problem:

The MovingDot class models a point on an x-y coordinate system that can change each turn. A moving dot that starts at the origin, for example, would have initial coordinates x = 0; y = 0;

After a turn where x += 1; y+= 1; the particle will have moved up and to the right.

For the particle described by the code here, what is the pattern of the dot's motion?

int x = 0;
int y = 0;
int x_step = 2;
int y_step = 1;
for (int turn = 0; turn < 5; turn++)
{
x = x + x_step; y = y + y_step;
// particle is now at position x, y
x_step = x_step * -1;
}
  1. back-and-forth upwards
  2. back-and-forth to the right
  3. a rectangular shape
  4. a straight line to the right

Show solution:

The correct answer is a. Tracing through the loop a few times reveals the following coordinates for the dot:

0, 0
2, 1
0, 2
2, 3
0, 4
2, 5

The y position of the dot is ascending steadily upwards, while the x-component goes back and forth between 0 and 2.