ITEMS ACCURATE MARKING
◉ What are the values of i and j after the following code fragment
runs?
int i = 60;
int j = 50;
int count = 0;
while (count < 5)
{
i = i + i;
i = i + 1;
j = j - 1;
j = j - j;
count++;
}
System.out.println("i=" + i + ", j=" + j);
a) i = 1951, j = 0
b) i = 1951, j = 45
,c) i = 65, j = 1
d) i = 65, j = 45. Answer: Answer: a) i = 1951, j = 0
◉ 9) Which error type does the "off-by-one" error belong to?
a) Syntax error
b) Compile-time error
c) Run-time error
d) Infinite loop. Answer: Answer: c) Run-time error
◉ 10) How many times does the following code fragment display
"Hi"?
int i = 10;
while (i >= 0)
{
System.out.println("Hi");
i--;
}
a) 9 times
b) 10 times
,c) 11 times
d) 12 times. Answer: Answer: c) 11 times
◉ 11) What is the output of the following code fragment?
int i = 1;
int sum = 0;
while (i <= 11)
{
sum = sum + i;
i++;
}
System.out.println("The value of sum is " + sum);
a) The value of sum is 65
b) The value of sum is 66
c) The value of sum is 55
d) The value of sum is 56. Answer: Answer: b) The value of sum is 66
◉ 12) How many times does the loop execute in the following code
fragment?
, int i;
for (i = 0; i < 50; i = i + 4)
{
System.out.println(i);
}
a) 11
b) 12
c) 13
d) 14. Answer: Answer: c) 13
◉ 13) How many times does the following code snippet display
"Loop Execution"?
for (int i = 0; i < 10; i++);
{
System.out.println("Loop Execution");
}
a) Ten times.
b) The code snippet does not run because of a compile error.
c) Infinite loop.