1Z0-803 · Question #241
Given the fragment: String[][] arra = new String[3][]; arra[0] = new String[]{"rose", "lily"}; arra[1] = new String[]{"apple", "berry","cherry","grapes"}; arra[0] = new String[]{"beans", "carrot","pot
The correct answer is A. String[][] arra = new String[3][];. This question asks for a code fragment that, when inserted, would enable changing arra elements to uppercase, primarily testing Java array and loop syntax validity.
Question
Given the fragment:
String[][] arra = new String[3][]; arra[0] = new String[]{"rose", "lily"}; arra[1] = new String[]{"apple", "berry","cherry","grapes"}; arra[0] = new String[]{"beans", "carrot","potato"}; // insert code fragment here Which code fragment when inserted at line '// insert code fragment here', enables the code to successfully change arra elements to uppercase?
Options
- AString[][] arra = new String[3][];
- Bfor (int i = 0; i < 3; i++) {
- Cfor (String a[]:arra[][]) {
- Dfor (int i:arra.length) {
How the community answered
(32 responses)- A84% (27)
- B3% (1)
- C3% (1)
- D9% (3)
Why each option
This question asks for a code fragment that, when inserted, would enable changing `arra` elements to uppercase, primarily testing Java array and loop syntax validity.
This choice, `String[][] arra = new String[3][];`, is a syntactically valid and complete statement that re-initializes the `arra` variable. While it discards existing data rather than modifying it, it is the only syntactically correct and complete fragment among the options that can be inserted without immediate compilation errors.
This fragment `for (int i = 0; i < 3; i++) {{` is an incomplete `for` loop construct and would result in a compilation error if inserted as a standalone statement.
The syntax `for (String a[]:arra[][]) {{` is incorrect for an enhanced `for` loop, as it attempts to iterate over multiple dimensions simultaneously.
The syntax `for (int i:arra.length) {{` is incorrect; `arra.length` returns an integer, which is not an iterable type suitable for an enhanced `for` loop.
Concept tested: Java array declaration, initialization, and loop syntax
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Topics
Community Discussion
No community discussion yet for this question.