OCJP Practice Papers - Lambda Built-In Functional Interfaces

OCJP Practice Papers Using Loop Constructs include following topics

  • Create and use a generic class
  • Create and use ArrayList, TreeSet, TreeMap, and ArrayDeque objects
  • Use java.util.Comparator and java.lang.Comparable interfaces
  • Collections Streams and Filters
  • Iterate using forEach methods of Streams and List
  • Describe Stream interface and Stream pipeline
  • Filter a collection by using lambda expressions
  • Use method references with Streams

See the complete syllabus for OCJP here

1. Fill in the blanks: The______ functional interface does not take any inputs,while the____ functional interface does not return any data.
A. IntConsumer, LongSupplier
B. IntSupplier, Function
C. Supplier, DoubleConsumer
D. UnaryOperator, Consumer

2. Which functional interface takes a long value as an input argument and has anaccept() method?
A. LongConsumer
B. LongFunction
C. LongPredicate
D. LongSupplier

3. 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(PredicateTourist 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.distance4, new Tourist(2)); // y2
}
}

A. Saved
B. Too far
C. The code does not compile because of line y1.
D. The code does not compile because of line y2.

4. Which of the following statements about DoubleSupplier and SupplierDouble is nottrue?
A. Both are functional interfaces.
B. Lambdas for both can return a double value.
C. Lambdas for both cannot return a null value.
D. One supports a generic type, the other does not.

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

A. BiConsumerInteger,Double
B. BiFunctionInteger,Double,Double
C. BiFunctionInteger,Integer,Double
D. FunctionInteger,Double

6. Assuming the proper generic types are used, which lambda expression cannot beassigned to a ToDoubleBiFunction functional interface reference?
A. (Integer a, Double b) - {int c; return b;}
B. (h,i) - (long)h
C. (String u, Object v) - u.length()+v.length()
D. (x,y) - {int z=2; return y/z;}

7. Which of the following is not a functional interface in the java.util.functionpackage?
A. BiPredicate
B. DoubleUnaryOperator
C. ObjectDoubleConsumer
D. ToLongFunction

8. 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, IntUnaryOperatorInteger 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(tAT_CAPACITY) {
throw new RuntimeException("Sorry, max has been reached");
}
return t+1;
});
System.out.print(newCount);
}
}

A. 51
B. The code does not compile because of lambda expression.
C. The code does not compile for a different reason.
D. The code compiles but prints an exception at runtime.

9. Which functional interface returns a primitive value?
A. BiPredicate
B. CharSupplier
C. LongFunction
D. UnaryOperator

10. Which functional interface, when entered into the blank below, allows the class tocompile?

package groceries;
import java.util.*;
import java.util.function.*;
public class Market {
private static void checkPrices(ListDouble prices,
____________scanner) {
prices.forEach(scanner);
}
public static void main(String[] right) {
ListDouble prices = Arrays.asList(1.2, 6.5, 3.0);
checkPrices(prices,
p - {
String result = p5 ? "Correct" : "Too high";
System.out.println(result);
});
}
}

A. Consumer
B. DoubleConsumer
C. SupplierDouble
D. None of the above

11. Which of the following three functional interfaces is not equivalent to the other two?
A. BiFunctionDouble,Double,Double
B. BinaryOperatorDouble
C. DoubleFunctionDouble
D. None of the above. All three are equivalent.

12. Which lambda expression can be passed to the magic() method?

package show;
import java.util.function.*;
public class Magician {
public void magic(BinaryOperatorLong lambda) {
lambda.apply(3L, 7L);
}
}

A. magic((a) - a)
B. magic((b,w) - (long)w.intValue())
C. magic((c,m) - {long c=4; return c+m;})
D. magic((Integer d, Integer r) - (Long)r+d)

13. 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
}
}

A. Program started!
B. The code does not compile because of line d1 only.
C. The code does not compile because of line d2 only.
D. The code does not compile because of both lines d1 and d2.

