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);
Answer
Output
a:13 b:133
Explanation

The values of a and b are evaluated as follows in the for loop:

IterationabRemarks
  100Initial value
110110b = b + a = 100 + 10 = 110
211121b = b + a = 110 + 11 = 121
312110b = b + a = 121 + 12 = 133, Loop terminates

The print statements print the final values of a and b.