39.1k views
2 votes
8. What is output by the code segment below?

int [] x = {1,2,3,4,5};
int [] y = new int [5] ;
for (int i : x)
y = x;
for (int i : y)
System.out.print (i + " ");
a.
b.
c.
d.
e.
5 4 3 2 1
1 2 3 4 5
2 3 4 5 1
5 1 2 3 4
0 0 0 0 0

1 Answer

5 votes

Answer:

b. 1 2 3 4 5

Step-by-step explanation:

Let us analyze the given code one statement at a time.

int [] x = {1,2,3,4,5}; // Initializes an integer array x

int [] y = new int [5] ; // Allocates a new integer array y

for (int i : x)

y = x;

This assign the variable y with the array x

for (int i : y)

System.out.print (i + " ");

The content of the attay y is printed one element at a time.

The final output will be as follows:

1 2 3 4 5

User Please Help
by
4.6k points