What is the result?

Given:

import java.util.*;
public class SortOf {
public static void main(String[] args) {
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(1); a.add(5); a.add(3);
Collections.sort(a);
a.add(2);
Collections.reverse(a);
System.out.println(a);
}
}

What is the result?

Given:

import java.util.*;
public class SortOf {
public static void main(String[] args) {
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(1); a.add(5); a.add(3);
Collections.sort(a);
a.add(2);
Collections.reverse(a);
System.out.println(a);
}
}

What is the result?

A.
[1, 2, 3, 5]

B.
[2, 1, 3, 5]

C.
[2, 5, 3, 1]

D.
[5, 3, 2, 1]

E.
[1, 3, 5, 2]

F.
Compilation fails.

G.
An exception is thrown at runtime.



Leave a Reply 2

Your email address will not be published. Required fields are marked *


Gowtham

Gowtham

First the values enter as [1,5,3]
Then after sort it will be [1,3,5]
Then we add [2] to the list
Now the list will be [1,3,5,2]
after “reverse” the answer is [2,5,3,1]