nerdexam
Oracle

1Z0-803 · Question #232

Given the code fragment: public static void main(String[] args) { int iArray[] = {65, 68, 69}; iArray[2] = iArray[0]; iArray[0] = iArray[1]; iArray[1] = iArray[2]; for (int element : iArray) { System.

The correct answer is B. 68, 65, 65. This code demonstrates sequential array element assignments, where the current values of elements are copied to other positions in the array.

Creating and Using Arrays

Question

Given the code fragment:

public static void main(String[] args) { int iArray[] = {65, 68, 69}; iArray[2] = iArray[0]; iArray[0] = iArray[1]; iArray[1] = iArray[2]; for (int element : iArray) { System.out.print(element + " "); }

Options

  • A68, 65, 69
  • B68, 65, 65
  • C65, 68, 65
  • D65, 68, 69
  • ECompilation fails

How the community answered

(25 responses)
  • A
    4% (1)
  • B
    92% (23)
  • E
    4% (1)

Why each option

This code demonstrates sequential array element assignments, where the current values of elements are copied to other positions in the array.

A68, 65, 69

This result indicates an incorrect final value for the second and third elements after the sequence of assignments.

B68, 65, 65Correct

The array is initialized as `{65, 68, 69}`. `iArray[2] = iArray[0]` changes it to `{65, 68, 65}`. Then, `iArray[0] = iArray[1]` updates it to `{68, 68, 65}`. Finally, `iArray[1] = iArray[2]` sets `iArray[1]` to 65, resulting in the final array state of `{68, 65, 65}`.

C65, 68, 65

This result suggests incorrect values for the first and third elements after the array manipulations.

D65, 68, 69

This result implies no changes or incorrect changes were made to the array elements during the assignment operations.

ECompilation fails

The code fragment contains valid Java syntax for array initialization and element assignment, so it will compile successfully.

Concept tested: Array element assignment, order of operations

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

Topics

#arrays#array assignment#for-each loop

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice