Which declaration compiles?
A
Supplier<List<String>> s = () -> new ArrayList<String>();
B
Supplier<List<String>> s = x -> new ArrayList<String>();
C
Consumer<List<String>> s = () -> new ArrayList<String>();
D
Predicate<List<String>> s = () -> new ArrayList<String>();
Check answer
Which pairing of a functional interface with its single abstract method is wrong ?
A
Supplier<T> — T get()
B
Consumer<T> — void accept(T t)
C
Predicate<T> — boolean apply(T t)
D
BiFunction<T, U, R> — R apply(T t, U u)
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
What is the result of compiling and running this fragment?
int limit = 3;
Predicate<String> longer = s -> s.length() > limit;
limit = 4;
System.out.println(longer.test("tree"));
A
It prints true
B
It prints false
C
Compilation fails: limit is used in the lambda but is not effectively final
D
It compiles but throws an exception at run time
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
Assuming a suitable functional-interface target type, which lambda is not legal syntax?
A
(a, b) -> a + b
B
(String s) -> s.length()
C
x -> { return x * 2; }
D
x -> return x * 2;
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
Which method reference can replace the lambda in Function<String, String> f = s -> s.toUpperCase();?
A
s::toUpperCase
B
String::toUpperCase
C
String::toUpperCase()
D
String.toUpperCase
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
Which of these is an unbound instance method reference?
A
Math::abs
B
System.out::println
C
StringBuilder::new
D
String::length
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
What does this fragment print?
BiFunction<String, Integer, Character> f = String::charAt;
Function<String, StringBuilder> make = StringBuilder::new;
System.out.println(f.apply("lambda", 2) + "" + make.apply("ab").reverse());
A
mba
B
aab
C
mab
D
Compilation fails: charAt takes one argument, not two
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
What does this fragment print?
List<String> words = Arrays.asList("stack", "queue", "tree", "graph", "heap");
List<String> r = words.stream()
.filter(w -> w.length() == 5)
.map(w -> w.substring(0, 2))
.collect(Collectors.toList());
System.out.println(r);
A
[stack, queue, graph]
B
[tr, he]
C
[st, qu, gr]
D
[st, qu, tr, gr, he]
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
What does this fragment print?
Stream<Integer> s = Stream.of(1, 2, 3).map(x -> {
System.out.print(x);
return x * 2;
});
System.out.println("done");
A
123done
B
246done
C
done
D
Nothing: compilation fails because the stream is never used
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
What is the result?
Stream<String> s = Stream.of("a", "b", "c");
System.out.println(s.count());
System.out.println(s.count());
A
It prints 3 twice
B
It prints 3 and then 0
C
Compilation fails on the second count()
D
It prints 3, then throws IllegalStateException
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
You want list.sort(c) to put the longest strings first. Which c works?
A
Comparator.comparing(String::length)
B
(a, b) -> a.length() - b.length()
C
Comparator.comparing(String::length).reversed()
D
Comparator.comparing(String::length, Comparator.naturalOrder())
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
What does this fragment print?
List<String> list = new ArrayList<>(Arrays.asList("pear", "fig", "apple", "kiwi", "date"));
list.sort(Comparator.comparing(String::length)
.thenComparing(Comparator.reverseOrder()));
System.out.println(list);
A
[fig, date, kiwi, pear, apple]
B
[fig, pear, kiwi, date, apple]
C
[apple, pear, kiwi, date, fig]
D
[apple, date, kiwi, pear, fig]
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
Which statement about Comparable and Comparator is true?
A
Comparable is in java.lang and declares int compareTo(T o); Comparator is in java.util and declares int compare(T a, T b)
B
Comparable declares int compare(T a, T b) and is implemented by a separate sorting class
C
A class can have only one Comparator but many Comparable orders
D
Comparator can only be used with classes that already implement Comparable
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
Given the class below, what happens with the fragment that follows?
class Box {
int weight;
Box(int weight) { this.weight = weight; }
}
// ... in main:
List<Box> boxes = new ArrayList<>();
boxes.add(new Box(5));
boxes.add(new Box(2));
Collections.sort(boxes);
A
The boxes are sorted by weight
B
Compilation fails: Box does not implement Comparable
C
It compiles but throws ClassCastException
D
It compiles and leaves the list unchanged
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
What does this fragment print?
List<Integer> nums = new ArrayList<>(Arrays.asList(5, 12, 8, 3, 20));
boolean changed = nums.removeIf(n -> n % 4 == 0);
System.out.println(changed + " " + nums);
A
true [5, 3]
B
true [12, 8, 20]
C
false [5, 12, 8, 3, 20]
D
3 [5, 3]
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
Does this line compile?
int n = Stream.of("x", "y", "z").filter(s -> !s.equals("y")).count();
A
Yes, and n is 2
B
Yes, and n is 3
C
No: count() returns long, which cannot be assigned to int without a cast
D
No: filter cannot be followed directly by a terminal operation
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
What does this fragment print?
int r = Stream.of(1, 2, 3, 4).reduce(10, (a, b) -> a + b);
System.out.println(r);
A
10
B
20
C
14
D
24
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
What does this fragment print?
List<String> w = Arrays.asList("ant", "bee", "cat", "dove", "eel", "frog");
Map<Integer, Long> m = w.stream()
.collect(Collectors.groupingBy(String::length, Collectors.counting()));
System.out.println(m.get(3) + " " + m.get(4) + " " + m.get(5));
A
4 2 0
B
3 4 5
C
[ant, bee, cat, eel] [dove, frog] []
D
4 2 null
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
What does this fragment print?
String s = Stream.of("b", "a", "c", "a")
.distinct()
.sorted()
.collect(Collectors.joining("-", "<", ">"));
System.out.println(s);
A
<a-b-c>
B
<b-a-c>
C
<a>-<b>-<c>
D
<a-a-b-c>
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
What is the declared return type of IntStream.of(2, 4, 7).average()?
A
double
B
Double
C
Optional<Double>
D
OptionalDouble
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
What does this fragment print?
List<String> empty = new ArrayList<>();
String a = empty.stream().max(Comparator.naturalOrder()).orElse("none");
Optional<String> b = Stream.of("kiwi", "fig", "banana", "melon")
.max(Comparator.comparing(String::length));
System.out.println(a + " " + b.get());
A
null banana
B
none banana
C
none melon
D
It throws NoSuchElementException
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
What does this fragment print?
Stream.of(5, 3, 5, 1, 3, 8, 1)
.distinct()
.sorted()
.limit(3)
.forEach(x -> System.out.print(x + " "));
A
5 3 1
B
1 1 3
C
1 3 5
D
1 3 5 8
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
What does this fragment print?
Stream.of("a", "bb", "ccc")
.filter(s -> { System.out.print("F" + s + " "); return s.length() > 1; })
.map(s -> { System.out.print("M" + s + " "); return s.toUpperCase(); })
.forEach(s -> System.out.print(s + " "));
A
Fa Fbb Fccc Mbb Mccc BB CCC
B
Fa Fbb Mbb BB Fccc Mccc CCC
C
Fa Ma Fbb Mbb BB Fccc Mccc CCC
D
BB CCC
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
What does this fragment print?
List<Integer> nums = new ArrayList<>(Arrays.asList(4, 9, 1, 7));
Comparator<Integer> c = (a, b) -> b - a;
nums.sort(c.reversed());
System.out.println(nums);
A
[9, 7, 4, 1]
B
[4, 9, 1, 7]
C
[7, 1, 9, 4]
D
[1, 4, 7, 9]
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
What does this fragment print?
Function<Integer, Integer> add3 = x -> x + 3;
Function<Integer, Integer> times2 = x -> x * 2;
System.out.println(add3.andThen(times2).apply(4) + " " + add3.compose(times2).apply(4));
A
14 11
B
11 14
C
14 14
D
25
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
Using the Box class of question 14 (no Comparable), what happens here?
List<Box> sorted = Stream.of(new Box(5), new Box(2))
.sorted()
.collect(Collectors.toList());
System.out.println(sorted.size());
A
Compilation fails, exactly as with Collections.sort
B
It prints 2, with the boxes sorted by weight
C
It compiles, then throws ClassCastException at run time
D
It prints 2, with the boxes in their original order
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
Which statement compiles and sorts List<String> names from longest to shortest?
A
names.sort(Comparator.comparing(s -> s.length()).reversed());
B
names.sort(Comparator.comparing((String s) -> s.length()).reversed());
C
names.sort(Comparator.comparing(String::length()).reversed());
D
names.sort(Comparator.comparing(s -> s.length()).reverse());
Check answer
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
Explain what it means that intermediate stream operations are lazy . Give a short pipeline where laziness means that fewer elements are processed than the source contains, and say how many are processed.
Submit & compare
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
String already implements Comparable<String>. Give two different situations in which you would still write a Comparator<String>, and show the comparator for each.
Submit & compare
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
A student writes the code below to count how many words are longer than 4 characters. Explain why it does not compile, and rewrite it correctly with a stream.
int count = 0;
words.forEach(w -> { if (w.length() > 4) count++; });
Submit & compare
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
Name the four kinds of method reference. For each, give an example and the equivalent lambda.
Submit & compare
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
Write a method
static List<String> shortlist(List<String> names, int k)
that returns at most k distinct names that start with an upper-case letter, ordered by length (shortest first) and, for equal lengths, alphabetically. Use a single stream pipeline. Ignore empty strings. For example, ["Zoe", "al", "Bea", "Omar", "Zoe", "Ian"] with k = 3 gives [Bea, Ian, Zoe].
Submit & compare
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post
Write a method
static Map<Character, Long> countByInitial(List<String> words)
that counts the words by their first letter, ignoring case, and returns the map with its keys in alphabetical order. Skip empty strings. For ["Apple", "avocado", "banana", "Cherry", "", "cranberry"] the result prints as {a=2, b=1, c=2}.
Submit & compare
💬 Discussion
Replying to the message above ·
cancel
Be kind and never paste a full solution before others have tried.
Post