Share on Google+Share on Google+

SCJP Module-9 Question-22

Ads
The sample code given below will test your understanding of reversing the ArrayList object using the Collection Interface.

Given below the sample code :

import java.util.*;
public class reverseLIst {
public static void main(String[] args) {
ArrayList<String> mylist = new ArrayList<String>();
mylist.add("1");
mylist.add("2");
mylist.add("3");
System.out.println("Before reversing array list:"+mylist);
Collections.reverse(mylist);
System.out.println("After reversing array list:"+mylist);
}
}

What will be the output of the above code ?

1. Compilation error

2. No output

3. Before reversing array list:[1, 2, 3]
    After reversing array list:[3, 2, 1]

4. Before reversing array list:[1, 2, 3]
    After reversing array list:[1, 2, 3]

Answer

(3)

Advertisements

Posted on: July 19, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.