Nested Summation
A program uses nested loops where the inner loop control variable depends on the outer loop control variable to accumulate values.
What is printed when the following Java program is compiled and run?
public class QuestionTrace {
public static void main(String[] args) {
int res = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < i; j++) {
res += i + j;
}
}
System.out.print(res);
}
}
A
6
B
12
C
9
D
3
Question Leaderboard
Not enough data yet to show leaderboard.
