Menu
Topics Index
...
`

Collections creating Array list While loop


If you need explanation Read this topic

If you need Answer Take test on this topic

Collections creating Array list While loop
What will be the output of the following program?
import java.util.*;

public class LoopTest {
    public static void main(String... l) {
        List<String> li = new ArrayList<String>();
        li.add("Hai");
        li.add("Hello");
        ListIterator i = li.listIterator();
        while (i.hasNext()) {
            i.add("Hi"); // Line 1
                }
        System.out.println(li);
    }
}

A. [Hi, Hai, Hi, Hello]
B. [Hai, Hello]
C. [Hai, Hello, Hi, Hi]
D. Some other output
E. 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.

e.
because Main class should contain
method: public static void main
(String[] args)

Posted by Shubham Bansal    2014-10-15 03:47:55


E

Posted by Arul Palanisamy    2014-10-15 03:55:53


Answer is E.jvm will throw outOfMemory error because it keeps on adding "hi" string to the list as the list traversal never ends because  hasNext() method in the if condition just returns true or false depending on whether there are elements in the list but it does not advance the cursor position forward.add method of LitIterator  inserts immediately before the element that would be returned by next(), so the loop never ends until it runs out of heap space

Posted by Shashanka Mogaliraju    2014-10-15 05:41:17


Ans is E...

Here we are dealing with List interface and ArrayList class

ArrayList is an class which implements List interface...here we create an generic ArrayList which stores only Homogenious elements means only String type..
here we didnt mention any size than the default size is 10...
her we are adding to String objects as "Hai" and "Hello"

next we are getting ListIterator which is applicable to Only all classes of List Interface..by using this cursor we can move forward,backword,we can remove,we can add..

in while loop we are adding elements to the list by using the add() here hasNext() alwasy return true bca Everty time we are adding so that it wil never give false..
coming to ArrayList wen it reaches its default size than new Array wil create with Huge Size and all elements wil get copied into this new List and old list wil be Eligible for Garbage Collection..the huge size is calculated Using CurretSize*(3/2)+1
10*(3/2)+1=16
16*(3/2)+1=25 so on..
here we are adding every time so outof memorr error wil give

Posted by Uday Kumar    2014-10-15 15:23:49


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Šâí Râm for the 'Popular Comment'. The 'lucky liker' is Kon Bros Love Study. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-10-16 03:56:40


© meritcampus 2019

All Rights Reserved.

Open In App