Knowee
Questions
Features
Study Tools

What will be the output of the following Java code? public class array_output { public static void main(String args[]) { int array_variable [] = new int[10]; for (int i = 0; i < 10; ++i) { array_variable[i] = i; System.out.print(array_variable[i] + " "); i++; } } }

Question

What will be the output of the following Java code?

public class array_output

{

   public static void main(String args[])

   {

       int array_variable [] = new int[10];

for (int i = 0; i &lt; 10; ++i)

       {

           array_variable[i] = i;

           System.out.print(array_variable[i] + &quot; &quot;);

           i++;

       }

   }

}

🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the Java code will be:

0 2 4 6 8

Explanation:

The code initializes an array of size 10 and then enters a loop that runs 10 times. In each iteration of the loop, it assigns the current value of 'i' to the 'i'th index of the array and then prints the value. However, 'i' is incremented twice in each loop - once in the loop statement itself (i++) and once inside the loop (i++). This means that the loop only runs for every other index of the array, specifically the even indices. Therefore, the output is the values of the even indices of the array, which are just the even numbers from 0 to 8.

This problem has been solved

Similar Questions

What is the output of the following code?public class array_output { public static void main(String args[]) { int array_variable[] = new int[10]; for (int i = 0; i < 10; ++i) { array_variable[i] = i; System.out.print(array_variable[i] + " "); i++; } }}Options: Pick one correct answer from below0 2 4 6 81 3 5 7 90 1 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 9 10

What will be the output of the following Java program? class array_value { public static void main(String args[]) { int array_variable [] = new int[20]; for (int v = 10; v < 20; ++v) { array_variable[v] = v/2; array_variable[v]++; System.out.print(array_variable[v] + " "); v++; }  } }Options6 7 8 9 1010 12 14 16 1810 11 12 13 14 15 16 17 18 1910 11 12 13 14 15 16 17 18 19 20

Select the correct answerWhat will be the output of the following Java code?class array_output {  public static void main(String args[])   {    int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}};    int sum = 0;    for (int i = 1; i < 3; ++i)      for (int j = 1; j < 3 ; ++j)        sum = sum + array_variable[i][j];    System.out.print(sum / 6);  } }Options8413

e correct answerWhat is the output of the following Java program?class Main { public static void main(String args[]) { final int i; i = 20; i = 30; System.out.println(i); }}

elect the correct answerWhat will be the output of the following Java code?class array_output {  public static void main(String args[])   {    char array_variable [] = new char[10]; for (int i = 0; i < 3; ++i)     {      array_variable[i] = 'R';      System.out.print(array_variable[i] + "");    }  } }OptionsPQR0 1 2 3 4 51 2 3 4 5 6 7 8 9 10RRR

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.