All Java

Test Your Knowledge

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

1. A method only ever WRITES elements into a `List<T>` parameter and never reads from it. According to PECS, which wildcard form should it use?

2. What will happen if you try to access an index that is out of bounds in an array?

3. According to the lesson, which type is the best fit for storing a timestamp (like "when did this event happen") in a database?

4. What does this print?

import java.util.Optional;

public class Demo {
    public static void main(String[] args) {
        Optional<Integer> num = Optional.of(4);
        Optional<Integer> filtered = num.filter(n -> n > 10);
        System.out.println(filtered.isPresent());
    }
}

5. A method is written as `static T firstElement(List<T> list) { ... }`, without a `<T>` before the return type. What is the result?

6. What will be the output of the following code?

Integer a = 100; Integer b = 100; System.out.println(a == b);

7. According to this lesson, why does `java.util.Stack`'s own javadoc recommend using `Deque`/`ArrayDeque` instead?

8. If a subclass constructor never explicitly calls `super(...)`, what does the compiler do?

9. What will this code print?

LocalDate d1 = LocalDate.of(2026, 1, 31);
LocalDate d2 = d1.plusDays(1);
System.out.println(d1);

10. What does this print?

import java.util.List;

public class Demo {
    public static void main(String[] args) {
        List<Integer> nums = List.of(2, 4, 6, 8);
        System.out.println(nums.stream().allMatch(n -> n % 2 == 0));
        System.out.println(nums.stream().anyMatch(n -> n > 7));
        System.out.println(nums.stream().noneMatch(n -> n < 0));
    }
}

11. What are the two direct subclasses of `Throwable`?

12. A `List<String>` is declared, and code tries to call `.add(42)` on it. Which of the following are true? (Select all that apply)

13. Which call to `toArray()` on a `List<String>` correctly produces a `String[]` rather than an `Object[]`?

14. What does this print?

import java.util.OptionalDouble;
import java.util.stream.IntStream;

public class Demo {
    public static void main(String[] args) {
        IntStream empty = IntStream.of();
        System.out.println(empty.sum());
        OptionalDouble avg = IntStream.of().average();
        System.out.println(avg.isPresent());
    }
}

15. Why should you avoid persisting the ordinal() value as permanent data in a database?

16. What access modifier can an enum constructor have?

17. Which statement correctly distinguishes compile-time polymorphism from runtime polymorphism?

18. Which statement correctly distinguishes `Consumer<T>` from `Supplier<T>`?

19. A method accepts a `List<?>` parameter and calls `.size()` on it, then tries `.add("new element")` on the same parameter. What happens?

20. What does this print?

Map<String, Integer> map = new TreeMap<>();
map.put("banana", 2);
map.put("apple", 1);
map.put("cherry", 3);
System.out.println(map.keySet());