MCQ Single Best Answer Moderate

QWhat will be the output of the following Java code?

for (int i = 0; i < 3; i++) {
    System.out.println(i);
}

ID: #23074 For Loop in Programming Language 69 views
Question Info
#23074Q ID
ModerateDifficulty
For Loop in Programming LanguageTopic

Choose the Best Option

Click any option to instantly check if you're correct.

  • A 0 1 2
  • B 1 2 3
  • C 0 1 2 3
  • D 1 2
Correct Answer: Option A

Explanation

The loop starts with i = 0 and prints the value of i in each iteration. The loop increments i by 1 after each iteration, and it runs as long as i < 3. Therefore, it prints 0, 1, and 2. Once i becomes 3, the loop condition i < 3 becomes false, and the loop terminates, so 3 is not printed. The correct sequence of outputs is 0 1 2.

Share This Question

Challenge a friend or share with your study group.