Answer all 20 questions, then submit to see your score.
1. 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)); } }
2. Which of these statements about an exception object are true? (Select all that apply)
3. 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); } } }
4. What generally happens when `list.remove(...)` is called inside an enhanced for that is running over that same `List`?
5. What does this code print?
Thread worker = new Thread(() -> System.out.println(Thread.currentThread().getName())); worker.run();
6. Which of these statements about the classic `switch` syntax are true? (Select all that apply)
7. What happens when this code is compiled?
static double sum(List<? extends Number> numbers) { double total = 0; for (Number n : numbers) total += n.doubleValue(); numbers.add(5); return total; }
8. What happens when you try to unbox a null wrapper in Java?
9. What does this print?
class Animal { Animal reproduce() { return new Animal(); } } class Dog extends Animal { @Override Dog reproduce() { return new Dog(); } } public class Demo { public static void main(String[] args) { Dog d = new Dog(); Dog puppy = d.reproduce(); System.out.println(puppy.getClass().getSimpleName()); } }
10. What does this print?
Map<String, Integer> ages = new HashMap<>(); ages.put("Alice", 30); Integer bobAge = ages.get("Bob"); System.out.println(bobAge);
11. According to this lesson, what does a growing chain of `if (obj instanceof TypeA) {...} else if (obj instanceof TypeB) {...}` usually indicate?
12. What happens when this code is compiled?
class Utils { static <T> double describe(T value) { return value.doubleValue(); } }
13. What does this print?
class InvalidOrderQuantityException extends RuntimeException { private final int quantity; InvalidOrderQuantityException(int quantity) { super("invalid quantity: " + quantity); this.quantity = quantity; } int getQuantity() { return quantity; } } public class Demo { static void placeOrder(int quantity) { if (quantity <= 0) { throw new InvalidOrderQuantityException(quantity); } } public static void main(String[] args) { try { placeOrder(-3); } catch (InvalidOrderQuantityException e) { System.out.println(e.getMessage() + " / qty=" + e.getQuantity()); } } }
14. What happens when this code is compiled?
class Container<T> { private T value; static T sharedDefault; }
15. What does this print?
interface Auditable { String auditLog(); } abstract class Document implements Auditable { abstract String content(); } class Report extends Document { String content() { return "report content"; } public String auditLog() { return "audited: " + content(); } } public class Demo { public static void main(String[] args) { Document d = new Report(); System.out.println(d.auditLog()); } }
16. What will the resulting file contain after this code runs?
try (BufferedWriter w = new BufferedWriter(new FileWriter(path.toFile()))) { w.write("Line1"); w.write("Line2"); }
17. Which of the following statements about Java's exception hierarchy are true? (Select all that apply)
18. After this code runs, what will the file contain?
List<String> lines = List.of("Alpha", "Beta", "Gamma"); Files.write(path, lines);
19. What happens when this code is compiled?
Map<String, Integer> ages = new HashMap<>(); ages.put("Alice", 30); ages.put(42, 25);
20. Which of the following statements about enums are true?