LearnFrenzy
Sign in Take a free test

HashMap and TreeMap · Exercise 2 of 4

HashMap and TreeMap Predict the Output

  • 20 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each Java program and choose what it prints. Every program was compiled and run on Java 21, so the answers are the real output.

Question 11 Easy

What is the output of the following Java program?

Main.java
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        Map<Character, List<String>> groups = new TreeMap<>();
        for (String n : List.of("Ravi", "Asha", "Rohit", "Meena", "Arjun", "Riya")) {
            groups.computeIfAbsent(n.charAt(0), k -> new ArrayList<>()).add(n);
        }
        System.out.println(groups + " " + groups.get('R').size());
    }
}
A {A=[Asha, Arjun], M=[Meena], R=[Ravi, Rohit, Riya]} 3
B {R=[Ravi, Rohit, Riya], A=[Asha, Arjun], M=[Meena]} 3
C {A=[Arjun, Asha], M=[Meena], R=[Ravi, Riya, Rohit]} 3
D {A=[Arjun], M=[Meena], R=[Riya]} 1
Question page & discussion
Question 12 Medium

What is the output of the following Java program?

Main.java
import java.util.Map;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> stock = new TreeMap<>(Map.of("pen", 3, "book", 1, "bag", 5));
        for (String sold : new String[]{"book", "pen", "lamp", "pen"}) {
            stock.computeIfPresent(sold, (k, v) -> v > 1 ? v - 1 : null);
        }
        stock.compute("lamp", (k, v) -> v == null ? 10 : v + 10);
        System.out.println(stock);
    }
}
A {bag=5, lamp=10, pen=1}
B {bag=5, book=0, lamp=10, pen=1}
C {bag=5, pen=1}
D {bag=5, book=1, lamp=10, pen=1}
Question page & discussion
Question 13 Medium

What is the output of the following Java program?

Main.java
import java.util.Map;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> marks = new TreeMap<>(Map.of("Asha", 91, "Ravi", 35, "Meena", 78, "John", 28, "Zoya", 40));
        marks.entrySet().removeIf(e -> e.getValue() < 40);
        marks.values().removeIf(v -> v == 40);
        marks.keySet().remove("Meena");
        System.out.println(marks);
    }
}
A {Asha=91}
B {Asha=91, Meena=78}
C {Asha=91, Meena=78, Zoya=40}
D ConcurrentModificationException
Question page & discussion
Question 14 Medium

What is the output of the following Java program?

Main.java
import java.util.HashMap;
import java.util.Map;

record Emp(int id) { }

public class Main {
    public static void main(String[] args) {
        Map<Emp, String> m = new HashMap<>();
        m.put(new Emp(1), "Asha");
        m.put(new Emp(1), "Asha K");
        m.put(new Emp(2), "Ravi");
        Map<String, Integer> s = new HashMap<>();
        s.put(new String("pin"), 1);
        s.put("pin", 2);
        System.out.println(m.size() + " " + m.get(new Emp(1)) + " " + s.size() + " " + s.get("pin"));
    }
}
A 2 Asha K 1 2
B 3 Asha 2 1
C 2 Asha 1 1
D 3 Asha K 2 2
Question page & discussion
Question 15 Medium

What is the output of the following Java program?

Main.java
import java.util.Comparator;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        TreeMap<String, Integer> byLen = new TreeMap<>(Comparator.comparing(String::length).thenComparing(Comparator.naturalOrder()));
        for (String s : new String[]{"kiwi", "fig", "banana", "plum", "apple"}) byLen.put(s, s.length());
        TreeMap<Integer, String> desc = new TreeMap<>(Comparator.reverseOrder());
        desc.put(1, "a"); desc.put(3, "c"); desc.put(2, "b");
        System.out.println(byLen.keySet() + " " + desc + " " + desc.firstKey());
    }
}
A [fig, kiwi, plum, apple, banana] {3=c, 2=b, 1=a} 3
B [apple, banana, fig, kiwi, plum] {1=a, 2=b, 3=c} 1
C [fig, plum, kiwi, apple, banana] {3=c, 2=b, 1=a} 3
D [fig, kiwi, plum, apple, banana] {3=c, 2=b, 1=a} 1
Question page & discussion
Question 16 Easy

What is the output of the following Java program?

Main.java
import java.util.EnumMap;
import java.util.Map;

enum Size { S, M, L, XL }

public class Main {
    public static void main(String[] args) {
        Map<Size, Integer> orders = new EnumMap<>(Size.class);
        for (String s : "L M XL M S L L".split(" ")) orders.merge(Size.valueOf(s), 1, Integer::sum);
        System.out.println(orders);
    }
}
A {S=1, M=2, L=3, XL=1}
B {L=3, M=2, XL=1, S=1}
C {L=3, M=2, S=1, XL=1}
D {XL=1, L=3, M=2, S=1}
Question page & discussion
Question 17 Medium

What is the output of the following Java program?

Main.java
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        Map<String, String> capitalOf = new LinkedHashMap<>();
        capitalOf.put("MH", "Mumbai"); capitalOf.put("KA", "Bengaluru"); capitalOf.put("XX", "Mumbai");
        Map<String, String> stateOf = new TreeMap<>();
        capitalOf.forEach((state, city) -> stateOf.put(city, state));
        System.out.println(stateOf);
    }
}
A {Bengaluru=KA, Mumbai=XX}
B {Bengaluru=KA, Mumbai=MH}
C {Mumbai=MH, Bengaluru=KA, Mumbai=XX}
D {KA=Bengaluru, MH=Mumbai}
Question page & discussion
Question 18 Medium

What is the output of the following Java program?

Main.java
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        try {
            Map<String, Integer> m = Map.of("a", 1, "b", 2, "a", 3);
            System.out.println(m.size());
        } catch (IllegalArgumentException e) {
            System.out.print("IAE ");
        }
        try {
            Map.of("a", null);
        } catch (NullPointerException e) {
            System.out.println("NPE");
        }
    }
}
A IAE NPE
B 2 NPE
C 3 NPE
D IAE null
Question page & discussion
Question 19 Medium

What is the output of the following Java program?

Main.java
import java.util.Map;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        Map<String, Map<String, Integer>> marks = new TreeMap<>();
        String[][] data = {{"Asha", "Maths", "91"}, {"Ravi", "Maths", "67"}, {"Asha", "Physics", "84"}, {"Ravi", "Physics", "72"}, {"Asha", "Maths", "95"}};
        for (String[] d : data) marks.computeIfAbsent(d[0], k -> new TreeMap<>()).merge(d[1], Integer.parseInt(d[2]), Math::max);
        System.out.println(marks + " " + marks.get("Asha").get("Maths"));
    }
}
A {Asha={Maths=95, Physics=84}, Ravi={Maths=67, Physics=72}} 95
B {Asha={Maths=91, Physics=84}, Ravi={Maths=67, Physics=72}} 91
C {Asha={Maths=186, Physics=84}, Ravi={Maths=67, Physics=72}} 186
D {Ravi={Maths=67, Physics=72}, Asha={Maths=95, Physics=84}} 95
Question page & discussion
Question 20 Medium

What is the output of the following Java program?

Main.java
import java.util.Map;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        TreeMap<Integer, String> events = new TreeMap<>(Map.of(1030, "standup", 900, "email", 1400, "review", 1200, "lunch"));
        StringBuilder sb = new StringBuilder();
        Map.Entry<Integer, String> e;
        while ((e = events.pollFirstEntry()) != null) {
            sb.append(e.getKey()).append(':').append(e.getValue()).append(' ');
            if (e.getKey() >= 1200) break;
        }
        System.out.println(sb.toString().trim() + " | left " + events);
    }
}
A 900:email 1030:standup 1200:lunch | left {1400=review}
B 900:email 1030:standup | left {1200=lunch, 1400=review}
C 1400:review | left {}
D 900:email 1030:standup 1200:lunch 1400:review | left {}
Question page & discussion
Up next · Exercise 3Find the Error10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links