nerdexam
Oracle

1Z0-803 · Question #89

Which three are valid replacements for foo so that the program will compiled and run? public class ForTest { public static void main(String[] args) { int[] arrar = {1,2,3}; for ( foo ) { } } }

The correct answer is A. int i: array B. int i = 0; i < 1; i++ C. ;;. Java's for loop supports both enhanced (for-each) syntax and traditional syntax, each with specific requirements for its components to be syntactically valid.

Using Loop Constructs

Question

Which three are valid replacements for foo so that the program will compiled and run? public class ForTest { public static void main(String[] args) { int[] arrar = {1,2,3}; for ( foo ) { } } }

Options

  • Aint i: array
  • Bint i = 0; i < 1; i++
  • C;;
  • D; i < 1; i++
  • E; i < 1;

How the community answered

(45 responses)
  • A
    71% (32)
  • D
    18% (8)
  • E
    11% (5)

Why each option

Java's `for` loop supports both enhanced (for-each) syntax and traditional syntax, each with specific requirements for its components to be syntactically valid.

Aint i: arrayCorrect

This is a valid enhanced `for` loop (for-each loop) syntax, iterating over each element in the `arrar` array and assigning it to `i`.

Bint i = 0; i < 1; i++Correct

This is a valid traditional `for` loop syntax, containing all three required parts: initialization (`int i = 0`), termination condition (`i < 1`), and increment (`i++`).

C;;Correct

This is a valid traditional `for` loop syntax where all three components (initialization, termination, and increment) are optionally omitted, creating an infinite loop that compiles and runs.

D; i < 1; i++

This traditional `for` loop is missing the initialization statement for `i`, and `i` is not declared elsewhere, which prevents compilation.

E; i < 1;

This traditional `for` loop is missing both the initialization and the increment statements for `i`, and `i` is not declared elsewhere, which prevents compilation.

Concept tested: Java for loop syntax (traditional and enhanced)

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

Topics

#for loop#enhanced for loop#loop syntax

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice