Menu
Question Index
...

What will be the output of the following program?

import java.util.*;
public class Test {
    public static void main(String[] args) {
        Map hashMap = new HashMap();
        Map weakHashMap = new WeakHashMap();
        String keyHashMap = new String("Map");
        String keyWeakHashMap = new String("WeakHashMap");
        hashMap.put(keyHashMap, "Merit");
        weakHashMap.put(keyWeakHashMap, "Campus");
        System.gc();
        System.out.println(hashMap.get("Map") + " " + weakHashMap.get("WeakHashMap"));
        keyHashMap = null;
        keyWeakHashMap = null;
        System.gc();
        System.out.println(weakHashMap.get("WeakHashMap") + "  " + hashMap.get("Map") );
    }
}


Merit Campus
Merit  Campus
Merit Campus
null  Merit
Merit Campus
Campus Merit
Merit null
null Merit
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Collection Framework 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