Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
OCP Java SE 8 Exam
Sample Questions
Lambda Expressions
Hari Kiran & S G Ganesh
Question
Which of these are valid lambda expressions (select ALL
that apply):
A. (int x) -> x + x
B. x -> x % x
C. -> 7
D....
Answer
Which of these are valid lambda expressions (select ALL that apply):
A. (int x) -> x + x
B. x -> x % x
C. -> 7
D. (...
Explanation
A. & B. are correct lambda expressions.
Why other options are wrong:
C. -> 7. if no parameters, then empty par...
Question
Determine the behaviour of the following program:
class BlockLambda {
interface LambdaFunction {
String intKind(i...
Answer
Determine the behaviour of the following program:
class BlockLambda {
interface LambdaFunction {
String intKind(int...
Explanation
B. is the correct answer as the expression evaluates input
value as even
Why other options are wrong:
A. There...
Question
Predict the output of below program:
interface SuffixFunction {
void call();
}
class Latin {
public static void m...
Answer
Predict the output of below program:
interface SuffixFunction {
void call();
}
class Latin {
public static void mai...
Explanation
Inside the lambda expression, we are using the local variable
word. Because it is used in a lambda expression,...
Question
Which of the following has correct usage of
functional interfaces and doesn’t result in compilation error
(select...
Answer
Which of the following has correct usage of
functional interfaces and doesn’t result in compilation error
(select a...
Explanation
D. This interface is a functional interface though it declares
two abstract methods: compare() and equals() me...
Question
Predict the output of below program:
class LambdaFunctionTest {
@FunctionalInterface
interface LambdaFunction {
i...
Answer
Predict the output of below program:
class LambdaFunctionTest {
@FunctionalInterface
interface LambdaFunction {
int...
Explanation
D. is the correct answer as this program compiles without
errors, and when run, it prints 100 in console.
Why ...
Question
Predict the output of below program:
interface DoNothing {
default void doNothing() { System.out.println("doNothi...
Answer
Predict the output of below program:
interface DoNothing {
default void doNothing() { System.out.println("doNothing...
Explanation
B. is the correct answer as the call beIdle.doNothing() calls the
System.out.println given in the lambda expre...
20
• Check out our latest book for
OCPJP 8 exam preparation
• http://amzn.to/1NNtho2
• www.apress.com/9781484218
358 (down...
Upcoming SlideShare
Loading in …5
×

OCP Java SE 8 Exam Sample Questions

5,775 views

Published on

Are you a Java enthusiast and preparing for Java 8 certification exam (OCP Java SE 8)? Try out this Quiz on lambda expressions.

This quiz has answers with detailed explanation. At the end of the quiz there is an exercise along with answer

Published in: Software

OCP Java SE 8 Exam Sample Questions

  1. 1. OCP Java SE 8 Exam Sample Questions Lambda Expressions Hari Kiran & S G Ganesh
  2. 2. Question Which of these are valid lambda expressions (select ALL that apply): A. (int x) -> x + x B. x -> x % x C. -> 7 D. (arg1, int arg2) -> arg1 / arg2 https://ocpjava.wordpress.com
  3. 3. Answer Which of these are valid lambda expressions (select ALL that apply): A. (int x) -> x + x B. x -> x % x C. -> 7 D. (arg1, int arg2) -> arg1 / arg2 https://ocpjava.wordpress.com
  4. 4. Explanation A. & B. are correct lambda expressions. Why other options are wrong: C. -> 7. if no parameters, then empty parenthesis () must be provided i.e., ()->7 D. (arg1, int arg2) -> arg1 / arg2 if argument types are provided, then it should be provided for all the arguments, or none of them https://ocpjava.wordpress.com
  5. 5. Question Determine the behaviour of the following program: class BlockLambda { interface LambdaFunction { String intKind(int a); } public static void main(String []args) { LambdaFunction lambdaFunction = (int i) -> { //#1 if((i % 2) == 0) return "even"; else return "odd"; }; System.out.println(lambdaFunction.intKind(10)); } } A. Compiler error at #1 B. Prints even C. Prints odd D. Runtime error (throws exception) https://ocpjava.wordpress.com
  6. 6. Answer Determine the behaviour of the following program: class BlockLambda { interface LambdaFunction { String intKind(int a); } public static void main(String []args) { LambdaFunction lambdaFunction = (int i) -> { //#1 if((i % 2) == 0) return "even"; else return "odd"; }; System.out.println(lambdaFunction.intKind(10)); } } A. Compiler error at #1 B. Prints even C. Prints odd D. Runtime error (throws exception) https://ocpjava.wordpress.com
  7. 7. Explanation B. is the correct answer as the expression evaluates input value as even Why other options are wrong: A. There is no compilation error, this is correct way of defining block lambda C. Input value passed is 10 so the expression returns even D. This program doesn’t thrown any runtime exceptions https://ocpjava.wordpress.com
  8. 8. Question Predict the output of below program: interface SuffixFunction { void call(); } class Latin { public static void main(String []args) { String word = "hello"; SuffixFunction suffixFunc = () -> System.out.println(word + "ay"); word = "e"; suffixFunc.call(); } } Choose the correct option A. Prints helloay B. Prints helloe C. Prints eay D. Compiler error https://ocpjava.wordpress.com
  9. 9. Answer Predict the output of below program: interface SuffixFunction { void call(); } class Latin { public static void main(String []args) { String word = "hello"; SuffixFunction suffixFunc = () -> System.out.println(word + "ay"); word = "e"; suffixFunc.call(); } } Choose the correct option A. Prints helloay B. Prints helloe C. Prints eay D. Compiler error Latin.java:7: error: local variables referenced from a lambda expression must be final or effectively final https://ocpjava.wordpress.com
  10. 10. Explanation Inside the lambda expression, we are using the local variable word. Because it is used in a lambda expression, this variable is considered to be final (though it is not explicitly declared final). Hence A, B and C are incorrect options Snippet String word = "hello"; SuffixFunction suffixFunc = () -> System.out.println(word + "ay"); word = "e"; https://ocpjava.wordpress.com
  11. 11. Question Which of the following has correct usage of functional interfaces and doesn’t result in compilation error (select all that apply): A. @FunctionalInterface public abstract class AnnotationTest { abstract int foo(); } B. @FunctionalInterface public interface AnnotationTest { default int foo() {}; } C. @FunctionalInterface public interface AnnotationTest { /* no methods provided */ } D. @FunctionalInterface public interface Comparator<T> { int compare(T o1, T o2); boolean equals(Object obj); } https://ocpjava.wordpress.com
  12. 12. Answer Which of the following has correct usage of functional interfaces and doesn’t result in compilation error (select all that apply): A. @FunctionalInterface public abstract class AnnotationTest { abstract int foo(); } B. @FunctionalInterface public interface AnnotationTest { default int foo() {}; } C. @FunctionalInterface public interface AnnotationTest { /* no methods provided */ } D. @FunctionalInterface public interface Comparator<T> { int compare(T o1, T o2); boolean equals(Object obj); } https://ocpjava.wordpress.com
  13. 13. Explanation D. This interface is a functional interface though it declares two abstract methods: compare() and equals() methods. How is it a functional interface when it has two abstract methods? Because equals() method signature matches from Object , and the compare() method is the only remaining abstract method, and hence the Comparator interface is a functional interface. Why other options are wrong: A. An abstract class cannot be declared with annotation @FunctionalInterface C. and B. doesn’t have abstract methods. Hence they do no qualify to be declared as functional interfaces https://ocpjava.wordpress.com
  14. 14. Question Predict the output of below program: class LambdaFunctionTest { @FunctionalInterface interface LambdaFunction { int apply(int j); boolean equals(java.lang.Object arg0); } public static void main(String []args) { LambdaFunction lambdaFunction = i -> i * i; // #1 System.out.println(lambdaFunction.apply(10)); } } A. This program results in a compiler error: interfaces cannot be defined inside classes B. This program results in a compiler error: @FunctionalInterface used for LambdaFunction that defines two abstract methods C. This program results in a compiler error in code marked with #1: syntax error D. This program compiles without errors, and when run, it prints 100 in console https://ocpjava.wordpress.com
  15. 15. Answer Predict the output of below program: class LambdaFunctionTest { @FunctionalInterface interface LambdaFunction { int apply(int j); boolean equals(java.lang.Object arg0); } public static void main(String []args) { LambdaFunction lambdaFunction = i -> i * i; // #1 System.out.println(lambdaFunction.apply(10)); } } A. This program results in a compiler error: interfaces cannot be defined inside classes B. This program results in a compiler error: @FunctionalInterface used for LambdaFunction that defines two abstract methods C. This program results in a compiler error in code marked with #1: syntax error D. This program compiles without errors, and when run, it prints 100 in console https://ocpjava.wordpress.com
  16. 16. Explanation D. is the correct answer as this program compiles without errors, and when run, it prints 100 in console. Why other options are wrong: A. An interface can be defined inside a class B. The signature of the equals method matches that of the equal method in Object class; hence it is not counted as an abstract method in the functional interface C. It is acceptable to omit the parameter type when there is only one parameter and the parameter and return type are inferred from the LambdaFunction abstract method declaration int apply(int j) https://ocpjava.wordpress.com
  17. 17. Question Predict the output of below program: interface DoNothing { default void doNothing() { System.out.println("doNothing"); } } @FunctionalInterface interface DontDoAnything extends DoNothing { @Override abstract void doNothing(); } class LambdaTest { public static void main(String []args) { DontDoAnything beIdle = () -> System.out.println("be idle"); beIdle.doNothing(); } } A. This program results in a compiler error for DontDoAnything interface: cannot override default method to be an abstract method B. This program prints: be idle C. This program prints: doNothing D. This program results in a compiler error: DontDoAnything is not a functional interface https://ocpjava.wordpress.com
  18. 18. Answer Predict the output of below program: interface DoNothing { default void doNothing() { System.out.println("doNothing"); } } @FunctionalInterface interface DontDoAnything extends DoNothing { @Override abstract void doNothing(); } class LambdaTest { public static void main(String []args) { DontDoAnything beIdle = () -> System.out.println("be idle"); beIdle.doNothing(); } } A. This program results in a compiler error for DontDoAnything interface: cannot override default method to be an abstract method B. This program prints: be idle C. This program prints: doNothing D. This program results in a compiler error: DontDoAnything is not a functional interface https://ocpjava.wordpress.com
  19. 19. Explanation B. is the correct answer as the call beIdle.doNothing() calls the System.out.println given in the lambda expression and hence it prints “be idle” on the console Why other options are wrong: A. A default method can be overridden in a derived interface and can be made abstract C. DoNothing.doNothing() will not be called D. DontDoNothing is a functional interface because it has an abstract method https://ocpjava.wordpress.com
  20. 20. 20 • Check out our latest book for OCPJP 8 exam preparation • http://amzn.to/1NNtho2 • www.apress.com/9781484218 358 (download source code here) • https://ocpjava.wordpress.com (more ocpjp 8 resource here) http://facebook.com/ocpjava

×