THE OCP EXAM TOPICS COVERED IN THIS PRACTICE TEST INCLUDE THE FOLLOWING:
Fill in the blanks: The____________ functional interface does not take any inputs, while the____________ functional interface does not return any data.
Which functional interface takes a long value as an input argument and has an accept() method?
What is the output of the following application?
package beach;
import java.util.function.*;
class Tourist {
public Tourist(double distance) {
this.distance = distance;
}
public double distance;
}
public class Lifeguard {
private void saveLife(Predicate<Tourist> canSave, Tourist tourist) {
System.out.print(canSave.test(tourist) ? "Saved" : "Too far"); // y1
}
public final static void main(String... sand) {
new Lifeguard().saveLife(s -> s.distance<4, new Tourist(2)); // y2
}
}
Which of the following statements about DoubleSupplier and Supplier<Double> is not true?
Which functional interface, when filled into the blank, allows the class to compile?
package space;
import java.util.function.*;
public class Asteroid {
public void mine(____________ lambda) {
// TODO: Apply functional interface
}
public static void main(String[] debris) {
new Asteroid().mine((s,p) -> s+p);
}
}
Assuming the proper generic types are used, which lambda expression cannot be assigned to a ToDoubleBiFunction functional interface reference?
Which of the following is not a functional interface in the java.util.function package?
What is the output of the following application?
package zoo;
import java.util.function.*;
public class TicketTaker {
private static int AT_CAPACITY = 100;
public int takeTicket(int currentCount, IntUnaryOperator<Integer> counter) {
return counter.applyAsInt(currentCount);
}
public static void main(String...theater) {
final TicketTaker bob = new TicketTaker();
final int oldCount = 50;
final int newCount = bob.takeTicket(oldCount,t -> {
if(t>AT_CAPACITY) {
throw new RuntimeException("Sorry, max has been reached");
}
return t+1;
});
System.out.print(newCount);
}
}
Which functional interface returns a primitive value?
Which functional interface, when entered into the blank below, allows the class to compile?
package groceries;
import java.util.*;
import java.util.function.*;
public class Market {
private static void checkPrices(List<Double> prices,
____________scanner) {
prices.forEach(scanner);
}
public static void main(String[] right) {
List<Double> prices = Arrays.asList(1.2, 6.5, 3.0);
checkPrices(prices,
p -> {
String result = p<5 ? "Correct" : "Too high";
System.out.println(result);
});
}
}
Which of the following three functional interfaces is not equivalent to the other two?
Which lambda expression can be passed to the magic() method?
package show;
import java.util.function.*;
public class Magician {
public void magic(BinaryOperator<Long> lambda) {
lambda.apply(3L, 7L);
}
}
What is the output of the following program?
package ai;
import java.util.function.*;
public class Android {
public void wakeUp(Supplier supplier) { // d1
supplier.get();
}
public static void main(String... electricSheep) {
Android data = new Android();
data.wakeUp(() -> System.out.print("Program started!")); // d2
}
}
Which statement about all UnaryOperator functional interfaces (generic and primitive) is correct?
Starting with DoubleConsumer and going downward, fill in the missing values for the table.
Functional Interface | # Parameters |
DoubleConsumer | |
IntFunction | |
LongSupplier | |
ObjDoubleConsumer |
Starting with DoubleConsumer and going downward, fill in the values for the table. For the choices below, assume R is a generic type.
Functional Interface | Return Type |
DoubleConsumer | |
IntFunction | |
LongSupplier | |
ObjDoubleConsumer |
Fill in the blanks: In the Collection interface, the method removeIf() takes a____________ , while the method forEach() takes a____________ .
What is the output of the following application?
package nesting;
import java.util.function.*;
public class Doll {
private int layer;
public Doll(int layer) {
super();
this.layer = layer;
}
public static void open(UnaryOperator<Doll> task, Doll doll) {
while((doll = task.accept(doll)) != null) {
System.out.print("X");
}
}
public static void main(String[] wood) {
open(s -> {
if(s.layer<=0) return null;
else return new Doll(s.layer);
}, new Doll(5));
}
}
Which functional interface has a get() method?
The following diagram shows input arguments being applied to three functional interfaces of unknown type. Which three functional interfaces, inserted in order from left to right, could be used to complete the diagram?
DoubleBinaryOperator
ToDoubleBiFunction<Integer,Double>
UnaryOperator<Integer>
BinaryOperator<Double>
BiFunction<Integer,Integer,Double>
UnaryOperator<Integer>
Function<Double,Integer>
BiFunction<Integer,Integer,Double>
DoubleToIntFunction
BiFunction<Integer,Double,Integer>
BinaryOperator<Integer>
IntUnaryOperator
Which statement about functional interfaces and lambda expressions is not true?
Which expression is compatible with the IntSupplier functional interface?
What is the output of the following application?
package tps;
import java.util.*;
class Boss {
private String name;
public Boss(String name) {
this.name = name;
}
public String getName() {return name.toUpperCase();}
public String toString() {return getName();}
}
public class Initech {
public static void main(String[] reports) {
final List<Boss> bosses = new ArrayList(8);
bosses.add(new Boss("Jenny"));
bosses.add(new Boss("Ted"));
bosses.add(new Boss("Grace"));
bosses.removeIf(s -> s.equalsIgnoreCase("ted"));
System.out.print(bosses);
}
}
Which of the following method references can be passed to a method that takes Consumer<Object> as an argument?
Which of the following is a valid functional interface in the java.util.function package?
Which functional interface, when filled into the blank, prevents the class from compiling?
package morning;
import java.util.function.*;
public class Sun {
public static void dawn(____________ sunrise) {}
public void main(String... rays) {
dawn(s -> s+1);
}
}
Which functional interface does not have the correct number of generic arguments?
Which lambda expression, when filled into the blank, allows the code to compile?
package ballroom;
import java.util.function.*;
public class Dance {
public static Integer rest(BiFunction<Integer,Double,Integer> takeABreak) {
return takeABreak.apply(3, 10.2);
}
public static void main(String[] participants) {
rest(____________);
}
}
Fill in the blank: ____________is the only functional interface that does not involve double, int, or long.
What is the output of the following application?
package savings;
import java.util.function.*;
public class Bank {
private int savingsInCents;
private static class ConvertToCents {
static DoubleToIntFunction f = p -> p*100;
}
public static void main(String... currency) {
Bank creditUnion = new Bank();
creditUnion.savingsInCents = 100;
double deposit = 1.5;
creditUnion.savingsInCents += ConvertToCents.f.applyAsInt(deposit); // j1
System.out.print(creditUnion.savingsInCents);
}
}
Which functional interface takes a double value and has a test() method?
Given the following class, how many lines contain compilation errors?
1: package showtimes;
2: import java.util.*;
3: import java.util.function.*;
4: public class FindMovie {
5: private Function<String> printer;
6: protected FindMovie() {
7: printer = s -> {System.out.println(s); return s;}
8: }
9: void printMovies(List<String> movies) {
10: movies.forEach(printer);
11: }
12: public static void main(String[] screen) {
13: List<String> movies = new ArrayList<>();
14: movies.add("Stream 3");
15: movies.add("Lord of the Recursion");
16: movies.add("Silence of the Lambdas");
17: new FindMovie().printMovies(movies);
18: }
19: }
Which lambda expression cannot be assigned to a DoubleToLongFunction functional interface?
Which of the following is not a functional interface in the java.util.function package?
Which functional interface, when filled into the blank, allows the class to compile?
package sleep;
import java.util.function.*;
class Sheep {}
public class Dream {
int MAX_SHEEP = 10;
int sheepCount;
public void countSheep( ____________backToSleep) {
while(sheepCount<MAX_SHEEP) {
// TODO: Apply lambda
sheepCount++;
}
}
public static void main(String[] dark) {
new Dream().countSheep(System.out::println);
}
}
What is the output of the following application?
package pet;
import java.util.*;
import java.util.function.*;
public class DogSearch {
void reduceList(List<String> names, Predicate<String> tester) {
names.removeIf(tester);
}
public static void main(String[] treats) {
int MAX_LENGTH = 2;
DogSearch search = new DogSearch();
List<String> names = new ArrayList<>();
names.add("Lassie");
names.add("Benji");
names.add("Brian");
MAX_LENGTH += names.size();
search.reduceList(names, d -> d.length()>MAX_LENGTH);
System.out.print(names.size());
}
}
Which functional interface takes two values and has an apply() method?
Which of the following lambda expressions can be passed to a method that takes IntFunction<Integer> as an argument?
What is the output of the following application?
package lot;
import java.util.function.*;
public class Warehouse {
private int quantity = 40;
private final BooleanSupplier stock;
{
stock = () -> quantity>0;
}
public void checkInventory() {
if(stock.get())
System.out.print("Plenty!");
else {
System.out.print("On Backorder!");
}
}
public static void main(String... widget) {
final Warehouse w13 = new Warehouse();
w13.checkInventory();
}
}
Which of the following statements about functional interfaces is true?