203k views
4 votes
What is the output of the following code fragment?

int[] zip = new int[5];
zip[0] = 7;
zip[1] = 3;
zip[2] = 4;
zip[3] = 1;
zip[4] = 9;
int j = 3;
System.out.println( zip[j-1]);
a.4
b.7
c.3
d.7

1 Answer

0 votes

Final answer:

The code fragment will output the number 4, as the 'println' statement refers to the third element of the array 'zip', which holds the value 4.

Step-by-step explanation:

The output of the given code fragment in Java is 4. Here are the steps that explain how this output is arrived at:

  1. An array named zip is created with a length of 5, which means it can hold 5 integer values.
  2. The code sequentially assigns the values 7, 3, 4, 1, and 9 to the indexes 0 through 4 of the array.
  3. An integer variable named j is set to 3.
  4. The System.out.println statement is asking to print the value of the array at the index j-1, which is 3-1=2.
  5. The value at index 2 of the zip array is 4, therefore 4 is printed to the console.
User Foob
by
7.3k points