Menu
Topics Index
...
`

Collections creating Array list and List Iterator


If you need explanation Read this topic

If you need Answer Take test on this topic

Collections creating Array list and List Iterator
What will be the output of the following program?
import java.util.*;
public class TwoInOne {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add("M");    list.add("N");
        list.add("O");    list.add("P");
        list.add("Q");
        ListIterator listIt = list.listIterator();
        listIt.next();    listIt.next();
        listIt.next();    listIt.next();
        listIt.remove();    listIt.previous();
        listIt.previous();    listIt.remove();
        System.out.println(list);
    }
}

A. [M, O, Q]
B. [M, N, O, P, Q]
C. Some other output
D. Compilation Error
E. Runtime Error
Topic: List Iterator In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans Is A.. becpz first list contains [M,N,O,P,Q] elements then it moves to P and P vl be removed and to N and N vl be removed.. finally list contains [M,O,Q]..

Posted by Om Prakash Om    2013-07-10 08:52:27


ans is A....after four listIt.next() the cursor points to fourth ele in the list i.e, "P" this entry is removed and then the cursor moves two positions backwards after the next two statements now it points to second ele "N", it is removed...now list contains M,O,Q

Posted by Santosh Munugota    2013-07-10 08:53:41


Congratulations Om Prakash Om. You are this dose winner. Please send us your mobile details and we will process the recharge asap.

Posted by Merit Campus    2013-07-11 11:45:17


Thank u MC.. :)

Posted by Om Prakash Om    2013-07-21 14:07:34


© meritcampus 2019

All Rights Reserved.

Open In App