Menu
Topics Index
...
`

Singleton Collections Test


If you need explanation Read this topic

If you need Answer Take test on this topic

Singleton Collections Test
What will be the output of the following program?
import java.util.*;
public class SingletonCollectionsTest {
    public static void main(String[] args) {
        Set<String> meritSet = Collections.singleton("Merit");
        List<String> meritList = Collections.singletonList("Merit");
        p(meritSet.contains(meritList), 'J');
        p(meritList.contains(meritSet), 'A');
        p(meritSet.containsAll(meritList), 'V');
        p(meritList.containsAll(meritSet), 'A');
        meritSet.add("Campus");
        meritList.addAll(meritSet);
        System.out.print(Collections.indexOfSubList(meritList, new ArrayList<String>(meritSet)));
    }
    private static void p(boolean condition, char c) {
        if (condition) {
            System.out.print(c);
        }
    }
}

A. JAVA0
B. VA1
C. JA1
D. Compilation Error
E. Runtime Error
Topic: Java Singleton

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. RUntime error ariases  as during compiling it may be right.but at run time
exception in thread "main" java.lang.UnsupportedOperationException  and java.util.AbstractCollection.add exception may happen to occur

Posted by Asad Ahmed    2013-12-27 14:14:11


Answer is E,The program generates a runtime exception because   unmodifiable view of set doesnot support add() and remove() methods,so run time error is generated at this line" meritSet.add( "campus ") ;"

Posted by Praveen Kumar    2013-12-27 14:15:39


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

Posted by Merit Campus    2013-12-30 03:44:26


© meritcampus 2019

All Rights Reserved.

Open In App