Multiple choice general knowledge science & technology

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);

  1. 5

  2. 6

  3. 7

  4. 8

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The loop executes three iterations: item=1 adds 1 to sum (sum=1), item=2 adds 2 (sum=3), item=3 adds 3 (sum=6). When sum becomes 6, it exceeds 4 and the break statement exits the loop before checking the while condition.