Computer Applications
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);
Java
Java Iterative Stmts
7 Likes
Answer
a:13 b:133
Working
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.
Answered By
4 Likes
Related Questions
State the type of errors, if any in the following statements.
(a)
switch (x < 2)
(b)
int a = 100, b = 0; System.out.println (a / b);
What will be the output of the following code?
int num = 10; if (num < 20) System.out.print(num++); else System.out.print(--num);
The following code has some error(s). Identify the errors and write the corrected code.
Int x = 4, y = 8; { y = y + (x++) } while (x <= 10) System.out.println(y);
State the method that determines, if the specified character is an uppercase character.