Question 1031
int x = 100;
int count = 0;
while (x > 1)
{
if (x % 3 == 0)
{
x = x / 3;
}
else
{
x = x - 1;
}
count++;
}
System.out.println(count);
What is printed when the code segment executes?Main Solution
Solution:
Answer: B. Each pass either divides by 3 (when a multiple of 3) or subtracts 1. Trace: 100 → 99 (subtract; not divisible by 3), 99 → 33, 33 → 11, 11 → 10, 10 → 9, 9 → 3, 3 → 1. That is seven transitions, so the loop exits with count = 7.
Answer: Correct: B
Answer images

