12.9k views
2 votes
What is the output of the following code fragment:

int[] ar = {2, 4, 6, 8 };
ar[0] = 23;
ar[3] = ar[1];
System.out.println( ar[0] + " " + ar[3] );

User Neztreh
by
6.6k points

1 Answer

2 votes

Final answer:

The output of the given Java code is '23 4'. The code changes the first element of the array to 23 and the last element to the value of the second element, which is 4.

Step-by-step explanation:

The question involves a piece of Java code that manipulates an array of integers. The code initializes the array ar with four elements, then changes the first element (ar[0]) to 23, and sets the last element (ar[3]) equal to the second element (ar[1]). Finally, it prints out the first and last elements of the array. When the code runs:

  • The statement ar[0] = 23 assigns the value 23 to the first element of the array, which was 2.
  • The statement ar[3] = ar[1] assigns the value of the second element (which is 4) to the last element of the array, which was 8.
  • So, the output of the System.out.println() statement will be 23 4.

User Ahsan Kamal
by
6.8k points