24.9k views
2 votes
What does this method do?

void mystery()
{
while( !karel.nextToABeeper() )
{
karel.pickBeeper();
}
}

A) Picks one beeper.
B) Clears the corner i.e. picks up all beepers.
C) Errors out.
D) Does nothing.

User Aroc
by
8.3k points

1 Answer

5 votes

Final answer:

The method has a logical error and does nothing because it tries to pick a beeper where there is none, due to the condition of the while loop.

Step-by-step explanation:

The method in question contains a loop that continues to execute as long as a certain condition is met. This condition is that the robotic object karel is not next to a beeper. Inside the loop, there is an action for karel to pick up a beeper. However, due to the condition checked in the while loop, this method has a logical error. The method will enter the loop only if karel is not next to a beeper, but the first action inside the loop is to pick up a beeper, which cannot be executed if a beeper is not present. Therefore, the correct answer is:

D) Does nothing.

Because the robot will enter the loop only if it's not next to a beeper, yet it immediately tries to pick up a beeper, which it cannot do as per the while loop's condition. Thus, the method will not execute any code and will not pick up any beepers.

User Sean Holden
by
8.4k points