nerdexam
Oracle

1Z0-803 · Question #91

Given the code fragment: int b = 3; if ( !(b > 3)) { System.out.println("square"); }{ System.out.println("circle"); } System.out.println("..."); What is the result?

The correct answer is C. squarecircle.... The if condition evaluates to true, causing "square" to print, and the subsequent standalone code block containing "circle" always executes, followed by "...".

Using Operators and Decision Constructs

Question

Given the code fragment:

int b = 3; if ( !(b > 3)) { System.out.println("square"); }{ System.out.println("circle"); } System.out.println("..."); What is the result?

Options

  • Asquare...
  • Bcircle...
  • Csquarecircle...
  • DCompilation fails.

How the community answered

(28 responses)
  • A
    4% (1)
  • B
    14% (4)
  • C
    75% (21)
  • D
    7% (2)

Why each option

The `if` condition evaluates to true, causing "square" to print, and the subsequent standalone code block containing "circle" always executes, followed by "...".

Asquare...

This choice misses the unconditional execution of the `System.out.println("circle");` statement after the `if` block.

Bcircle...

This choice incorrectly assumes the `if` condition is false or that "square" is not printed, and also misses the output of "square".

Csquarecircle...Correct

The condition `!(b > 3)` evaluates to `!(3 > 3)`, which is `!(false)` or `true`, so "square" is printed. The block `{{ System.out.println("circle"); }}` is not an `else` block and is executed unconditionally after the `if` statement, printing "circle", followed by "..." from the final statement.

DCompilation fails.

The code fragment is syntactically valid and will compile without errors.

Concept tested: Java conditional logic, block execution flow

Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

Topics

#if statement#conditional logic#code blocks

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice