Knowee
Questions
Features
Study Tools

What is the output of the following code?import java.util.*;public class Main { public static void main(String[] args) { Map map = new HashMap<>(); map.put("A", 1); map.put("B", 2); map.put("C", 3); for (Map.Entry entry : map.entrySet()) { if (entry.getValue() == 2) { map.remove(entry.getKey()); } } System.out.println(map); }}a){A=1, C=3}b)IllegalArgumentExceptionc){A=1, B=2, C=3}d){B=2, A=1, C=3}e)ConcurrentModificationException

Question

What is the output of the following code?import java.util.*;public class Main { public static void main(String[] args) { Map map = new HashMap<>(); map.put("A", 1); map.put("B", 2); map.put("C", 3); for (Map.Entry entry : map.entrySet()) { if (entry.getValue() == 2) { map.remove(entry.getKey()); } } System.out.println(map); }}a){A=1, C=3}b)IllegalArgumentExceptionc){A=1, B=2, C=3}d){B=2, A=1, C=3}e)ConcurrentModificationException

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the code will be e)ConcurrentModificationException.

This is because you are trying to remove an item from a map while iterating over it. This is not allowed in Java and will throw a ConcurrentModificationException.

To avoid this, you could use an Iterator to safely remove items from the map while iterating. Here's how you could modify the code:

Iterator<Map.Entry> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
    Map.Entry entry = iterator.next();
    if (entry.getValue() == 2) {
        iterator.remove();
    }
}

With this modification, the output would be {A=1, C=3}.

This problem has been solved

Similar Questions

What will be the output of the following Java code?Code:import java.util.*;import java.util.Map.Entry;public class OrderValue{ public static void main(String a[]){ Map<String, Integer> names = new HashMap<String, Integer>(); names.put("Anne", 10); names.put("John", 35); names.put("Bob", 2); Set<Entry<String, Integer>> set = names.entrySet(); List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set); Collections.sort( list, new Comparator<Map.Entry<String, Integer>>() { public int compare( Map.Entry<String, Integer> order1, Map.Entry<String, Integer> order2) { return (order2.getValue()).compareTo( order1.getValue() ); } }); for(Map.Entry<String, Integer> entry:list){ System.out.print(" "+entry.getKey()); } }}

What is the output of the following program:import java.util.ArrayList;class Main { public static void main(String[] args){ ArrayList<String> animals = new ArrayList<>(); animals.add("Dog"); animals.add("Cat"); animals.add("Horse"); animals.remove(2); System.out.println("ArrayList: " + animals); }}Select one:a.ArrayList: [Dog, Cat, Horse]b.ArrayList: [Dog, Cat]c.ArrayList: [Dog]d.ArrayList: [Horse]

import java.util.*;        public class Test {              public static void main(String[] args) {                     Map<Set<String>, List<String>> m = new HashMap<>();                     ArrayList<String> list1 = new ArrayList<>();                     list1.add("30");                     list1.add("40");                                    ArrayList<String> list2 = new ArrayList<String>();                     list2.add("35");                     list2.add("45");                     Set<String> set1 = new HashSet<>();                     set1.add("ravi");                     set1.add("ravi");                     m.put(set1,list1);                     m.put(set1,list2);                     System.out.println(m);              }         }

What is the output of the following code?import java.util.*;public class Main { public static void main(String[] args) { List list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); Iterator iterator = list.iterator(); while (iterator.hasNext()) { Integer value = iterator.next(); if (value == 2) { iterator.remove(); } } System.out.println(list); }}a)[2, 3]b)[3, 2, 1]c)[1, 2, 3]d)[1, 3]

What is the output of the following Java program?

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.