Switch to Dark Mode

Java Iterators

Here are 10 essential multiple-choice questions on Java Iterators, covering key concepts.

Last Updated : Apr 3, 2025
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

What happens if next() is called on an Iterator when there are no more elements?


  • A

    It returns null

  • B

    It resets the iterator

  • C

    It throws a NoSuchElementException

  • D

    It stops the iteration silently


Question 4

Which type of Java collection supports ListIterator?

  • A

    Set

  • B

    Queue

  • C

    Map

  • D

    List

Question 5

What is a key advantage of ListIterator over Iterator?


  • A

    It can iterate in both forward and backward directions

  • B

    It is faster than Iterator


  • C

    It can modify the collection structure while iterating

  • D

    It works with Set and Queue collections


Question 6

What is the output of the following code?

Java
import java.util.*;

public class Geeks {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));
        Iterator<String> iterator = list.iterator();
        while(iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
            iterator.remove();
        }
        System.out.println(list.isEmpty());
    }
}


  • A

    A B C true


  • B

    A B C false

  • C

    A B C [A, B, C]


  • D

    Compilation Error


Question 7

Which of the following is NOT true about the Iterator interface in Java?

  • A

    It allows removing elements during iteration

  • B

    It works with Set, List, and Queue

  • C

    It allows backward traversal using previous()

  • D

    It provides hasNext() and next() methods

Question 8

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 9

Which exception occurs when an Iterator is used after modifying the collection directly?


  • A

    NullPointerException

  • B

    ConcurrentModificationException

  • C

    IndexOutOfBoundsException

  • D

    IllegalArgumentException

Question 10

What is the correct way to iterate over a HashSet?


  • A
    Java
    for (int i = 0; i < hashSet.size(); i++) {
        System.out.println(hashSet.get(i));
    }
    


  • B
    Java
    Iterator<String> it = hashSet.iterator();
    while (it.hasNext()) {
        System.out.println(it.next());
    }
    


  • C
    Java
    for (String item : hashSet) {
        System.out.println(item);
    }
    


  • D

    Both B and C

There are 10 questions to complete.

Take a part in the ongoing discussion