Java Multiple Choice Questions & Answers on Iterators for Freshers

https://www.computersprofessor.com/2018/01/java-multiple-choice-questions-answers_29.html
1. Which of these return type of hasNext() method of an iterator?
a) Integer
b) Double
c) Boolean
d) Collections Object
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()
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()
Answer: a
4. Which of these iterators can be used only with List?
a) Setiterator
b) ListIterator
c) Literator
d) None of the mentioned
Answer: b
5. Which of these is a method of ListIterator used to obtain index of previous element?
a) previous()
b) previousIndex()
c) back()
d) goBack()
Answer: b
Explanation: previousIndex() returns index of previous element. if there is no previous element then -1 is returned.
6. Which of these exceptions is thrown by remover() method?
a) IOException
b) SystemException
c) ObjectNotFoundExeception
d) IllegalStateException
Answer: d
7. What is the output of this program?
import java.util.*;
class Collection_iterators {
public static void main(String args[]) {
ListIterator a = list.listIterator();
if(a.previousIndex()! = -1)
while(a.hasNext())
System.out.print(a.next() + " ");
else
System.out.print("EMPTY");
}
}
a) 0
b) 1
c) -1
d) EMPTY
b) 1
c) -1
d) EMPTY
Answer: d
8. What is the output of this program?
import java.util.*;
class Collection_iterators {
public static void main(String args[]) {
LinkedList list = new LinkedList();
list.add(new Integer(2));
list.add(new Integer(8));
list.add(new Integer(5));
list.add(new Integer(1));
Iterator i = list.iterator();
Collections.reverse(list);
while(i.hasNext())
System.out.print(i.next() + " ");
}
}
a) 2 8 5 1
b) 1 5 8 2
c) 2
d) 2 1 8 5
b) 1 5 8 2
c) 2
d) 2 1 8 5
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.
9. What is the output of this program?
import java.util.*;
class Collection_iterators {
public static void main(String args[]) {
LinkedList list = new LinkedList();
list.add(new Integer(2));
list.add(new Integer(8));
list.add(new Integer(5));
list.add(new Integer(1));
Iterator i = list.iterator();
Collections.reverse(list);
Collections.sort(list);
while(i.hasNext())
System.out.print(i.next() + " ");
}
}
a) 2 8 5 1
b) 1 5 8 2
c) 1 2 5 8
d) 2 1 8 5
b) 1 5 8 2
c) 1 2 5 8
d) 2 1 8 5
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.
10. What is the output of this program?
import java.util.*;
class Collection_iterators {
public static void main(String args[]) {
LinkedList list = new LinkedList();
list.add(new Integer(2));
list.add(new Integer(8));
list.add(new Integer(5));
list.add(new Integer(1));
Iterator i = list.iterator();
Collections.reverse(list);
Collections.shuffle(list);
i.next();
i.remove();
while(i.hasNext())
System.out.print(i.next() + " ");
}
}
a) 2 8 5
b) 2 1 8
c) 2 5 8
d) 8 5 1
b) 2 1 8
c) 2 5 8
d) 8 5 1
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.