Menu
Topics Index
...
`

Add strings to list


If you need explanation Read this topic

If you need Answer Take test on this topic

Add strings to list
What will be the output of the following program?
import java.util.*;

public class AddCollection1
{
    public static void main(String args[])
    {
        List<String> list = new ArrayList<String>();
        Collections.addAll(list, "C", "C++", "C#", "JAVA");
        System.out.println(list);

        String[] strs = {".Net", "UNIX"};
        Collections.addAll(list, strs); //LINE A
        System.out.println(list);

        Collections.addAll(list);
        System.out.println(list);
    }
}

A. [C, C++, C#, JAVA]
[C, C++, C#, JAVA, .Net, UNIX]
[C, C++, C#, JAVA]
B. [C, C++, C#, JAVA]
[C, C++, C#, JAVA, .Net, UNIX]
[C, C++, C#, JAVA, .Net, UNIX]
C. [C, C++, C#, JAVA]
[C, C++, C#, JAVA, .Net, UNIX]
[C, C++, C#, JAVA, .Net, UNIX, C, C++, C#, JAVA, .Net, UNIX]
D. Compilation Error at LINE A because String[] can not be added to list
E. Compilation Error for some other reason 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.

B is ans.as 1st SOP collction list is prntd in nxt SOP strs is appnd t list nd same for 3rd SOP

Posted by Shabaaz Shaikz    2014-08-04 03:37:28


B.......list allows duplicates to follow

Posted by Arul Palanisamy    2014-08-04 05:38:00


Ans is C..first list is printed then .net and unix are added and then in the third statement list is copied and printed

Posted by Badam Swathi    2014-08-04 06:40:19


B

Posted by Ria Gupta    2014-08-04 12:03:18


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

Posted by Merit Campus    2014-08-05 04:48:12


© meritcampus 2019

All Rights Reserved.

Open In App