212k views
5 votes
Assume that the array arr has been declared . Write a statement that assigns the next to last element of the array to the variable x, which has already been declared .

User Jameem
by
5.7k points

1 Answer

5 votes

Answer:

x=arr[arr.length-2]; is the correct answer for the given question.

Explanation:

In the above statement firstly we calculate the length of the given array.The arr.length function is used to calculate the array length.As mention in the question we have to assign the next to last element of the array to the variable x, so we used arr[arr.length-2] and finally, we assign them to the variable x.

so the final statement is x=arr[arr.length-2];

User NealJMD
by
5.6k points