Advanced Java

Test Your Knowledge

Answer all 10 questions, then submit to see your score.

1. What happens when this code runs?

static void pollute(List list) {
    list.add("oops");
}

public class Demo {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        pollute(numbers);
        for (Integer n : numbers) {
            System.out.println(n);
        }
    }
}

2. Which statement correctly describes what a `Stream<T>` actually is?

3. How many interfaces can a single class implement at once?

4. Why can a broad `catch (Exception e)` block that does nothing meaningful be dangerous?

5. What does this print?

public class Demo {
    static void check(int index) {
        int[] data = {1, 2, 3};
        try {
            System.out.println(data[index] / 0);
        } catch (RuntimeException e) {
            System.out.println("caught: " + e.getClass().getSimpleName());
        }
    }
    public static void main(String[] args) {
        check(1);
    }
}

6. Which of the following are true about functional interfaces? (Select all that apply)

7. A `Map<String, Integer>` is declared, and code tries `map.put("Bob", "thirty")`, passing a String where an Integer value is expected. Which of the following are true? (Select all that apply)

8. What does this print?

import java.util.Comparator;
import java.util.function.BiFunction;

public class Demo {
    public static void main(String[] args) {
        Comparator<String> cmp = (a, b) -> a.length() - b.length();
        BiFunction<String, String, Integer> fn = (a, b) -> a.length() - b.length();
        System.out.println(cmp.compare("hi", "hello"));
        System.out.println(fn.apply("hi", "hello"));
    }
}

9. In `catch (IOException e) { ... }`, what is `e`?

10. Which of the following correctly describe the three functions that make up a `Collector`? (Select all that apply)