Home Tutorial Java Scjp Part9 SCJP Module-9 Question-21

 
 

SCJP Module-9 Question-21
Posted on: July 19, 2010 at 12:00 AM
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 .

Related Tags for SCJP Module-9 Question-21:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.