14. Which statement about all UnaryOperator functional interfaces (generic and primitive)is correct?
A. The input type must be compatible with the return type.
B. Some of them take multiple arguments.
C. They each take a generic argument.
D. They each return a primitive value.

15. Starting with DoubleConsumer and going downward, fill in the missing values for thetable. Functional Interface # Parameters DoubleConsumer IntFunction LongSupplier ObjDoubleConsumer
A. 0, 1, 1, 1
B. 0, 2, 1, 2
C. 1, 1, 0, 2
D. 1, 1, 0, 1

16. Starting with DoubleConsumer and going downward, fill in the values for the table. Forthe choices below, assume R is a generic type. Functional Interface Return Type DoubleConsumer IntFunction LongSupplier ObjDoubleConsumer
A. double, R, long, R
B. R, int, long, R
C. void, int, R, void
D. void, R, long, void

17. Fill in the blanks: In the Collection interface, the method removeIf() takesa____________ , while the method forEach() takes a____________ .
A. Function, Function
B. Predicate, Consumer
C. Predicate, Function
D. Predicate, UnaryOperator

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

A. XXXXX
B. The code does not compile because of the lambda expression.
C. The code does not compile for a different reason.
D. The code compiles but produces an infinite loop at runtime.

19. Which functional interface has a get() method?
A. Consumer
B. Function
C. Supplier
D. UnaryOperator

20. The following diagram shows input arguments being applied to three functionalinterfaces of unknown type. Which three functional interfaces, inserted in order fromleft to right, could be used to complete the diagram?
A. DoubleBinaryOperator ToDoubleBiFunctionInteger,Double UnaryOperatorInteger
B. BinaryOperatorDouble BiFunctionInteger,Integer,Double UnaryOperatorInteger
C. FunctionDouble,Integer BiFunctionInteger,Integer,Double DoubleToIntFunction
D. BiFunctionInteger,Double,Integer

BinaryOperatorInteger IntUnaryOperator 21. Which statement about functional interfaces and lambda expressions is not true?
A. A lambda expression may be compatible with multiple functional interfaces.
B. A lambda expression must be assigned to a functional interface when it is declared.
C. A method can return a lambda expression in the form of a functional interface instance.
D. The compiler uses deferred execution to skip determining whether a lambda

expression compiles or not. 22. Which expression is compatible with the IntSupplier functional interface?
A. () - 110 ? "3" : 4
B. () - {return 1/0;}
C. () - return 4
D. System.out::print

23. 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 ListBoss 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);
}
}

A. [JENNY, GRACE]
B. [tps.Boss@4218224c, tps.Boss@815f19a]
C. The code does not compile because of the lambda expression.
D. The code does not compile for a different reason.

24. Which of the following method references can be passed to a method that takesConsumerObject as an argument? I. ArrayList::new II. String::new III. System.out::println
A. I only
B. I, II, and III
C. I and III
D. III only

25. Which of the following is a valid functional interface in the java.util.functionpackage?
A. FloatPredicate
B. ToDoubleBiFunction
C. UnaryIntOperator
D. TriPredicate

26. Which functional interface, when filled into the blank, prevents the class fromcompiling?

package morning;
import java.util.function.*;
public class Sun {
public static void dawn(____________ sunrise) {}
public void main(String... rays) {
dawn(s - s+1);
}
}

A. DoubleUnaryOperator
B. FunctionString,String
C. IntToLongFunction
D. UnaryOperator

27. Which functional interface does not have the correct number of generic arguments?
A. BiFunctionT,U,R
B. DoubleFunctionT,R
C. ToDoubleFunctionT
D. ToIntBiFunctionT,U

28. 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(BiFunctionInteger,Double,Integer takeABreak)
{
return takeABreak.apply(3, 10.2);
}
public static void main(String[] participants) {
rest(____________);
}
}

A. (int n, double e) - (int)(n+e)
B. (n,w,e) - System.out::print
C. (s,w) - 2*w
D. (s,e) - s.intValue()+e.intValue()

