What is the output of the following code snippet?

Welcome to Java MCQ (Mulitple Choice Question) series. In this post, we will see one more quiz question on Java Loops.

What is the output of the following code snippet?

int i = 0;
for(i = 0 ; i < 5; i++){
}
System.out.println(i);
A. 5
B. 0
C. 4
D. Compilation Error

Answer:

A. 5

Explanation:

The integer variable i declared before using it in for loop, and can be accessible after for loop execution completes. In for loop, the i value will be incremented until the condition fails ( i < 5) so i value is 5.

Comments