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
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
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
Explanation: None.
4. Which of these iterators can be used only with List?
a) Setiterator
b) ListIterator
c) Literator
d) None of the mentioned
View Answer
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
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
View Answer
Explanation: None.
7. What will be the output of the following Java 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
View Answer
Explanation: None.
Output:
$ javac Collection_iterators.java
$ java Collection_iterators
EMPTY
8. What will be the output of the following Java 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
View Answer
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?
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
View Answer
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?
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
View Answer
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]
- Check Programming Books
- Practice Programming MCQs
- Practice BCA MCQs
- Check Java Books
- Apply for Computer Science Internship