Given:
public static Iterator reverse(List list) {
Collections.reverse(list);
return list.iterator();
}
public static void main(String[] args) {
List list = new ArrayList();
list.add("1");
list.add("2");
list.add("3");
for (Object obj: reverse(list))
System.out.print(obj + ", ");
}
What is the result?
A. 3, 2, 1,
B. 1, 2, 3,
C. Compilation fails.
D. The code runs with no output.
E. An exception is thrown at runtime.
Execution:
For Each is not applicable to expression type. You require an Array or Iterable
but not Iterator. Here the reverse method is returning an iterator
to correct it should be used as
for (Object obj: list)
Answer:
C. Compilation fails.
public static Iterator reverse(List list) {
Collections.reverse(list);
return list.iterator();
}
public static void main(String[] args) {
List list = new ArrayList();
list.add("1");
list.add("2");
list.add("3");
for (Object obj: reverse(list))
System.out.print(obj + ", ");
}
What is the result?
A. 3, 2, 1,
B. 1, 2, 3,
C. Compilation fails.
D. The code runs with no output.
E. An exception is thrown at runtime.
Execution:
For Each is not applicable to expression type. You require an Array or Iterable
but not Iterator. Here the reverse method is returning an iterator
to correct it should be used as
for (Object obj: list)
Answer:
C. Compilation fails.
No comments:
Post a Comment