LearnFrenzy
Sign in Take a free test

HashMap and TreeMap · Exercise 2 of 4

HashMap and TreeMap Predict the Output

  • 20 questions
  • Page 1 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 1 Easy

What is the output of the following Java program?

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

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> m = new HashMap<>();
        Integer a = m.put("x", 1);
        Integer b = m.put("x", 2);
        Integer c = m.put("y", 3);
        System.out.println(a + " " + b + " " + c + " " + m.get("x") + " " + m.size());
    }
}
A null 1 null 2 2
B null null null 2 3
C 1 2 3 2 2
D null 1 null 1 2
Question page & discussion
Question 2 Easy

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<Character, Integer> f = new TreeMap<>();
        for (char c : "mississippi".toCharArray()) f.merge(c, 1, Integer::sum);
        System.out.println(f);
    }
}
A {i=4, m=1, p=2, s=4}
B {m=1, i=4, s=4, p=2}
C {i=4, s=4, p=2, m=1}
D {s=4, i=4, p=2, m=1}
Question page & discussion
Question 3 Medium

What is the output of the following Java program?

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

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> m = new HashMap<>();
        m.put("a", null);
        System.out.println(m.getOrDefault("a", 5) + " " + m.getOrDefault("b", 5) + " " + m.containsKey("a") + " " + m.putIfAbsent("a", 7) + " " + m.get("a"));
    }
}
A null 5 true null 7
B 5 5 true null 7
C null 5 true 7 7
D 5 5 false null 7
Question page & discussion
Question 4 Hard

What is the output of the following Java program?

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

public class Main {
    public static void main(String[] args) {
        Map<Integer, String> m = new HashMap<>();
        m.put(9, "nine"); m.put(3, "three"); m.put(7, "seven"); m.put(1, "one");
        System.out.println(m);
    }
}
A {1=one, 3=three, 7=seven, 9=nine}
B {9=nine, 3=three, 7=seven, 1=one}
C {9=nine, 7=seven, 3=three, 1=one}
D The order is random on every run
Question page & discussion
Question 5 Easy

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) {
        String[][] expenses = {{"food", "250"}, {"travel", "1200"}, {"food", "180"}, {"books", "499"}, {"travel", "300"}, {"food", "70"}};
        Map<String, Integer> total = new TreeMap<>();
        for (String[] e : expenses) total.merge(e[0], Integer.parseInt(e[1]), Integer::sum);
        System.out.println(total + " " + total.values().stream().mapToInt(Integer::intValue).sum());
    }
}
A {books=499, food=500, travel=1500} 2499
B {food=500, travel=1500, books=499} 2499
C {books=499, food=70, travel=300} 869
D {books=499, food=500, travel=1500} 2500
Question page & discussion
Question 6 Medium

What is the output of the following Java program?

Main.java
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        TreeMap<Integer, String> t = new TreeMap<>();
        for (int k : new int[]{10, 20, 30, 40}) t.put(k, "v" + k);
        System.out.println(t.floorKey(25) + " " + t.ceilingKey(25) + " " + t.higherKey(30) + " " + t.lowerKey(10) + " " + t.floorKey(5) + " " + t.ceilingEntry(40));
    }
}
A 20 30 40 null null 40=v40
B 30 20 40 null 10 40=v40
C 20 30 30 10 null 40=v40
D 20 30 40 null 10 null
Question page & discussion
Question 7 Medium

What is the output of the following Java program?

Main.java
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        TreeMap<String, Integer> t = new TreeMap<>();
        for (String s : new String[]{"apple", "banana", "cherry", "date", "fig"}) t.put(s, s.length());
        System.out.println(t.headMap("cherry") + " " + t.tailMap("date") + " " + t.subMap("b", "d") + " " + t.firstKey() + " " + t.lastEntry());
    }
}
A {apple=5, banana=6} {date=4, fig=3} {banana=6, cherry=6} apple fig=3
B {apple=5, banana=6, cherry=6} {fig=3} {banana=6, cherry=6, date=4} apple fig=3
C {apple=5, banana=6} {date=4, fig=3} {} apple fig=3
D {apple=5} {date=4, fig=3} {banana=6} apple date=4
Question page & discussion
Question 8 Medium

What is the output of the following Java program?

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

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> m = new LinkedHashMap<>(16, 0.75f, true);
        m.put("a", 1); m.put("b", 2); m.put("c", 3);
        m.get("a");
        m.put("b", 20);
        m.getOrDefault("zz", 0);
        System.out.println(m.keySet());
    }
}
A [c, a, b]
B [a, b, c]
C [b, a, c]
D [c, b, a]
Question page & discussion
Question 9 Hard

What is the output of the following Java program?

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

public class Main {
    public static void main(String[] args) {
        Map<Integer, String> cache = new LinkedHashMap<>(16, 0.75f, true) {
            private static final long serialVersionUID = 1L;
            @Override protected boolean removeEldestEntry(Map.Entry<Integer, String> e) { return size() > 2; }
        };
        cache.put(1, "one");
        cache.put(2, "two");
        cache.get(1);
        cache.put(3, "three");
        cache.get(2);
        cache.put(4, "four");
        System.out.println(cache);
    }
}
A {3=three, 4=four}
B {1=one, 4=four}
C {2=two, 4=four}
D {3=three, 1=one}
Question page & discussion
Question 10 Hard

What is the output of the following Java program?

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

public class Main {
    public static void main(String[] args) {
        List<String> key = new ArrayList<>(List.of("a"));
        Map<List<String>, String> m = new HashMap<>();
        m.put(key, "first");
        key.add("b");
        System.out.println(m.get(key) + " " + m.get(List.of("a")) + " " + m.size() + " " + m.containsValue("first"));
    }
}
A null null 1 true
B first null 1 true
C first first 1 true
D null first 1 true
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links