nerdexam
Oracle

1Z0-851 · Question #265

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")…

The correct answer is C. Compilation fails. Can only iterate over an array or an instance of java.lang.Iterable

Generics and Collections

Question

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?

Options

  • A3, 2, 1,
  • B1, 2, 3,
  • CCompilation fails.
  • DThe code runs with no output.
  • EAn exception is thrown at runtime.

How the community answered

(42 responses)
  • A
    12% (5)
  • B
    2% (1)
  • C
    76% (32)
  • D
    2% (1)
  • E
    7% (3)

Explanation

Can only iterate over an array or an instance of java.lang.Iterable

Topics

#Iterator#Iterable#for-each loop#raw types

Community Discussion

No community discussion yet for this question.

Full 1Z0-851 Practice