Menu
Topics Index
...
`

Utilities


If you need explanation Read this topic

If you need Answer Take test on this topic

Utilities
What will be the output of the following program?
import java.util.*;
public class WorkOrNot {
    public static void main(String args[]) {
        Integer big = new Integer(2000000000);
        Integer small = new Integer(-2000000000);
        Integer zero = new Integer(0);
        Integer[] arr = new Integer[]{big, small, zero};
        Arrays.sort(arr, new Comparator() {
            public int compare(Object o1, Object o2) {
                int i1 = ((Integer) o1).intValue();
                int i2 = ((Integer) o2).intValue();
                return (i2 < i1 ? -1 : (i2 == i1 ? 0 : 1));
            }
        });
        System.out.println(Arrays.asList(arr));
    }
}

A. [-2000000000, 0, 2000000000]
B. [2000000000, 0, -2000000000]
C. [-2000000000, 2000000000, 0]
D. [0, 2000000000, -2000000000]
E. Some other output
F. Compilation Error or Runtime Error
Topic: java.util.Arrays - Class Arrays In Collection Framework

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is B ...By using Comparator() we are sorting given array(arr) in descending order and finally the result array is printing as list by using asList().

Posted by Nagendra Reddy    2014-05-08 09:35:09


ans is B..1st tim comparator wil return 1 bcz i1=-200000000 n i2=200000000 n next it wl return -1 so on.

Posted by Uday Kumar    2014-05-08 09:36:52


Congratulations Nagendra Reddy. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-05-09 04:01:37


© meritcampus 2019

All Rights Reserved.

Open In App