Menu
Topics Index
...
`

Creating ArrayList and Iterator for it


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating ArrayList and Iterator for it
What will be the output of the following program?
import java.util.*;
public class Capital {
    public static void main(String args[]) {
        List mylist = new ArrayList();
        int[] val = {15, 5565, 646, 233, 100};
        for (int element : val)
            mylist.add(new Integer(element));
        System.out.println(mylist);
        Iterator iterator = mylist.iterator();
        int i = mylist.size();
        while (iterator.hasNext()) {
            Integer element = (Integer) iterator.next();
            int value = element.intValue();
            i--;
            if ((value < 100) || mylist.get(i).equals(100))
                iterator.remove();
        }
        System.out.println(mylist);
    }
}

A. [15, 5565, 646, 233, 100]
[15, 5565, 646, 233]
B. [15, 5565, 646, 233, 100]
[5565, 646, 233]
C. [15, 5565, 646, 233, 100]
[]
D. Some other output
E. Compilation Error or Runtime Error
Topic: Java Iterator

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 B.. here we are creating a list and ptinting that aand then testing the list by conditions value<100 or mylist.get(i).equals(100) So 100 and 15 vl be removed from the list..

Posted by Om Prakash Om    2014-03-26 13:08:20


Ans is E. Compilation error occurs. We are creating an empty list i.e., without specifying any type of elements that the list should take and adding Integers to the list, which results in an error.

Posted by Nikhil Sulibhavi    2014-03-26 13:32:37


Ans is C: 1s we are coping all elements into mylist and we are printg it..so it wl print same..now v are takng size of mylist into i so it wl give 5..using iterator we wil get 1st element so it is less than 100 so it wl removed..nxt it wl get 5565 1st cndtion wl fail so nxt cndtion wl be true 100.equal(100) so it wl tru..evry tm this cndtion wl true so all elemnts removed at last empty list wl print

Posted by Uday Kumar    2014-03-26 15:06:18


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

Posted by Merit Campus    2014-03-27 04:20:57


© meritcampus 2019

All Rights Reserved.

Open In App