Java Multiple Choice Questions & Answers on Iterators for Freshers

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?
  1.     import java.util.*;
  2.     class Collection_iterators {
  3.         public static void main(String args[]) {
  4.             ListIterator a = list.listIterator();
  5.                 if(a.previousIndex()! = -1)
  6.                     while(a.hasNext())
  7.                  System.out.print(a.next() + " ");
  8.                 else
  9.                    System.out.print("EMPTY");
  10.         }
  11.     }
a) 0
b) 1
c) -1
d) EMPTY
Answer: d
8. What is the output of this program?
  1.     import java.util.*;
  2.     class Collection_iterators {
  3.         public static void main(String args[]) {
  4.             LinkedList list = new LinkedList();
  5.             list.add(new Integer(2));
  6.             list.add(new Integer(8));
  7.             list.add(new Integer(5));
  8.             list.add(new Integer(1));
  9.             Iterator i = list.iterator();
  10.             Collections.reverse(list);
  11.      while(i.hasNext())
  12.          System.out.print(i.next() + " ");
  13.         }
  14.     }
a) 2 8 5 1
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?
  1.     import java.util.*;
  2.     class Collection_iterators {
  3.         public static void main(String args[]) {
  4.             LinkedList list = new LinkedList();
  5.             list.add(new Integer(2));
  6.             list.add(new Integer(8));
  7.             list.add(new Integer(5));
  8.             list.add(new Integer(1));
  9.             Iterator i = list.iterator();
  10.             Collections.reverse(list);
  11.      Collections.sort(list);
  12.             while(i.hasNext())
  13.          System.out.print(i.next() + " ");
  14.         }
  15.     }
a) 2 8 5 1
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?
  1.     import java.util.*;
  2.     class Collection_iterators {
  3.         public static void main(String args[]) {
  4.             LinkedList list = new LinkedList();
  5.             list.add(new Integer(2));
  6.             list.add(new Integer(8));
  7.             list.add(new Integer(5));
  8.             list.add(new Integer(1));
  9.             Iterator i = list.iterator();
  10.             Collections.reverse(list);
  11.      Collections.shuffle(list);
  12.             i.next();
  13.             i.remove();
  14.             while(i.hasNext())
  15.          System.out.print(i.next() + " ");
  16.         }
  17.     }
a) 2 8 5
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.

Related

C Programming Questions and Answers on Structures and Functions for Freshers

1. What is the output of this C code? #include struct student { char *name; }; struct student s; struct student fun(void) { s.name = "new...

CSS Multiple Choice Questions & Answers on Styling Tables for Freshers

1. Which of the following element is used within the table element to define a caption? a) b) <caption> c) d) Answer: b  2. Which of the following property is used ...

Java Multiple Choice Questions & Answers on Java.util package Dictionary, Hashtable & Properties for Freshers

1. Which of these class object uses key to store value? a) Dictionaryb) Mapc) Hashtabled) All if the mentioned Answer: d Explanation: Dictionary, Map & Hashtable all implement Map interface h...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item