Menu
Topics Index
...
`

Collections1897


If you need explanation Read this topic

If you need Answer Take test on this topic

Collections1897
What will be the output of the following program?
import java.util.*;

public class RemoveThis {
    public static void main(String[] args) {
        List xl = new ArrayList();
        xl.add("A");    xl.add("B");
        xl.add("C");    xl.add("D");
        xl.add("E");    xl.add("F");
        xl.add("G");    xl.add("H");
        xl.add("I");    xl.add("J");
        xl.add("K");

        ListIterator lt = xl.listIterator();
        lt.next();    lt.next();
        lt.next();    lt.next();
        lt.next();    lt.next();
        lt.remove();    lt.previous();
        lt.previous();    lt.next();
        lt.remove();    lt.previous();
        lt.next();    lt.previous();
        lt.remove();
        System.out.print(xl);
    }
}

A. [A, C, E, G, H, I, J, K]
B. [A, B, D, F, G, H, I, J, K]
C. [B, D, F, G, H, I, J, K]
D. [A, B, E, G, H, I, J, K]
E. Some other output
F. Compilation Error or 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 D. First all the elements are added to the arraylist. next() gives the next element of the current element of the list. For the first time next() returns the first element. So after six next() remove() removes "F" and two times previous() and one next() gives "D" so it is removed by the remove() method and so on.

Posted by Nikhil Sulibhavi    2014-05-05 08:56:18


ans is D , after 6 iterations it removes the element "f", after pre,pre,next, stmt it removes "d" element in the arraylist,  finally it removes the element "c" in a array which is pre,next,pre,remove stmt.

Posted by Saikiran Vavilala    2014-05-05 09:09:08


Congratulations Nikhil Sulibhavi. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-05-06 04:22:28


© meritcampus 2019

All Rights Reserved.

Open In App