Menu
Topics Index
...
`

Collections creating Array list


If you need explanation Read this topic

If you need Answer Take test on this topic

Collections creating Array list
What will be the output of the following program?
import java.util.*;
public class Copy {
    public static void main(String[] args) {
        List<String> list1 = new ArrayList<String>();
        list1.add("M");
        list1.add("E");
        List<String> list2 = list1;
        list1.add("R");
        list1.add("I");
        list1.add("T");
        System.out.format("%s", list1);
        System.out.format("%s", list2);
    }
}

A. [M, E, R, I, T]
B. [M, E, R, I, T][M, E]
C. [M, E, R, I, T][R, I, T]
D. [M, E, R, I, T][M, E, R, I, T]
E. Compilation Error or Runtime Error
Topic: Java ArrayList

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.

answer D because M,E,R,I,T are added to list1.and list2 is refers to same list1.hence both refers to list with elements "M","E","R","I","T".

Posted by Mamatha Jyothi    2013-07-12 08:56:37


ans is D....since both list1,list 2 referes to same list object both contain same values....MERIT

Posted by Santosh Munugota    2013-07-12 09:31:36


Congratulations Mamatha Jyothi, you are this dose winner. Please send us your mobile details and we will process the recharge asap.

Posted by Merit Campus    2013-07-13 05:29:14


© meritcampus 2019

All Rights Reserved.

Open In App