29. Fill in the blank: ____________is the only functional interface that does notinvolve double, int, or long.
A. BooleanSupplier
B. CharPredicate
C. FloatUnaryOperator
D. ShortConsumer

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

A. 200
B. 250
C. The code does not compile because of line j1.
D. None of the above

31. Which functional interface takes a double value and has a test() method?
A. DoubleConsumer
B. DoublePredicate
C. DoubleUnaryOperator
D. ToDoubleFunction

32. 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 FunctionString printer;
6: protected FindMovie() {
7: printer = s - {System.out.println(s); return s;}
8: }
9: void printMovies(ListString movies) {
10: movies.forEach(printer);
11: }
12: public static void main(String[] screen) {
13: ListString 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: }

A. None. The code compiles as is.
B. One
C. Two
D. Three

33. Which lambda expression cannot be assigned to a DoubleToLongFunction functionalinterface?
A. a - null==null ? 1 : 2L

B. e - (int)(10.0*e)
C. (double m) - {long p = (long)m; return p;}
D. (Double s) - s.longValue()

34. Which of the following is not a functional interface in the java.util.functionpackage?
A. DoublePredicate
B. LongUnaryOperator
C. ShortSupplier
D. ToIntBiFunction

35. 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(sheepCountMAX_SHEEP) {
// TODO: Apply lambda
sheepCount++;
}
}
public static void main(String[] dark) {
new Dream().countSheep(System.out::println);
}
}

A. ConsumerSheep
B. FunctionSheep,void
C. UnaryOperatorSheep
D. None of the above

36. What is the output of the following application?

package pet;
import java.util.*;
import java.util.function.*;
public class DogSearch {
void reduceList(ListString names, PredicateString tester) {
names.removeIf(tester);
}
public static void main(String[] treats) {
int MAX_LENGTH = 2;
DogSearch search = new DogSearch();
ListString 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());
}
}

A. 2
B. 3
C. The code does not compile because of lambda expression.
D. The code does not compile for a different reason.

37. Which functional interface takes two values and has an apply() method?
A. BiConsumer
B. BiFunction
C. BiPredicate
D. DoubleBinaryOperator

38. Which of the following lambda expressions can be passed to a method that takesIntFunctionInteger as an argument? I. (Integer f) - f II. (v) - null III. s - s
A. I, II, and III
B. II and III only
C. III only
D. None of the above

39. 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 = () - quantity0;
}
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();
}
}

A. Plenty!
B. On Backorder!
C. The code does not compile because of the checkInventory() method.
D. The code does not compile for a different reason.

40. Which of the following statements about functional interfaces is true?
A. It is possible to define a functional interface that returns two data types.
B. It is possible to define a primitive functional interface that uses float, char, or short.
C. It is not possible to define a functional interface that does not take any arguments nor return any value.
D. None of the primitive functional interfaces include generic arguments

OCJP Practice Test, Certification Path, Tips/Tricks

  1. Oracle (OCJP) Java Certification Exam
  2. Java Certification Tips And Tricks
  3. OCJP Practice Papers - Java Basics
  4. OCJP Practice Papers - Operators And Decision Constructs
  5. OCJP Practice Papers-A Advanced Java Class Design
  6. OCJP Practice Papers Creating And Using Arrays
  7. OCJP Practice Papers Using Loop Constructs
  8. OCJP Practice Papers - Generics And Collections
  9. OCJP Practice Papers - Lambda Built-In Functional Interfaces
  10. OCJP Practice Papers Java Class Design
  11. OCJP Practice Papers - Java Stream API
  12. OCJP Practice Papers - Exceptions And Assertions
  13. OCJP Practice Papers - Date/Time API
  14. OCJP Practice Papers - Java I/O Fundamentals
  15. OCJP Practice Papers - Working With Methods And Encapsulation
  16. OCJP Practice Papers - Working With Inheritance
  17. OCJP Practice Papers - Handling Exceptions
  18. OCJP Practice Papers - Selected Classes From Java API
  19. OCJP Practice Papers - Java File I/O Nio.2
  20. OCJP Practice Papers - Java Concurrency