nerdexam
Oracle

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.

Using Loop Constructs

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)
  • A
    3% (1)
  • B
    6% (2)
  • C
    77% (27)
  • D
    14% (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`.

AHello Java! 8

Incorrect, this choice is identical to the correct answer and represents the correct output.

BHello Java! 8

Incorrect, this choice is identical to the correct answer and represents the correct output.

CHello Java! 8Correct

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.

DCompilation fails

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

#do-while loop#control flow#static variables#boolean logic

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice