Menu
Question Index
...

What will be output of the following program?

class PrintNumbers
{
    public static void main(String[] args)
    {
        int a[] = {2};
        a = new int[]{5};
        int x = a[0];
        while (x >= 0)
        {
            process(a);
            x = a[0];
            x--;
            System.out.println("x = " + x);
        }
    }

    private static void process(int[] a)
    {
        int x = a[0]--;
        x++;
    }
}


x = 4
x = 3
x = 2
x = 1
x = 3
x = 2
x = 1
x = 0
x = -1
x = 4
x = 3
x = 2
x = 1
x = 0
Compilation Error
Goes into infinite loop

Doubts

Problems

Topic: Learn Arrays And Loops

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App