Java Iterators

Last Updated :
Discuss
Comments

Question 1

What is the purpose of an Iterator in Java?


  • A

    To modify elements in a collection

  • B

    To traverse a collection one element at a time


  • C

    To sort elements in a collection

  • D

    To store elements in a collection

Question 2

Which method of Iterator removes the last element returned by next()?


  • A

    delete()

  • B

    remove()

  • C

    discard()

  • D

    erase()

Question 3

Which type of Java collection supports ListIterator?

  • A

    Set

  • B

    Queue

  • C

    Map

  • D

    List

Question 4

How can you safely modify a collection while iterating through it?


  • A

    Using Iterator.remove()

  • B

    Using List.remove(Object) inside a loop

  • C

    Using collection.stream().forEach()


  • D

    Using for-each loop



Question 5

Which method of the Iterator interface removes the last element returned by the iterator?

  • A

    delete()

  • B

    remove()

  • C

    clear()

  • D

    discard()

Question 6

What happens if you modify a collection directly while iterating over it using a normal Iterator?

  • A

    It updates automatically

  • B

    Throws ConcurrentModificationException

  • C

    Skips modified elements

  • D

    The iterator restarts

Question 7

Which iterator type allows iteration in both directions in Java?

  • A

    Iterator

  • B

    Enumeration

  • C

    CollectionIterator

  • D

    ListIterator

There are 7 questions to complete.

Take a part in the ongoing discussion