Menu
Topics Index
...
`

WeakHashMap


If you need explanation Read this topic

If you need Answer Take test on this topic

WeakHashMap
What will be the output of following program?
import java.util.*;

public class WeakHashMapTest {
    public static void main(String[] args) throws InterruptedException {
        WeakHashMap<Grill, String> s = new WeakHashMap<Grill, String>();
        Grill g1 = new Grill();
        Grill g2 = new Grill();
        Grill g3 = new Grill();
        s.put(g1, "Grill 1");
        s.put(g2, "Grill 2");
        s.put(g3, "Grill 3");
        cleanAndPrintSize(s);
        s.put(null, null);
        cleanAndPrintSize(s);
        g1 = g2 = g3 = null;
        cleanAndPrintSize(s);
    }
    
    public static void cleanAndPrintSize(WeakHashMap<Grill, String> s) throws InterruptedException {
        System.gc();
        Thread.sleep(5000);
        System.out.print(s.size() + "^");
    }
}

class Grill {
}

A. 3^4^4^
B. 3^3^3^
C. 3^4^1^
D. Some other output
E. Compilation Error or Runtime Error
Topic: Java WeakHashMap Class

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.

D is the answer.sm othr ouput such as 3^0^ .as der r 3 strings and we have asgnd g1 2 3 as null

Posted by Shabaaz Shaikz    2013-12-24 12:49:20


Ans is C, because In main method, calling cleanAndPrintSize() method at 1st time, s object is having 3 Grill values. and at 2nd time, s object is adding one more value contains null so s obj size becomes 4, and at 3rd time , all Grill objets becomes null, so s is having single value.

Posted by Mallikarjuna Rao    2013-12-24 15:19:37


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

Posted by Merit Campus    2013-12-25 03:54:22


© meritcampus 2019

All Rights Reserved.

Open In App