ICSE Class 10 Computer Applications
Question 27 of 30
Solved Sample Paper 4 — Question 27
Back to all questions 27
Question Question 2(vii)
Find the output of the given code.
int a, b = 100;
for (a = 10; a <= 12; a++)
{
b += a;
}
System.out.print("a:" + a + " " + "b:" + b);Output
a:13 b:133
Explanation
The values of a and b are evaluated as follows in the for loop:
| Iteration | a | b | Remarks |
|---|---|---|---|
| 100 | Initial value | ||
| 1 | 10 | 110 | b = b + a = 100 + 10 = 110 |
| 2 | 11 | 121 | b = b + a = 110 + 11 = 121 |
| 3 | 12 | 110 | b = b + a = 121 + 12 = 133, Loop terminates |
The print statements print the final values of a and b.