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.
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)- A71% (32)
- D18% (8)
- E11% (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.
This is a valid enhanced `for` loop (for-each loop) syntax, iterating over each element in the `arrar` array and assigning it to `i`.
This is a valid traditional `for` loop syntax, containing all three required parts: initialization (`int i = 0`), termination condition (`i < 1`), and increment (`i++`).
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.
This traditional `for` loop is missing the initialization statement for `i`, and `i` is not declared elsewhere, which prevents compilation.
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
Community Discussion
No community discussion yet for this question.