Menu
Question Index
...

What will be the output of the following program?

import java.util.*;

public class java {
    public static void main(String[] args) {
        Random rnd = new Random();
        Integer[] arr = new Integer[10];
        for (int i = 0; i < arr.length; i++)
            arr[i] = rnd.nextInt();
        Comparator<Integer> cmp = new Comparator<Integer>() {
            public int compare(Integer i1, Integer i2) {
                return i2 - i1;
            }
        };
        Arrays.sort(arr, cmp);
        System.out.println(order(arr));
    }
    enum Order {
        ASCENDING, DESCENDING, CONSTANT, UNORDER
    };
    static Order order(Integer[] a) {
        boolean ascending = false;
        boolean descending = false;
        for (int i = 1; i < a.length; i++) {
            ascending |= (a[i] > a[i - 1]);
            descending |= (a[i] < a[i - 1]);
        }
        if (ascending && descending)    return Order.ASCENDING;
        if (descending && !ascending)    return Order.DESCENDING;
        if (!ascending)    return Order.CONSTANT;
        return Order.UNORDER;
    }
}


ASCENDING
DESCENDING
CONSTANT
Some other output
Output can not be determined
Compilation Error or Runtime Error

Doubts

Problems

Topic: Java Random Class - java.util.Random Package

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App