nerdexam
Oracle

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.

Creating and Using Arrays

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)
  • A
    84% (27)
  • B
    3% (1)
  • C
    3% (1)
  • D
    9% (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.

AString[][] arra = new String[3][];Correct

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.

Bfor (int i = 0; i < 3; i++) {

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.

Cfor (String a[]:arra[][]) {

The syntax `for (String a[]:arra[][]) {{` is incorrect for an enhanced `for` loop, as it attempts to iterate over multiple dimensions simultaneously.

Dfor (int i:arra.length) {

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

#2D arrays#array declaration#ragged arrays

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice