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
b. 6
c. 7
d. 8
e. 9
b 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.
You might also like to view...
A field in a table is a collection of related data such as customer's contact information
Indicate whether the statement is true or false
A(n) ________ is a routing protocol that was designed and intended for use inside a single autonomous system
A) IGP B) EGP C) BGP D) IXR