Topics Index

Singleton Collections Test2138

If you need explanation Read this topic

If you need Answer Take test on this topic

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

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.

C : first it checks if Merit is present in meritlist it is false and hence prints the character 'J' and same it does. now when it prints the indexof sublist that is meritlist then it prints the index 1...hence JA1 will be printed.

Posted by Sana Ferdouse    2014-04-11 09:08:31

Ans is A. every time no character is present in the given List or Set , So all time it returns false and Java is printed. Next MeritSet's first occurence in meritList is printed that is 0.

Posted by Nikhil Sulibhavi    2014-04-11 10:51:59

Ans is A list comtain merit and checks whether character is present in list and prints J A V A since it is  not matched and 0 is printed as merit is matched

Posted by Naga Sinst    2014-04-11 12:12:27

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

Posted by Merit Campus    2014-04-14 05:03:34

© meritcampus 2019

All Rights Reserved.

Open In App