SCJP Module-9 Question-21


 

SCJP Module-9 Question-21

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

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 .

Ads