What is sum after the following loop terminates?
```
int sum = 0;
int item = 0;
do {
item++;
if (sum >= 4)
continue;
sum += item;
}
while (item < 5);
```
a. 6
b. 7
c. 8
d. 9
e. 10
a. 6
In this loop, when sum >= 4, the continue statement is executed to exit the current iteration so the next statement sum += item after the if statement will not be executed. The loop keeps adding item to sum for item 1, 2, 3, and so on. So sum is 1, 2, and 6. When sum is 6, sum >=4 is true, the continue statement is executed to skip the rest of the iteration. When the loop ends, sum is still 6. So, the correct answer is A.
You might also like to view...
Given the Java statement which of the following statements is false?
int sum = number1 + number2; a. It’s an assignment statement. b. It calculates the sum of variables number1 and number2. c. The operands of the addition operator are number1 and number2. d. It assigns the value of number1 to sum.
The ________ Amendment to the U.S. Constitution guarantees the right of the accused to be confronted with the witnesses against him
Fill in the blank(s) with correct word
Host (A) records are used to provide a static mapping of IP addresses to hostnames within a reverse lookup zone
Indicate whether the statement is true or false
Which of the following formulas is not entered correctly?
A. =10+50 B. =B7+14 C. 10+50 D. =B7*B1