Share on Google+Share on Google+

SCJP Module-9 Question-21

Advertisement
The sample example given below will test your knowledge about the reversing an ArryList in Java.

Given below the sample code :

1 import java.util.*;
2 public class reverseLIst {
3 public static Iterator<String> reverse(List<String> mylist) {
4 Collections.reverse(mylist);
5 return mylist.iterator();
6 }
7
8 public static void main(String[] args) {
9 List<String> mylist = new ArrayList<String>();
10 mylist.add("1");
11 mylist.add("2");
12 mylist.add("3");
13 for (Object obj : reverse(mylist))
14 System.out.print(obj + ", ");
15 }
16 }

What will be the output of the above code ?

1. 3 2 1

2. 1 2 3

3. no output

4. Compilation error

Answer

(4)

Explanation

Error at line 13 . Can only iterate over an array or an instance of java.lang.Iterable .

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.