Learn AP Comp Sci

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 valuefinal valuenumber of lines of output produced
a.044
b.045
c.056
d.144

Show solution:

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.