Skip to content
KALANBLON
Show contents

Chapter 3 of 7 · 10 min

Quiz: a more expressive Java

Generics, sealed types and pattern matching: let's check that you can make the compiler work for you. You need 70% correct answers.

Quiz

6 questions · pass mark: 70%

  1. 1.Why doesn't List<Object> objects = new ArrayList<String>(); compile?

    One answer

  2. 2.A method copies elements from a source list to a destination list. Which wildcards should you choose?

    One answer

  3. 3.Complete to allow only comparable types.

    Complete the code

    <T ____ Comparable<? super T>> T largest(List<T> l)
  4. 4.Why doesn't this switch on a sealed interface need a default?

    One answer

    sealed interface Shape permits Circle, Square {}
    
    double area(Shape s) {
        return switch (s) {
            case Circle c -> Math.PI * c.r() * c.r();
            case Square q -> q.side() * q.side();
        };
    }
  5. 5.Which statements about patterns are true? (several answers)

    Several answers possible

  6. 6.Why can't you write new T() in a generic class?

    One answer

Your progress is saved on this device. Create a free account to keep your progress on all your devices.