What does this code print?
Set<String> s = new HashSet<>();
boolean a = s.add("kiwi");
boolean b = s.add("kiwi");
boolean c = s.add("KIWI");
System.out.println(a + " " + b + " " + c + " " + s.size());
THINK FIRST·CODE LATER
What does this code print?
Set<String> s = new HashSet<>();
boolean a = s.add("kiwi");
boolean b = s.add("kiwi");
boolean c = s.add("KIWI");
System.out.println(a + " " + b + " " + c + " " + s.size());
What does this code print?
Set<Integer> linked = new LinkedHashSet<>();
Set<Integer> tree = new TreeSet<>();
int[] data = {5, 1, 3, 1, 9, 5};
for (int d : data) { linked.add(d); tree.add(d); }
System.out.println(linked + " " + tree);
What does this code print?
Set<String> s = new TreeSet<>();
s.add("pear"); s.add("Apple"); s.add("banana");
s.add("apple"); s.add("Banana");
System.out.println(s);
Tag overrides hashCode but not equals. What does the program print?
class Tag {
String name;
Tag(String n) { name = n; }
@Override public int hashCode() { return name.hashCode(); }
}
// in main:
Set<Tag> s = new HashSet<>();
s.add(new Tag("x"));
s.add(new Tag("x"));
System.out.println(s.size() + " " + s.contains(new Tag("x")));
A class Point overrides equals (comparing x and y) but does not override hashCode. Which statement best describes a HashSet<Point>?
Which of these is not required by the equals/hashCode contract?
What does this code print?
Set<String> s = new TreeSet<>(Comparator.comparingInt(String::length));
s.add("cat"); s.add("dog"); s.add("bird"); s.add("ox"); s.add("emu");
System.out.println(s.size() + " " + s);
Box does not implement Comparable. What happens?
class Box {
int w;
Box(int w) { this.w = w; }
}
// in main:
Set<Box> s = new TreeSet<>();
s.add(new Box(1));
System.out.println(s.size());
What does this code print?
TreeSet<Integer> t = new TreeSet<>(Arrays.asList(10, 20, 30, 40));
System.out.println(t.floor(25) + " " + t.ceiling(25) + " "
+ t.higher(40) + " " + t.lower(10) + " " + t.floor(30));
What does this code print?
TreeSet<Integer> t = new TreeSet<>(Arrays.asList(10, 20, 30, 40, 50));
System.out.println(t.headSet(30) + " " + t.tailSet(30) + " " + t.subSet(20, 40));
What does this code print?
Map<String, Integer> m = new HashMap<>();
m.put("a", 1);
Integer x = m.put("a", 2);
Integer y = m.put("b", 3);
System.out.println(x + " " + y + " " + m.get("a"));
What happens when this code runs?
Map<String, Integer> stock = new HashMap<>();
stock.put("pen", 4);
int n = stock.get("ink");
System.out.println(n);
What does this code print?
Map<String, Integer> freq = new TreeMap<>();
for (String w : "to be or not to be".split(" ")) {
freq.merge(w, 1, Integer::sum);
}
System.out.println(freq);
What does this code print?
Map<Integer, List<String>> groups = new TreeMap<>();
String[] words = {"ox", "cat", "ant", "be", "emu", "go"};
for (String w : words) {
groups.computeIfAbsent(w.length(), k -> new ArrayList<>()).add(w);
}
System.out.println(groups);
Which numbered line throws an exception?
Map<String, Integer> h = new HashMap<>();
h.put(null, 1); // line 1
h.put(null, 2); // line 2
Map<String, Integer> t = new TreeMap<>();
t.put("a", null); // line 3
t.put(null, 3); // line 4
What does this code print?
Map<String, Integer> m = new TreeMap<>();
m.put("a", 1); m.put("b", 2); m.put("c", 3);
m.keySet().remove("b");
m.values().remove(3);
System.out.println(m);
What happens when this code runs?
Map<String, Integer> m = new TreeMap<>();
m.put("a", 1); m.put("b", 2); m.put("c", 3);
for (String k : m.keySet()) {
if (k.equals("a")) m.remove(k);
}
System.out.println(m);
What does this code print?
Set<Integer> a = new TreeSet<>(Arrays.asList(1, 2, 3, 4));
Set<Integer> b = new TreeSet<>(Arrays.asList(3, 4, 5));
Set<Integer> u = new TreeSet<>(a);
u.addAll(b);
boolean changed = a.retainAll(b);
System.out.println(u + " " + a + " " + changed);
You store 1,000,000 student IDs and must answer "is this ID registered?" as fast as possible; the order is irrelevant. Which choice and expected cost per query are correct?
A program reads configuration lines key=value and must later write them back in the order they appeared, with later lines overriding earlier ones for the same key. Which map fits best?
What does this code print?
TreeMap<Integer, String> g = new TreeMap<>();
g.put(90, "A"); g.put(80, "B"); g.put(70, "C"); g.put(60, "D"); g.put(0, "F");
System.out.println(g.floorEntry(85).getValue() + g.floorEntry(60).getValue()
+ g.ceilingKey(91) + " " + g.headMap(70));
What does this code print?
Map<String, Integer> m = new LinkedHashMap<>();
m.put("z", 3); m.put("x", 1); m.put("y", 2);
int total = 0;
for (Map.Entry<String, Integer> e : m.entrySet()) {
e.setValue(e.getValue() * 10);
total += e.getValue();
}
System.out.println(m + " " + total);
P overrides equals (comparing x and y) and hashCode (31 * x + y) correctly. What does this code print?
Set<P> s = new HashSet<>();
P p = new P(1, 2);
s.add(p);
p.x = 5;
System.out.println(s.contains(p) + " " + s.contains(new P(1, 2)) + " " + s.size());
What does this code print?
Set<String> s = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
s.add("Java"); s.add("JAVA"); s.add("java");
System.out.println(s + " " + s.contains("jAvA"));
Does this code compile? If so, what does it print?
Set<String> s = new TreeSet<>();
s.add("b");
s.add("a");
System.out.println(s.first());
A class Isbn wraps a String code and overrides equals to compare codes, but not hashCode. A colleague says "it's fine, equals works". Explain concretely what goes wrong when Isbn objects are stored in a HashSet, and write a correct hashCode.
Compare HashMap, LinkedHashMap and TreeMap under three headings: iteration order, cost of get/put, and null keys. Then give one situation in which each is the best choice.
A programmer stores Student objects in new TreeSet<>(Comparator.comparingDouble(Student::getGpa)). After adding 40 students, size() is 31. Explain why, and show how to fix the comparator while still sorting by GPA.
You need a Map<String, List<String>> that groups course codes by department. Explain why this loop body fails on the first course of a department, and rewrite it in one line.
byDept.get(dept).add(code);
Write a method
static Set<Integer> symmetricDifference(Set<Integer> a, Set<Integer> b)
that returns a new, sorted set containing the elements that are in exactly one of a and b. The arguments must not be modified. Example: {1, 2, 3, 4} and {3, 4, 5} → [1, 2, 5].
Write a method
static String firstRepeated(String[] words)
that returns the first word whose second occurrence comes earliest in the array, or null if no word repeats. Example: {"a", "b", "c", "b", "a"} → "b" (its repeat at index 3 comes before the repeat of "a" at index 4). It must run in O(n) expected time.