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".
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)- A71% (24)
- B3% (1)
- C3% (1)
- D18% (6)
- E6% (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".
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.
This choice is identical to A.
"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.
This choice is identical to C.
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
Community Discussion
No community discussion yet for this question.