Sentinel Summation
A program calculates a running sum by subtracting 2 from the value in each iteration until the value is no longer greater than 0.
What is printed when the following Java program is compiled and run?
public class QuestionTrace {
public static void main(String[] args) {
int val = 5;
int sum = 0;
while (val > 0) {
sum += val;
val -= 2;
}
System.out.println(sum);
}
}
A
6
B
5
C
12
D
9
Question Leaderboard
Not enough data yet to show leaderboard.
