What is sum after the following loop terminates?

```
int sum = 0;
int item = 0;
do {
item++;
sum += item;
if (sum > 4)
break;
}
while (item < 5);
```
a. 5
V
c. 7
d. 8
e. 9

b. 6
sum and item are initialized to 0 before the loop. In the loop, item++ increments item by 1 and item is then added to sum. If sum > 4, the break statement exits the loop. item is initially 0, then 1, 2, 3 and sum is initially 0, and then 1, 3, and 6. When sum is 6, (sum > 4) is true, which causes the break statement to be executed. So, the correct answer is B.

Computer Science & Information Technology

You might also like to view...

Another name for a flash drive is a thumb drive

Indicate whether the statement is true or false

Computer Science & Information Technology

Sizing ________ display on a selected object that enable you to resize an image

Fill in the blank(s) with correct word

Computer Science & Information Technology