Java Enumeration Iterator & ListIterator MCQ

Java Enumeration Iterator & ListIterator MCQ | In Java, collection framework classes are used to store elements in value or key-value format without size limitation. We have listed some Enumeration, Iterator, & ListIterator MCQ and programming questions, answer them. See:- Java Collections Quiz-1

Also see- Java Collection Framework OverviewJava Collection InterfaceList Interface in Java

Q1) Using Enumeration(I) we can perform?

a) Read Operation
b) Remove Operation
c) Add/Update Operation
d) Both (a) and (b)
e) All of these


Answer:- a) Read Operation

Using Enumeration(I) we can perform only read operation. The nextElement() read the elements.


Q2) Using Iterator(I) we can perform?

a) Read Operation
b) Remove Operation
c) Add/Update Operation
d) Both (a) and (b)
e) All of these


Answer:- d) Both (a) and (b)

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

Using Iterator(I) we can perform both read and remove operations. Iterator(I) contains next() method to read the element, and remove() method to delete/remove the element.


Q3) Using ListIterator(I) we can perform?

a) Read Operation
b) Remove Operation
c) Add/Update Operation
d) Both (a) and (b)
e) All of these


Answer:- e) All of these

Using ListIterator(I) we can read, remove, add, and update elements. ListIterator(I) is the child interface of Iterator(I) therefore it can read, and remove the element. Along with this, it also contains add() and set() methods to add and update operations.


Q4) Enumeration and Iterator is _ but ListIterator is __ ?

a) Unidirectional, Bidirectional
b) Bidirectional, Unidirectional
c) Unidirectional, Unidirectional
d) Bidirectional, Bidirectional


Answer:- a) Unidirectional, Bidirectional

The Enumeration and Iterator can move only in the forward direction but ListIterator can move in both the forward and backward directions. Hence Enumeration & Iterator are Unidirectional whereas ListIterator is bidirectional.


Q5) Using which method do we get the Enumeration cursor in Vector and Hashtable class?

a) elements()
b) enumeration()
c) enumerate()
d) getCursor()


Answer:- a) elements()

In Vector and Hashtable elements() method is given using which we can get the Enumeration cursor.

Example:-
Vector<string> vc = new Vector<string>();
Enumeration<string> e = vc.elements();


Q6) Using which method do we get the Enumeration cursor in all Collection classes?

a) elements()
b) enumeration()
c) enumerate()
d) getCursor()


Answer:- enumeration()

In Collections class enumeration() method is given to get the Enumeration cursor.

Example:-
ArrayList<string> al = new ArrayList<string>();
Enumeration<string> e = Collections.enumeration(al);


Q7) Find the output of the below Enumeration program?

import java.util.Enumeration;
import java.util.Vector;

public class Test {
   public static void main(String[] args) {

      Vector<Integer> v = new Vector<Integer>();
      for (int i=0; i<=10 ; i++) {
         v.addElement(i);
      }

      Enumeration<Integer> e = v.elements();
      while (e.hasMoreElements()) {
         Integer i = e.nextElement();
         if(i % 2 != 0) 
            System.out.print(i+" ");
      }
   }
}

a) 1 3 5 7 9
b) 2 4 6 8 10
c) 0 1 2 3 4 5 6 7 8 9 10
d) None of these


Answer:- a) 1 3 5 7 9

After inserting elements to the vector, the vector contains [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. We used Enumeration(I) to get the vector but we display only odd elements.


Q8) Find the output of the below Enumeration program?

import java.util.Enumeration;
import java.util.Vector;

public class Test {
   public static void main(String[] args) {

      Vector<Integer> v = new Vector<Integer>();
      v.addElement(10);
      v.addElement(20);
      // System.out.println(v);

      Enumeration<Integer> e = v.elements();
      
      v.addElement(88);
      v.addElement(99);
      // System.out.println(v);
      
      Enumeration<Integer> e1 = v.elements();
      
      while (e.hasMoreElements()) {
         Integer i = e.nextElement();
         System.out.print(i+" ");
      }
      
      while (e1.hasMoreElements()) {
          Integer i = e1.nextElement();
          System.out.print(i+" ");
       }
   }
}

