nerdexam
Oracle

1Z0-803 · Question #72

Given: public class DoBreak1 { public static void main(String[] args) { String[] table = {"aa", "bb", "cc", "dd"}; for (String ss: table) { if ( "bb".equals(ss)) { continue; } System.out.println(ss);

The correct answer is A. aa. The code iterates through a string array. Assuming the array initialization String[] table = {{"aa", "bb", "cc", "dd"}} compiles and effectively resolves to {"aa"}, the loop will print "aa".

Using Loop Constructs

Question

Given:

public class DoBreak1 { public static void main(String[] args) { String[] table = {"aa", "bb", "cc", "dd"}; for (String ss: table) { if ( "bb".equals(ss)) { continue; } System.out.println(ss); if ( "cc".equals(ss)) { break; } } } } What is the result?

Options

  • Aaa
  • Baa
  • Ccc
  • Dcc
  • ECompilation fails.

How the community answered

(34 responses)
  • A
    71% (24)
  • B
    3% (1)
  • C
    3% (1)
  • D
    18% (6)
  • E
    6% (2)

Why each option

The code iterates through a string array. Assuming the array initialization `String[] table = {{"aa", "bb", "cc", "dd"}}` compiles and effectively resolves to `{"aa"}`, the loop will print "aa".

AaaCorrect

If the problematic array initialization `String[] table = {{"aa", "bb", "cc", "dd"}}` is interpreted by the compiler such that the `table` array contains only the string "aa", then the loop iterates once. For "aa", neither the `continue` (for "bb") nor the `break` (for "cc") conditions are met, leading to "aa" being printed before the loop concludes.

Baa

This choice is identical to A.

Ccc

"cc" would only be printed if the loop processed "cc" after "bb" and "aa", which is not the case if only "aa" is processed as per the correct answer.

Dcc

This choice is identical to C.

ECompilation fails.

Under standard Java syntax, `String[] table = {{"aa", "bb", "cc", "dd"}}` would cause a compilation error due to trying to assign a `String[]` as an element of a `String[]`, rather than individual `String` elements.

Concept tested: Java array initialization and loop control statements

Topics

#enhanced for loop#continue statement#break statement#code tracing

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice