Menu
Question Index
...

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 {
}


3^4^4^
3^3^3^
3^4^1^
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Java WeakHashMap Class

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App