THE OCP EXAM TOPICS COVERED IN THIS PRACTICE TEST INCLUDE THE FOLLOWING:
Which of the following fills in the blank so that the code outputs one line but uses a poor practice?
import java.util.*;
public class Cheater {
int count = 0;
public void sneak(Collection<String> coll) {
coll.stream().________________________;
}
public static void main(String[] args) {
Cheater c = new Cheater();
c.sneak(Arrays.asList("weasel"));
}
}
Which can fill in the blank to have the code print true?
Stream<Integer> stream = Stream.iterate(1, i ‐> i+1);
boolean b = stream.____________(i -> i > 5);
System.out.println(b);
On a DoubleStream, how many of the methods average(), count(), and sum() return an OptionalDouble?
How many of the following can fill in the blank to have the code print 44?
Stream<String> stream = Stream.of("base", "ball");
stream._____________(s -> s.length()).forEach(System.out::print);
What is the result of the following?
IntStream s = IntStream.empty();
System.out.print(s.average().getAsDouble());
Which of these stream pipeline operations takes a Predicate as a parameter and returns an Optional?
What is the result of the following?
List<Double> list = new ArrayList<>();
list.add(5.4);
list.add(1.2);
Optional<Double> opt = list.stream().sorted().findFirst();
System.out.println(opt.get() + " " + list.get(0));
Fill in the blank so this code prints 8.0.
IntStream stream = IntStream.of(6, 10);
LongStream longs = stream.mapToLong(i -> i);
System.out.println(____________);
How many of these collectors can fill in the blank to make this code compile?
Stream<Character> chars = Stream.of(
'o', 'b', 's', 't', 'a', 'c', 'l', 'e');
chars.map(c -> c).collect(Collectors.____________ );
What does the following output?
import java.util.*;
public class MapOfMaps {
public static void main(String[] args) {
Map<Integer, Integer> map = new HashMap<>();
map.put(9, 3);
Map<Integer, Integer> result = map.stream().map((k,v) ‐> (v,k));
System.out.println(result.keySet().iterator().next());
}
}
Which of the following creates an Optional that returns true when calling opt.isPresent()?
What is the output of the following?
Stream<String> s = Stream.of("speak", "bark", "meow", "growl");
BinaryOperator<String> merge = (a, b) ‐> a;
Map<Integer, String> map = s.collect(toMap(String::length, k ‐> k, merge));
System.out.println(map.size() + " " + map.get(4));
What is the output of the following?
1: package reader;
2: import java.util.stream.*;
3:
4: public class Books {
5: public static void main(String[] args) {
6: IntegerStream pages = IntegerStream.of(200, 300);
7: IntegerSummaryStatistics stats = pages.summaryStatistics();
8: long total = stats.getSum();
9: long count = stats.getCount();
10: System.out.println(total + "-" + count);
11: }
12: }
If this method is called with Stream.of("hi"), how many lines are printed?
public static void print(Stream<String> stream) {
Consumer<String> print = System.out::println;
stream.peek(print)
.peek(print)
.map(s -> s)
.peek(print)
.forEach(print);
}
What is true of the following code?
Stream<Character> stream = Stream.of('c', 'b', 'a'); // z1
stream.sorted().findAny().ifPresent(System.out::println); // z2
Suppose you have a stream pipeline where all the elements are of type String. Which of the following can be passed to the intermediate operation sorted()?
Fill in the blanks so that both methods produce the same output for all inputs.
private static void longer(Optional<Boolean> opt) {
if (opt.____________())
System.out.println("run: " + opt.get());
}
private static void shorter(Optional<Boolean> opt) {
opt.map(x -> "run: " + x).____________(System.out::println);
}
What is the output of this code?
Stream<Boolean> bools = Stream.iterate(true, b ‐> !b);
Map<Boolean, List<Boolean>> map = bools.limit(1)
.collect(partitioningBy(b -> b));
System.out.println(map);
What does the following output?
Set<String> set = new HashSet<>();
set.add("tire-");
List<String> list = new LinkedList<>();
Deque<String> queue = new ArrayDeque<>();
queue.push("wheel-");
Stream.of(set, list, queue)
.flatMap(x -> x.stream())
.forEach(System.out::print);
What is the output of the following?
Stream<String> s = Stream.of("over the river",
"through the woods",
"to grandmother's house we go");
s.filter(n -> n.startsWith("t"))
.sorted(Comparator::reverseOrder)
.findFirst()
.ifPresent(System.out::println);
Which fills in the blank so the code is guaranteed to print 1?
Stream<Integer> stream = Stream.of(1, 2, 3);
System.out.println(stream.____________);
Which of the following can be the type for x?
private static void spot(____________ x) {
x.filter(y -> ! y.isEmpty())
.map(y -> 8)
.ifPresent(System.out::println);
}
Which can fill in the blank to have the code print true?
Stream<Integer> stream = Stream.iterate(1, i ‐> i);
boolean b = stream.____________(i -> i > 5);
System.out.println(b);
What collector turns the stream at left to the Map at right?
Which fills in the blank for this code to print 667788?
IntStream ints = IntStream.empty();
IntStream moreInts = IntStream.of(66, 77, 88);
Stream.of(ints, moreInts).____________(x -> x).forEach(System.out::print);
Fill in the blank so this code prints 8.0. Note that it must not print OptionalDouble[8.0].
LongStream stream = LongStream.of(6, 10);
LongSummaryStatistics stats = stream.summaryStatistics();
System.out.println(____________);
Which can independently fill in the blank to output No dessert today?
import java.util.*;
public class Dessert {
public static void main(String[] yum) {
eatDessert(Optional.of("Cupcake"));
}
private static void eatDessert(Optional<String> opt) {
System.out.println(opt.____________);
}
}
What does the following output?
Stream<Character> chars = Stream.generate(() ‐> 'a');
chars.filter(c -> c < 'b')
.sorted()
.findFirst()
.ifPresent(System.out::print);
How many of the following can fill in the blank to have the code print the single digit 9?
LongStream stream = LongStream.of(9);
stream.____________(p -> p).forEach(System.out::print);
Suppose you have a stream with one element and the code stream.xxxx.forEach(System.out::println). Filling in xxxx from top to bottom in the table, how many elements can be printed out?
xxxx | Number elements printed |
filter() | |
flatMap() | |
map() |
What is the output of the following?
Stream<Character> stream = Stream.of('c', 'b', 'a');
System.out.println(stream.sorted().findFirst());
What is the output of the following?
public class Compete {
public static void main(String[] args) {
Stream<Integer> is = Stream.of(8, 6, 9);
Comparator<Integer> c = (a, b) ‐> a ‐ b;
is.sort(c).forEach(System.out::print);
}
}
What is the result of the following?
class Ballot {
private String name;
private int judgeNumber;
private int score;
public Ballot(String name, int judgeNumber, int score) {
this.name = name;
this.judgeNumber = judgeNumber;
this.score = score;
}
// all getters and setters
}
public class Speaking {
public static void main(String[] args) {
Stream<Ballot> ballots = Stream.of(
new Ballot("Mario", 1, 10),
new Ballot("Christina", 1, 8),
new Ballot("Mario", 2, 9),
new Ballot("Christina", 2, 8)
);
Map<String, Integer> scores = ballots.collect(
groupingBy(Ballot::getName, summingInt(Ballot::getScore))); // w1
System.out.println(scores.get("Mario"));
}
}
Which can fill in the blank so this code outputs true?
import java.util.function.*;
import java.util.stream.*;
public class HideAndSeek {
public static void main(String[] args) {
Stream<Boolean> hide = Stream.of(true, false, true);
boolean found = hide.filter(b -> b).__________();
System.out.println(found);
}
}
What does the following output?
Set<String> set = new HashSet<>();
set.add("tire-");
List<String> list = new LinkedList<>();
Deque<String> queue = new ArrayDeque<>();
queue.push("wheel-");
Stream.of(set, list, queue)
.flatMap(x -> x)
.forEach(System.out::print);
When working with a Stream<String>, which of these types can be returned from the collect() terminal operator by passing arguments to Collectors.groupingBy()?
Which line can replace line 18 without changing the output of the program?
1: class Runner {
2: private int numberMinutes;
3: public Runner(int n) {
4: numberMinutes = n;
5: }
6: public int getNumberMinutes() {
7: return numberMinutes;
8: }
9: public boolean isFourMinuteMile() {
10: return numberMinutes < 4*60;
11: }
12: }
13: public class Marathon {
14: public static void main(String[] args) {
15: Stream<Runner> runners = Stream.of(new Runner(250),
16: new Runner(600), new Runner(201));
17: long count = runners
18: .filter(Runner::isFourMinuteMile)
19: .count();
20: System.out.println(count);
21: }
22: }
.mapToBool(Runner::isFourMinuteMile)
.filter(b -> b == true)
Which method is not available on the IntSummaryStatistics class?
Which can fill in the blank so this code outputs Caught it?
import java.util.*;
public class Catch {
public static void main(String[] args) {
Optional opt = Optional.empty();
try {
apply(opt);
} catch (IllegalArgumentException e) {
System.out.println("Caught it");
}
}
private static void apply(Optional<Exception> opt) {
opt.__________(IllegalArgumentException::new);
}
}
A developer tries to rewrite a method that uses flatMap() without using that intermediate operator. Which pair of method calls shows the withoutFlatMap() method is not equivalent to the withFlatMap() method?
public static void main(String[] args) {
List<String> list = new LinkedList<>();
Deque<String> queue = new ArrayDeque<>();
queue.push("all queued up");
queue.push("last");
}
private static void withFlatMap(Collection<?> coll) {
Stream.of(coll)
.flatMap(x -> x.stream())
.forEach(System.out::print);
System.out.println();
}
private static void withoutFlatMap(Collection<?> coll) {
Stream.of(coll)
.filter(x -> !x.isEmpty())
.map(x -> x)
.forEach(System.out::print);
System.out.println();
}