nerdexam
Oracle

1Z0-803 · Question #268

Given: public class ColorTest { public static void main(String[] args) { String[] colors = {"red", "blue","green","yellow","maroon","cyan"}; int count = 0; for (String c : colors) { if (count >= 4) {

The correct answer is C. Compilation fails. The line,if (c.length() >= 4) {, is never reached. This causes a compilation error. Note:The continue statement skips the current iteration of a for, while , or do-while loop. An unlabeled break statement terminates the innermost switch, for, while, or do-while statement, but a l

Java Basics

Question

Given:

public class ColorTest { public static void main(String[] args) { String[] colors = {"red", "blue","green","yellow","maroon","cyan"}; int count = 0; for (String c : colors) { if (count >= 4) { break; } else { continue; } if (c.length() >= 4) { colors[count] = c.substring(0,3); } count++; } System.out.println(colors[count]); } } What is the result?

Options

  • AYellow
  • BMaroon
  • CCompilation fails
  • DA StringIndexOutOfBoundsException is thrown at runtime.

How the community answered

(37 responses)
  • A
    5% (2)
  • C
    92% (34)
  • D
    3% (1)

Explanation

The line,if (c.length() >= 4) {, is never reached. This causes a compilation error. Note:The continue statement skips the current iteration of a for, while , or do-while loop. An unlabeled break statement terminates the innermost switch, for, while, or do-while statement, but a labeled break terminates an outer statement.

Topics

#Array initialization#Compilation error#String arrays

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice