Menu
Question Index
...

What will be the output of the following program?

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class Test {

    public static void main(String[] args) {
        Map<String, String> myMap = new ConcurrentHashMap<String, String>();
        myMap.put("1", "1");
        myMap.put("2", "1");
        myMap.put("3", "1");
        myMap.put("4", "1");
        myMap.put("5", "1");
        myMap.put("6", "1");
        Iterator<String> it = myMap.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            if (key.equals("3"))
                myMap.put(key + "new", "new3");
        }
        System.out.println("after iterator: " + myMap);
        myMap = new HashMap<String, String>();
        myMap.put("1", "1");
        myMap.put("2", "1");
        myMap.put("3", "1");
        myMap.put("4", "1");
        myMap.put("5", "1");
        myMap.put("6", "1");
        Iterator<String> it1 = myMap.keySet().iterator();
        while (it1.hasNext()) {
            String key = it1.next();
            if (key.equals("3")) {
                myMap.put(key + "new", "new3");
                break;
            }
        }
        System.out.println("Hash Map after iterator: " + myMap);
    }
}


after iterator: {1=1, 3new=new3, 5=1, 6=1, 3=1, 4=1, 2=1}
Hash Map after iterator: {3=1, 2=1, 1=1, 3new=new3, 6=1, 5=1, 4=1}
after iterator: {1=1, 3new=new3, 5=1, 6=1, 3=1, 4=1, 2=1}
Hash Map after iterator: {3=1, 2=1, 1=1, new=3new, 6=1, 5=1, 4=1}
Some other output
Output can not be determined
Compilation Error or Runtime Error

Doubts

Problems

Topic: Other Advanced Topics 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