Menu
Question Index
...

What will be the output of the following program?

public class Test {
    public static void main(String[] args) {
        int[] arg = {5, 0, 2, 3, 1, 4};
        int length = arg.length;
        int[] array = new int[length];
        for (int i = 0; i < length; i++)
            array[i] = arg[i];
        boolean[] exists = new boolean[length];
        for (int i = 0; i < length; i++) {
            if (array[i] < 0 || array[i] >= length || exists[array[i]])
                throw new RuntimeException("Input is not a per.");
            exists[array[i]] = true;
        }
        int[] ainv = new int[length];
        for (int i = 0; i < length; i++)
            ainv[array[i]] = i;
        for (int i = 0; i < length; i++)
            System.out.print(ainv[i] + " ");
        System.out.println();
    }
}


4 1 3 2 0 5
1 4 2 3 5 0
1 4 3 2 5 0
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: java.util.Arrays - Class Arrays In Collection Framework

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App