197k views
4 votes
Consider the following code segment.

int k = 35;
while (k >= 0)
{
System.out.println("X");
k -= 5;
}
How many times will the string "X" be printed as a result of executing the code segment?
A 1
B 7
C 8
D 35
E More than 35 times, because the code segment will cause an infinite loop.

1 Answer

3 votes

Final answer:

The string "X" will be printed 8 times.

Step-by-step explanation:

The code segment is a while loop that will continue to execute until the condition k >= 0 is no longer true. In each iteration of the loop, the string "X" is printed and the value of k is decreased by 5.

Starting with an initial value of 35, the loop will execute 8 times. The value of k will be decremented by 5 in each iteration as follows: 35, 30, 25, 20, 15, 10, 5, 0.

Therefore, the string "X" will be printed a total of 8 times.

User Aleya
by
7.2k points