a) 10 20 10 20 88 99
b) 10 20 88 99 10 20 88 99
c) Compile time error
d) Exception raised at Runtime


Answer:- b) 10 20 88 99 10 20 88 99

Q9) Find the output of the below Iterator program?

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test {
   public static void main(String[] args) {

      List<Integer> al = new ArrayList<Integer>();
      al.add(10);
      al.add(20);
      // System.out.println(al);

      Iterator<Integer> itr = al.iterator();

      al.add(88);
      al.add(99);
      // System.out.println(al);

      Iterator<Integer> itr1 = al.iterator();

      while (itr.hasNext()) {
         Integer i = itr.next();
         System.out.print(i + " ");
      }

      while (itr.hasNext()) {
         Integer i = itr.next();
         System.out.print(i + " ");
      }
   }
}

a) 10 20 10 20 88 99
b) 10 20 88 99 10 20 88 99
c) Compile time error
d) Exception raised at Runtime


Answer:- d) Exception raised at Runtime

Here we will get ConcurrentModificationException.

While retrieving elements, the collection object structure should not be modified by either adding elements or removing elements using collection object methods. After this modification, if we call itr.next(), it leads to java.util.ConcurrentModificationException. This behavior is called fail-fast. Note:- This point is also applicable for ListIterator.


Q10) Enumeration is ___ but Iterator and ListIterator are ___?

a) Fail-Fast, Fail-Fast
b) Fail-Fast, Fail-Safe
c) Fail-Safe, Fail-Fast
d) Fail-Safe, Fail-Safe


Answer:- c) Fail-Safe, Fail-Fast

The Enumeration is Fail-Safe it means while retrieving elements, the collection object can be modified and it will reflect in the Enumeration. But Iterator and ListIterator are Fail-Fast it means while retrieving elements, the collection object structure should not be modified by either adding elements or removing elements using collection object methods. After this modification, if we call itr.next(), it leads to java.util.ConcurrentModificationException.


Q11) Exception can be raised at which place?

import java.util.Enumeration;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Vector;

public class Test {
   public static void main(String[] args) {
      Vector<Integer> vc = new Vector<Integer>();

      Enumeration<Integer> e = vc.elements();
      System.out.println(e.nextElement()); // PLACE-1

      Iterator<Integer> itr = vc.iterator();
      System.out.println(itr.next()); // PLACE-2

      ListIterator<Integer> litr = vc.listIterator();
      System.out.println(litr.next()); // PLACE-3
   }
}

a) PLACE-1
b) PLACE-2
c) PLACE-3
d) All of these


Answer:- d) All of these

The exception “NoSuchElementException” will be raised at all three places, because we can’t read operation on empty Collection. Currently Vector class is empty.


Q12) Find the output of the below program?

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class Test {
   public static void main(String[] args) {

      List<Integer> ll = new LinkedList<Integer>();
      for (int i=10; i<=15 ; i++) {
         ll.add(i);
      }
      
      Iterator<Integer> itr = ll.iterator();
      while (itr.hasNext()) {
         Integer i = itr.next();
         if(i % 2 != 0) 
            System.out.print(i+" ");
         else 
            itr.remove();
      }
      
      // display list
      System.out.println(ll);
   }
}

a) 11 13 15 [10, 11, 12, 13, 14, 15]
b) 11 13 15 [11, 13, 15]
c) 10 11 12 13 14 15 [10, 11, 12, 13, 14, 15]
d) None of these


Answer:- b) 11 13 15 [11, 13, 15]

Initially, the list contains [10, 11, 12, 13, 14, 15], while iterating the list we displayed odd numbers, and the remaining numbers are removed from the list. Later list contains only [11, 13, 15].


