Menu
Question Index
...

What will be the output of the following program?

import java.util.*;

public class CollectionsTest {

    public static void main(String[] args) {
        List<A> l = new ArrayList<A>();
        l.add(new A(5, 4));
        l.add(l.get(0));
        l.add(new A(6, 0));
        l.add(new A(5, 4));
        l.add(new A(6));
        Set<A> s = new HashSet<A>();
        s.addAll(l);
        System.out.print("l = " + l.size() + " s = " + s.size());
    }
}

class A {
    int i, j;
    public A(int i, int j) { this.i = i; this.j = j; }
    public A(int i) { this.i = i; }
}


l = 5 s = 4
l = 5 s = 5
l = 3 s = 3
l = 5 s = 3
Compilation Error or Runtime Error

Doubts

Problems

Topic: HashSet Class In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App