Ask Question

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 ();

+2
Answers (2)
  1. 14 March, 04:27
    0
    10 8 6 4 2

    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.
  2. 14 March, 04:28
    0
    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".
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “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 ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers