nerdexam
Oracle

1Z0-803 · Question #71

Given the code fragment: int [][] array2d = new int[2][3]; System.out.println("Loading the data."); for ( int x = 0; x < array2d.length; x++) { for ( int y = 0; y < array2d[0].length; y++) { System.ou

The correct answer is D. Load statement: array2d[x][y] = x + y;. The question requires selecting the correct Java syntax for accessing and assigning a value to an element within a two-dimensional array using row and column indices.

Creating and Using Arrays

Question

Given the code fragment:

int [][] array2d = new int[2][3]; System.out.println("Loading the data."); for ( int x = 0; x < array2d.length; x++) { for ( int y = 0; y < array2d[0].length; y++) { System.out.println(" x = " + x); System.out.println(" y = " + y); // insert load statement here. } } System.out.println("Modify the data. "); for ( int x = 0; x < array2d.length; x++) { for ( int y = 0; y < array2d[0].length; y++) { System.out.println(" x = " + x); System.out.println(" y = " + y); // insert modify statement here. } } Which pair of load and modify statement should be inserted in the code? The load statement should set the array's x row and y column value to the sum of x and y The modify statement should modify the array's x row and y column value by multiplying it by 2

Options

  • ALoad statement: array2d(x,y) = x + y;
  • BB. Load statement: array2d[x y] = x + y;
  • CLoad statement: array2d[x,y] = x + y;
  • DLoad statement: array2d[x][y] = x + y;
  • ELoad statement: array2d[[x][y]] = x + y;

How the community answered

(27 responses)
  • B
    4% (1)
  • C
    4% (1)
  • D
    93% (25)

Why each option

The question requires selecting the correct Java syntax for accessing and assigning a value to an element within a two-dimensional array using row and column indices.

ALoad statement: array2d(x,y) = x + y;

This syntax uses parentheses `()`, which is incorrect for array access and is instead used for method calls.

BB. Load statement: array2d[x y] = x + y;

This syntax uses a space ` ` between indices within a single set of brackets, which is not valid Java array indexing syntax.

CLoad statement: array2d[x,y] = x + y;

This syntax uses a comma `,` between indices within a single set of brackets, which is incorrect for accessing a 2D array element in Java.

DLoad statement: array2d[x][y] = x + y;Correct

In Java, to access an element at a specific row `x` and column `y` in a two-dimensional array `array2d`, the correct syntax uses two sets of square brackets, `array2d[x][y]`. The statement `array2d[x][y] = x + y;` correctly assigns the sum of `x` and `y` to the array element.

ELoad statement: array2d[[x][y]] = x + y;

This syntax uses nested square brackets `[[x][y]]`, which incorrectly implies accessing an array of arrays of arrays, not a simple 2D array element.

Concept tested: Two-dimensional array indexing and assignment

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

Topics

#2D arrays#array indexing#array syntax

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice