All Java

Test Your Knowledge

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

1. What does this print?

class Employee {
    String describe() { return "Employee"; }
}

class Manager extends Employee {
    String describe() { return super.describe() + " + Manager"; }
}

public class Demo {
    public static void main(String[] args) {
        System.out.println(new Manager().describe());
    }
}

2. Which of the following lambda expressions fails to compile?

3. Why does extending `Throwable` directly (instead of `Exception` or `RuntimeException`) count as a common mistake?

4. 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());
    }
}

5. Where does a generic method's type parameter appear in its declaration?

6. What happens when this code is compiled?

interface Greeter {
    void greet();
}

class QuietGreeter implements Greeter {
    protected void greet() { System.out.println("hi"); }
}

7. What does this print?

List<Integer> list = new ArrayList<>(List.of(10, 20, 30, 40));
list.remove(2);
System.out.println(list);

8. What does this print?

import java.util.function.Predicate;

public class Demo {
    public static void main(String[] args) {
        Predicate<String> isLong = s -> s.length() > 3;
        Predicate<String> startsWithA = s -> s.startsWith("A");
        Predicate<String> combined = isLong.and(startsWithA);
        System.out.println(combined.test("Anna"));
        System.out.println(combined.test("Al"));
    }
}

9. Which class is recommended for building strings piece by piece in a loop?

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

11. Which of the following are best practices when working with arrays?

12. What happens when this code is compiled?

class NumericBox<T extends Number> {
    private T value;
    void set(T value) { this.value = value; }
}

public class Demo {
    public static void main(String[] args) {
        NumericBox<String> box = new NumericBox<>();
    }
}

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

Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
String line = scanner.nextLine();
System.out.println(line);

14. What does the Path.of(...) method do?

15. What exception might be thrown when using Scanner to read from a file?

16. What is the relationship between `UnaryOperator<T>` and `Function<T, T>`?

17. What does this print?

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Demo {
    public static void main(String[] args) {
        List<Integer> list = IntStream.rangeClosed(1, 3)
                .boxed()
                .collect(Collectors.toList());
        System.out.println(list);
    }
}

18. What type does `scores` have in `var scores = List.of(90, 85, 78);`?

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

20. By default, what happens if you call Files.writeString(path, content) on a file that already exists?