Iterators(Computer Science > Java Program ) Questions and Answers

Question 1.


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() + " ");
}
}
  1.    2 8 5
  2.    2 1 8
  3.    2 5 8
  4.    8 5 1
Explanation:-
Answer: Option B. -> 2 1 8

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)



Question 2.


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() + " ");
}
}
  1.    2 8 5 1
  2.    1 5 8 2
  3.    1 2 5 8
  4.    2 1 8 5
Explanation:-
Answer: Option C. -> 1 2 5 8

Collections.sort(list) sorts the given list, the list was 2->8->5->1 after sorting it 


Question 3.


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() + " ");
}
}
  1.    2 8 5 1
  2.    1 5 8 2
  3.    2
  4.    2 1 8 5
Explanation:-
Answer: Option B. -> 1 5 8 2

Collections.reverse(list) reverses the given list, the list was 2->8->5->1 after 


Question 4.


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");
}
}
  1.    0
  2.    1
  3.    -1
  4.    EMPTY
Explanation:-
Answer: Option D. -> EMPTY

None.
Output:
$ javac Collection_iterators.java
$ java Collection_iterators
EMPTY



Question 5.

Which of these exceptions is thrown by remover() method?


  1.    IOException
  2.    SystemException
  3.    ObjectNotFoundExeception
  4.    IllegalStateException
Explanation:-
Answer: Option D. -> IllegalStateException

None.


Question 6.

Which of these is a method of ListIterator used to obtain index of previous 

element?


  1.    previous()
  2.    previousIndex()
  3.    back()
  4.    goBack()
Explanation:-
Answer: Option B. -> previousIndex()

previousIndex() returns index of previous element. if there is no previous element 


Question 7.

Which of these iterators can be used only with List?


  1.    Setiterator
  2.    ListIterator
  3.    Literator
  4.    None of the mentioned
Explanation:-
Answer: Option B. -> ListIterator

None.


Question 8.

Which of these methods can be used to move to next element in a collection?


  1.    next()
  2.    move()
  3.    shuffle()
  4.    hasNext()
Explanation:-
Answer: Option A. -> next()

None.


Question 9.

Which of these return type of hasNext() method of an iterator?


  1.    Integer
  2.    Double
  3.    Boolean
  4.    Collections Object
Explanation:-
Answer: Option C. -> Boolean

hasNext() returns boolean values true or false.



Question 10.

Which of these methods is used to obtain an iterator to the start of collection?


  1.    start()
  2.    begin()
  3.    iteratorSet()
  4.    iterator()
Explanation:-
Answer: Option D. -> iterator()

To obtain an iterator to the start of the start of the collection we use iterator() method.