1Z0-803 · Question #257
Given: public class Test { static boolean bVar; public static void main(String[] args) { boolean bVar1 = true; int count =8; do { System.out.println("Hello Java! " +count); if (count >= 7) { bVar1 = f
The correct answer is C. Hello Java! 8. The do-while loop executes its body at least once. The loop condition bVar != bVar1 && count > 4 becomes false after the first iteration because bVar1 is changed to false, matching bVar.
Question
Given:
public class Test { static boolean bVar; public static void main(String[] args) { boolean bVar1 = true; int count =8; do { System.out.println("Hello Java! " +count); if (count >= 7) { bVar1 = false; } } while (bVar != bVar1 && count > 4); count -= 2; } } What is the result?
Options
- AHello Java! 8
- BHello Java! 8
- CHello Java! 8
- DCompilation fails
How the community answered
(35 responses)- A3% (1)
- B6% (2)
- C77% (27)
- D14% (5)
Why each option
The `do-while` loop executes its body at least once. The loop condition `bVar != bVar1 && count > 4` becomes `false` after the first iteration because `bVar1` is changed to `false`, matching `bVar`.
Incorrect, this choice is identical to the correct answer and represents the correct output.
Incorrect, this choice is identical to the correct answer and represents the correct output.
C is correct because the `do-while` loop executes at least once, printing "Hello Java! 8". During this first iteration, `bVar1` is set to `false`, making the loop condition `(false != false && count > 4)` evaluate to `false`, which causes the loop to terminate after a single execution.
Incorrect, the code has no compilation errors.
Concept tested: Java do-while loop, static boolean default value, short-circuit evaluation.
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
Topics
Community Discussion
No community discussion yet for this question.