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 "...".
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)- A4% (1)
- B14% (4)
- C75% (21)
- D7% (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 "...".
This choice misses the unconditional execution of the `System.out.println("circle");` statement after the `if` block.
This choice incorrectly assumes the `if` condition is false or that "square" is not printed, and also misses the output of "square".
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.
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
Community Discussion
No community discussion yet for this question.