LearnFrenzy
Sign in Take a free test

HashMap and TreeMap · Exercise 3 of 4

HashMap and TreeMap Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Each Java program misuses a Map in a way javac rejects. Choose the option that names the real problem.

Question 1 Easy

The program below does not compile. Which statement correctly describes the problem?

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

public class Main {
    public static void main(String[] args) {
        Map<String> phone = new HashMap<>();
        System.out.println(phone);
    }
}
A HashMap needs a capacity
B Map takes two type arguments – the key type and the value type, e.g. Map<String, String>
C Map must be imported from java.lang
D println cannot print maps
Question page & discussion
Question 2 Easy

The program below does not compile. Which statement correctly describes the problem?

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

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> visits = new HashMap<>();
        visits.put("home");
        System.out.println(visits);
    }
}
A "home" must be a char
B put needs both a key and a value, e.g. visits.put("home", 1) – or use merge to count
C visits must be final
D HashMap has no put method
Question page & discussion
Question 3 Easy

The program below does not compile. Which statement correctly describes the problem?

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

public class Main {
    public static void main(String[] args) {
        Map<String, Double> prices = new HashMap<>();
        prices.add("tea", 20.0);
        System.out.println(prices);
    }
}
A 20.0 must be a float
B Map has no add method; entries are inserted with put(key, value)
C prices must be a TreeMap
D Double cannot be a value
Question page & discussion
Question 4 Medium

The program below does not compile. Which statement correctly describes the problem?

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

public class Main {
    public static void main(String[] args) {
        Map<Integer, String> ranks = new TreeMap<>();
        ranks.put(2, "silver"); ranks.put(1, "gold");
        System.out.println(ranks.firstKey());
    }
}
A TreeMap cannot hold Integers
B firstKey belongs to SortedMap/TreeMap, not Map; declare the variable as TreeMap or SortedMap to use it
C put must come after firstKey
D firstKey needs an argument
Question page & discussion
Question 5 Easy

The program below does not compile. Which statement correctly describes the problem?

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

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> age = new HashMap<>();
        age.put("Asha", 21);
        String a = age.get("Asha");
        System.out.println(a);
    }
}
A get needs a default
B get returns an Integer (the value type), which cannot be assigned to a String; use Integer a or String.valueOf(...)
C Asha must be a key object
D age must be a TreeMap
Question page & discussion
Question 6 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> stock = Map.of("pen", 10, "ink", 4);
        stock.forEach(item -> System.out.println(item));
    }
}
A Map.of maps cannot be iterated
B Map.forEach takes a BiConsumer, so the lambda needs two parameters: (item, qty) -> …
C println cannot be used in a lambda
D forEach must return a value
Question page & discussion
Question 7 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        TreeMap<int, double> rates = new TreeMap<>();
        rates.put(1, 6.5);
        System.out.println(rates);
    }
}
A TreeMap cannot store numbers
B Type arguments must be reference types: use TreeMap<Integer, Double>, and autoboxing converts the values
C rates needs a Comparator
D 6.5 must be 6.5f
Question page & discussion
Question 8 Medium

The program below does not compile. Which statement correctly describes the problem?

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<>(Map.of("old", 1));
        for (Map.Entry<String, Integer> e : m.entrySet()) {
            e.setKey("new");
        }
        System.out.println(m);
    }
}
A entrySet cannot be looped
B Map.Entry has setValue but no setKey – keys cannot be changed in place; remove the old key and put the new one
C e must be final
D HashMap entries are immutable entirely
Question page & discussion
Question 9 Hard

The program below does not compile. Which statement correctly describes the problem?

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

public class Main {
    public static void main(String[] args) {
        Map<String, List<Integer>> scores = new HashMap<>();
        scores.computeIfAbsent("Asha", k -> 0).add(90);
        System.out.println(scores);
    }
}
A add cannot take 90
B The mapping function must produce the value type List<Integer>, but it returns 0; write k -> new ArrayList<>()
C computeIfAbsent needs three arguments
D scores must be a TreeMap
Question page & discussion
Question 10 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, String> config = new Map<>();
        config.put("env", "prod");
        System.out.println(config);
    }
}
A put needs a third argument
B Map is an interface; create an implementation such as new HashMap<>() or new TreeMap<>()
C The diamond operator cannot be used with maps
D config must be static
Question page & discussion
Up next · Exercise 4HashMap and TreeMap Programs10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links