Problem of the Day
Thursday, November 21, 2024
Problem:
Consider the following code segment.
int a = 0;
int b = 5;
for (i = a; i < b; i++)
{
System.out.println(a);
}
What are the initial and final values of i
printed, and how many lines of output are produced?
initial value | final value | number of lines of output produced | |
a. | 0 | 4 | 4 |
b. | 0 | 4 | 5 |
c. | 0 | 5 | 6 |
d. | 1 | 4 | 4 |
The correct answer is b. The initial value printed is the initial value of i
, 0
, and the loop will only execute as long as i
is less than 5
, so 4
will be the last value printed. That makes a total of 5 values printed.