Given: class Animal { String speak() { return "..."; } } class Dog extends Animal { @Override String speak() { return "Woof"; } } Animal a = new Dog(); String s = a.speak(); What is the value of s, and why?
Java Class Design
Question · Easy
Which set of characteristics correctly describes a properly implemented immutable class in Java?
Question · Medium
Given a thread-safe singleton implemented with an enum: public enum Config { INSTANCE; private int v = 1; } Which statement about this pattern is TRUE?
Tech Exam Prep Podcast
Listen on Spotify
Podcast
Given: class Base { Base() { init(); } void init() { System.out.print("B"); } } class Sub extends Base { int x = 5; void init() { System.out.print(x); } } What does new Sub() print?
Question · Hard
An overriding method in a subclass may legally do which of the following relative to the method it overrides?
Given: class A { static String who() { return "A"; } } class B extends A { static String who() { return "B"; } } A ref = new B(); String s = ref.who(); What is s?
What is the primary purpose of overriding both equals(Object) and hashCode() together when a class is used as a HashMap key?
Given two interfaces with the SAME default method signature: interface X { default String id() { return "X"; } } interface Y { default String id() { return "Y"; } } class C implements X, Y { } What happens?
Advanced Java Class Design
Which statement about an abstract class in Java SE 8 is TRUE?
Given: enum Planet { EARTH(9.8), MARS(3.7); private final double g; Planet(double g){this.g=g;} double gravity(){return g;} } Which statement is TRUE?
A local class is defined inside a method. Which variables from the enclosing method can it reference?
Which of the following is a valid functional interface that can be the target of a lambda expression?
Given an inner (non-static) class Inner inside Outer, which expression correctly creates an Inner instance from outside Outer, where Outer o exists?
An interface in Java SE 8 may contain which of the following members?
Given: List<? extends Number> list = new ArrayList<Integer>(); Which statement is TRUE about the list reference?
Generics And Collections
The PECS principle (Producer Extends, Consumer Super) suggests which wildcard for a method that COPIES elements FROM a source list INTO a destination list, e.g. copy(List<? super T> dest, List<? extends T> src)?
Given a list of strings sorted with: list.sort(Comparator.comparing(String::length).thenComparing(Comparator.naturalOrder())); How are the strings ordered?
What does Map's merge(K key, V value, BiFunction remappingFunction) do when the key is already present with a non-null value?
Which collection guarantees that its iteration order matches the insertion order of elements?
Given a class Box<T> and a generic method: static <T extends Comparable<T>> T max(List<T> list) { ... } What does the bound 'T extends Comparable<T>' enforce?
What is the result of calling list.replaceAll(s -> s.toUpperCase()) on a List<String> backed by Arrays.asList("a","b")?
Which method reference form correctly refers to an instance method of an arbitrary object of a particular type, suitable for Comparator.comparing(...)?
Given a TreeMap<String,Integer>, which method returns the greatest key strictly less than the given key?
What distinguishes Comparable<T> from Comparator<T>?
Given: Deque<Integer> d = new ArrayDeque<>(); d.push(1); d.push(2); d.push(3); Integer x = d.pop(); What is x, and what behavior does push/pop give ArrayDeque?
Why does the following fail to compile? List<Integer> ints = new ArrayList<>(); List<Number> nums = ints;
What does Collections.unmodifiableList(list) return, and what happens on a subsequent add()?
Given: List<String> l = new ArrayList<>(List.of("a","b","c")); for (String s : l) { if (s.equals("b")) l.remove(s); } What is the most likely result?
Which built-in functional interface represents a function that takes one argument of type T and returns a boolean?
Lambda Built In Functional Interfaces
What is the single abstract method of Supplier<T>, and a typical use?
Given: BiFunction<Integer,Integer,Integer> add = (a,b) -> a + b; Function<Integer,Integer> twice = x -> x * 2; int r = add.andThen(twice).apply(3, 4); What is r?
Which primitive-specialized functional interface avoids autoboxing when mapping an int to a double?
What does Predicate's default method negate() return when called on p = s -> s.isEmpty()?
Which functional interface is the correct target type for the lambda (x) -> x, where x and the result are the same reference type T?
Given: Consumer<String> c1 = s -> System.out.print("a"); Consumer<String> c2 = s -> System.out.print("b"); c1.andThen(c2).accept("x"); What is printed?
Given: List<Integer> nums = Arrays.asList(1,2,3,4,5); int sum = nums.stream().filter(n -> n % 2 == 0).mapToInt(Integer::intValue).sum(); What is sum?
Java Stream Api
What does the following print? Stream.of("a","b","c") .peek(System.out::print) .map(String::toUpperCase) .findFirst();
Given a Stream<List<Integer>>, which operation flattens it into a single Stream<Integer>?
Given: Optional<String> o = Optional.ofNullable(null); String r = o.map(String::trim).orElse("empty"); What is r, and does map throw on an empty Optional?
What does this collect produce? Stream.of("apple","banana","avocado") .collect(Collectors.groupingBy(s -> s.charAt(0)));
Which statement about Collectors.partitioningBy(predicate) is TRUE?
Given: int total = Stream.of(1,2,3,4).reduce(0, (a,b) -> a + b); What is the role of the first argument 0?
What is a key requirement for safely using a parallel stream's reduction with an accumulator and combiner?
Given: IntStream.rangeClosed(1, 5).forEach(System.out::print); What is printed?
What does IntStream's summaryStatistics() return?
Given: String joined = Stream.of("x","y","z").collect(Collectors.joining(", ", "[", "]")); What is joined?
Why does the following throw IllegalStateException? Stream<String> s = Stream.of("a","b"); s.forEach(System.out::print); s.forEach(System.out::print);
Loading more…