OCPtech

OCP: Functional Programming in Java

Java8 revolutionised Java programming with the introduction of functional programming.

Here is a little quiz (you ‘ll find the answers below):

1 . What is the name of the abstract method of the Consumer interface?

A. apply

B. accept

C. test

D. get

 

2 . Which method can you call on Optional to perform an action when the Optional contains a value?

A. isPresent()

B. get()

C. ifPresent()

 

3 . What is false? A stream …

A. needs to have intermediate operations

B. needs to have a terminal operation

C. is lazily initialised

D. can have multiple intermediate operations

 

4 . Which one does not compile?

A. Stream<Integer> empty = Stream.empty()

B. Stream<Integer> stream = Stream.of(1)

C. Stream<Integer> stream = Stream.of(1,2,3)

D. They all compile

 

5 . Stream.reduce(T identity, BinaryOperator<T> accumulator); When no identity is specified…

A. The code does not compile

B. A runtime expression may be thrown

C. Optional is returned

 

6 . limit() and skip() methods make a Stream …

A. smaller

B. terminate

C. bigger

D. infinite

 

7 . Which method is useful for debugging by allowing an operation without changing the stream.

A. pop()

B. peek()

C. pol()

 

8 . Which stream does not contain 5 elements?

A. DoubleStream.generate(Math::Random).limit(5)

B. IntStream.iterate(1 , n -> ++n).skip(5).limit(5)

C. IntStream.range(1, 5)

D. IntStream.rangeClosed(1, 5)

 

9 . Stream<Integer> s =Arrays.asList(1, 2).stream(); s.add(4); System.out.println(stream.count());

A. prints 4

B. prints 3

C. prints 2

 

10 . IntStream.rangeClosed(0, 20).boxed() .collect(Collectors.toMap(n -> n, n -> n*n)));

A. Does not compile

B. returns a map with the squared keys as values

C. Throws a runtime exception

 

11 . True or False? sum() is a terminal operation on the Stream class.

A. True

B. False

 

12 . We can sort a stream with which method?

A. compare()

B.  sorted()

C.  compareTo

 

13 . Performing .sum() on an empty IntStream returns

A. a RunTimeException

B. an int

C. an OptionalInt

 

14 . Which interface does not exist?

A. BiConsumer

B. BinaryOperator

C. BinaryFunction

D. UnaryOperator

 

15 . Which Supplier does not exist?

A. BooleanSupplier

B. LongSupplier

C. DoubleSupplier

D. FloatSupplier

 

 

Answers:

1 : B

2 : C

3 : B

4 : D

5 : C

6 : A

7 : B

8 : C

9 : B

10 : B

11 : B

12 : B

13 : B

14 : C

15 : D

 

Check out my Kahoot to check where your knowledge is at!?

Link to the kahoot.

One thought on “OCP: Functional Programming in Java

  1. Pretty nice post. I just stumbled upon your blog and wished to say that I have really enjoyed
    surfing around your blog posts. In any case I
    will be subscribing to your feed and I hope you write again very soon!

Leave a Reply

Your email address will not be published.