Unit 4: Iteration

What is the output of the following code?

for (int i = 0; i < 5; i++) {
    System.out.print(i + " ");
}

a) 0 1 2 3 4
b) 1 2 3 4 5
c) 0 1 2 3 4 5
d) 1 2 3 4

Answer: a) 0 1 2 3 4

How many times will the loop execute?

int count = 0;
while (count < 3) {
    count++;
}

a) 2
b) 3
c) 4
d) 1

Answer: b) 3

Which type of loop is guaranteed to execute at least once?
a) for
b) while
c) do-while
d) none of the above

Answer: c) do-while