Q13) Find the output of the below program?

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class Test {
   public static void main(String[] args) {

      List<String> al = new ArrayList<String>();
      al.add("A");
      al.add("B");
      al.add("C");
      al.add("D");

      ListIterator<String> litr = al.listIterator();
      while (litr.hasNext()) {
         String str = litr.next();
         if (str.equals("A"))
            litr.remove();
         else if (str.equals("B"))
            litr.add("E");
         else if (str.equals("C"))
            litr.set("F");
      }
      
      System.out.println(al);
   }
}

a) [B, E, F, D]
b) [B, F, D, E]
c) [B, C, D, E, F]
d) None of these

Answer:- a) [B, E, F, D]

Initially, the list contains [A, B, C, D]. Using an iterator we started iterating the list. On calling next() cursor moved to “A”. The “A” is matched with the if-else condition, and due to remove() method “A” will be removed from the list. Currently, the list becomes [B, C, D], and the cursor is at the beginning of the list.

On calling next() cursor hold “B”. It is matched with the if-else condition and “E” will be added to the list. Note that here “E” will be added to the place where the cursor is available but not at the end of the list. Now, the list becomes [B, E, C, D]. Currently, the cursor holds “E”.

On calling next() cursor moved to “C”. Due to the set() method “C” will be replaced with “F”. Now the list becomes [B, E, F, D].

On Calling next() cursor moved to “D” but therefore there is no operation for the string “D”. Finally iteration was completed, and control came out of the loop. The list is:- [B, E, F, D]

You can demonstrate it through below code,

while (litr.hasNext()) {
String str = litr.next();
System.out.println(str);

if (str.equals("A")) {
litr.remove();
System.out.println(al);
}

else if (str.equals("B")) {
litr.add("E");
System.out.println(al);
}

else if (str.equals("C")) {
litr.set("F");
System.out.println(al);
}
}

System.out.println(al);


Q14) Find the output of the below code?

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class Test {
   public static void main(String[] args) {

      List<String> al = new ArrayList<String>();
      al.add("A");
      al.add("B");
      al.add("C");
      al.add("D");

      ListIterator<String> litr = al.listIterator();
      while(litr.hasNext()) {
        litr.next();
        litr.next();
        litr.remove();
        litr.remove();
      }
      System.out.println(al);
   }
}

a) [ ]
b) [A, C]
c) Exception
d) None of these


Answer:- c) Exception

In this program, we will get IllegalStateException. The remove() method must be called only after the next() method call that to only once, violation leads to java.util.IllegalStateException.


Q15) Find the output of the below program?

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class Test {
   public static void main(String[] args) {

      List<String> ll = new LinkedList<String>();
      ll.add("A");
      ll.add("B");
      ll.add("C");
      ll.add("D");
      
      ListIterator<String> litr = ll.listIterator();
      litr.next();
      litr.next();
      
      while (litr.hasPrevious()) {
         String str = litr.previous();
         System.out.print(str + " ");
      }
   }
}

a) A B
b) B A
c) A
d) D C


Answer:- b) B A

On calling next() two times the cursor it at “B”. On calling previous() cursor will remain on “B”. Again previous() is called in next iteration then cursor moved to “A”.


Q16) Find the output of the below code?

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class Test {
   public static void main(String[] args) {

      List<String> al = new ArrayList<String>();
      al.add("A");
      al.add("B");
      al.add("C");
      al.add("D");

      ListIterator<String> litr = al.listIterator();
      litr.next();
      System.out.print(litr.next() + " ");
      litr.remove();
      
      System.out.print(litr.previous() + " ");
      litr.remove();
      System.out.println(al);
   }
}

a) B A [C, D]
b) A B [C, D]
c) A B [B, C, D]
d) None of these


Answer:- a) B A [C, D]

The ListIterator is a bi-directional cursor and it can move both backward and forwards direction.


If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Mern Stack Web Development
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

Leave a Comment

Your email address will not be published. Required fields are marked *