Java Questions & Answers – Iterators

This section of our 1000+ Java MCQs focuses on iterators of Java Programming Language.

1. Which of these return type of hasNext() method of an iterator?
a) Integer
b) Double
c) Boolean
d) Collections Object
View Answer

Answer: c
Explanation: hasNext() returns boolean values true or false.

2. Which of these methods is used to obtain an iterator to the start of collection?
a) start()
b) begin()
c) iteratorSet()
d) iterator()
View Answer

Answer: d
Explanation: To obtain an iterator to the start of the start of the collection we use iterator() method.

3. Which of these methods can be used to move to next element in a collection?
a) next()
b) move()
c) shuffle()
d) hasNext()
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

4. Which of these iterators can be used only with List?
a) Setiterator
b) ListIterator
c) Literator
d) None of the mentioned
View Answer

Answer: b
Explanation: None.

5. Which of these is a method of ListIterator used to obtain index of previous element?
a) previous()
b) previousIndex()
c) back()
d) goBack()
View Answer

Answer: b
Explanation: previousIndex() returns index of previous element. if there is no previous element then -1 is returned.
Note: Join free Sanfoundry classes at Telegram or Youtube

6. Which of these exceptions is thrown by remover() method?
a) IOException
b) SystemException
c) ObjectNotFoundExeception
d) IllegalStateException
View Answer

Answer: d
Explanation: None.

7. What will be the output of the following Java program?

advertisement
  1.     import java.util.*;
  2.     class Collection_iterators 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             ListIterator a = list.listIterator();
  7.                 if(a.previousIndex()! = -1)
  8.                     while(a.hasNext())
  9. 	                System.out.print(a.next() + " ");
  10.                 else
  11.                    System.out.print("EMPTY");
  12.         }
  13.     }

a) 0
b) 1
c) -1
d) EMPTY
View Answer

Answer: d
Explanation: None.
Output:

advertisement
$ javac Collection_iterators.java
$ java Collection_iterators
EMPTY

8. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     class Collection_iterators 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             LinkedList list = new LinkedList();
  7.             list.add(new Integer(2));
  8.             list.add(new Integer(8));
  9.             list.add(new Integer(5));
  10.             list.add(new Integer(1));
  11.             Iterator i = list.iterator();
  12.             Collections.reverse(list);
  13. 	    while(i.hasNext())
  14. 	        System.out.print(i.next() + " ");
  15.         }
  16.     }

a) 2 8 5 1
b) 1 5 8 2
c) 2
d) 2 1 8 5
View Answer

Answer: b
Explanation: Collections.reverse(list) reverses the given list, the list was 2->8->5->1 after reversing it became 1->5->8->2.
Output:

$ javac Collection_iterators.java
$ java Collection_iterators
1 5 8 2

9. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     class Collection_iterators 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             LinkedList list = new LinkedList();
  7.             list.add(new Integer(2));
  8.             list.add(new Integer(8));
  9.             list.add(new Integer(5));
  10.             list.add(new Integer(1));
  11.             Iterator i = list.iterator();
  12.             Collections.reverse(list);
  13. 	    Collections.sort(list);
  14.             while(i.hasNext())
  15. 	        System.out.print(i.next() + " ");
  16.         }
  17.     }

a) 2 8 5 1
b) 1 5 8 2
c) 1 2 5 8
d) 2 1 8 5
View Answer

Answer: c
Explanation: Collections.sort(list) sorts the given list, the list was 2->8->5->1 after sorting it became 1->2->5->8.
Output:

$ javac Collection_iterators.java
$ java Collection_iterators
1 2 5 8

10. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     class Collection_iterators 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             LinkedList list = new LinkedList();
  7.             list.add(new Integer(2));
  8.             list.add(new Integer(8));
  9.             list.add(new Integer(5));
  10.             list.add(new Integer(1));
  11.             Iterator i = list.iterator();
  12.             Collections.reverse(list);
  13. 	    Collections.shuffle(list);
  14.             i.next();
  15.             i.remove();
  16.             while(i.hasNext())
  17. 	        System.out.print(i.next() + " ");
  18.         }
  19.     }

a) 2 8 5
b) 2 1 8
c) 2 5 8
d) 8 5 1
View Answer

Answer: b
Explanation: i.next() returns the next element in the iteration. i.remove() removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next(). The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.
Output:

$ javac Collection_iterators.java
$ java Collection_iterators
2 1 8

(output will be different on your system)

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.