Topics Index

Enjoy Fully

If you need explanation Read this topic

If you need Answer Take test on this topic

Enjoy Fully
What will be the output of the following program?
import java.util.*;
public class EnjoyFully {
    public static void main(String[] args) {
        Integer[] a = new Integer[]{1, 2, 3, 4, 5};
        Integer[] b = new Integer[]{2, 3, 1, 0, 5};
        Character[] c = new Character[]{'a', 'b', 'c', 'd', 'e'};
        Character[] d = new Character[]{'e', 'a', 'd', 'C', 'a'};
        Boolean[] e = new Boolean[]{true, false, true, true, false};
        Boolean[] f = new Boolean[]{true, false, false, true, false};
        Set setA = new HashSet<>(Arrays.asList(a));
        Set setB = new HashSet<>(Arrays.asList(b));
        Set setC = new HashSet<>(Arrays.asList(c));
        Set setD = new HashSet<>(Arrays.asList(d));
        List listE = new ArrayList<>(Arrays.asList(e));
        List listF = new ArrayList<>(Arrays.asList(f));
        Set setG = new HashSet<>(Arrays.asList(e));
        Set setH = new HashSet<>(Arrays.asList(f));
        System.out.print(setB + ", ");
        System.out.print(setC + ", ");
        System.out.print(setG + ", ");

A. [0], [b, c], [], []
B. [0], [b], [], []
C. [0], [b, c], [], [false]
D. [0], [b, c], [false], [false]
E. Some other output
F. Compilation Error or Runtime Error
Topic: Collection Framework 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.

Answer is B.RemoveAll Removes from the  set all of its elements that are contained in the specified collection

Posted by Shashanka Mogaliraju    2014-08-22 13:05:40

Ans is A. For the first two sets only 0 is not contained in setA so it is not removed from setB. In the next sets, b and c are not there in D.(D contains 'C' but not 'c') so b and c remains. For the remaining sets, every element of the setG is contained in setH. So empty sets are formed.

Posted by Nikhil Sulibhavi    2014-08-22 13:14:50

ANS IS E Some other output

[ ],[ ],[ ],[ ]

Elements of setB are removed by the statement setB.removeAll(setA)
similarly elements of set C,G, and E are removed

Posted by Mânïshå Mùlchåndânï    2014-08-23 18:31:47

ans b.
1st sop prints [0]
setA elemnts frm setB removed remaining it ve ly 0.
setD char elemnt remvd frm setC.
setC ly ve 'b'
listE and listF both ve true false.
so liste all elemnts removd.
similarly setG all elemnts removed.
empty set get printd

Posted by Maheshwari Natarajan    2014-08-24 11:00:42

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

Posted by Merit Campus    2014-08-25 07:26:20

© meritcampus 2019

All Rights Reserved.

Open In App