144k views
0 votes
What is the output of the following Java code?

int[] alpha = {2, 4, 6, 8, 10};
int j;

for (j = 4; j >= 0; j--)

System.out.print(alpha[j] + " ");
System.out.println();

User Rmhartog
by
6.6k points

2 Answers

3 votes

Answer:

The Output to given java code can be given as follows:

Output:

10 8 6 4 2

Explanation:

  • In the given java code a one-dimensional array alpha is defined that contains some elements that are "2, 4, 6, 8, 10". In the next line, a variable j is declared and both array and variable data type is an integer.
  • In the next step, a for loop is declare that use variable j. It starts from 4 and ends when j value is greater than equal to 0 and decrements the value of variable j and inside a loop we print the value of the array. This loop prints array value in reverse order that's why the output is "10 8 6 4 2".
User Safiron
by
6.9k points
3 votes

Answer:

10 8 6 4 2

Step-by-step explanation:

If the mentioned code put inside in a static void main() of a public class the it will produce the aforesaid output. In the code, an array is traversed from the back. As array has elements till index 4 so, loop is started with j=4 and till j=0 and each time value of j is decremented and element corresponding to that index is printed each time on a same line. After whole array printed , a new line is printed as well.

User PastryExplosion
by
7.0k points