20
OCP Java SE 8 Exam Sample Questions Lambda Expressions Hari Kiran & S G Ganesh

Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

Embed Size (px)

Citation preview

Page 1: Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

OCP Java SE 8 Exam Sample Questions

Lambda Expressions

Hari Kiran & S G Ganesh

Page 2: Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

Question Which of these are valid lambda expressions (select ALL that apply):

A. (int x) -> x + xB. x -> x % xC. -> 7D. (arg1, int arg2) -> arg1 / arg2

https://ocpjava.wordpress.com

Page 3: Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

AnswerWhich of these are valid lambda expressions (select ALL that apply):

A. (int x) -> x + xB. x -> x % xC. -> 7D. (arg1, int arg2) -> arg1 / arg2

https://ocpjava.wordpress.com

Page 4: Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

ExplanationA. & 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 / arg2if argument types are provided, then it should be providedfor all the arguments, or none of them

https://ocpjava.wordpress.com

Page 5: Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

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 #1B. Prints evenC. Prints oddD. Runtime error (throws exception)

https://ocpjava.wordpress.com

Page 6: Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

AnswerDetermine 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 #1B. Prints evenC. Prints oddD. Runtime error (throws exception)

https://ocpjava.wordpress.com

Page 7: Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

ExplanationB. 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

Page 8: Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

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 helloayB. Prints helloeC. Prints eayD. Compiler error

https://ocpjava.wordpress.com

Page 9: Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

AnswerPredict 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 helloayB. Prints helloeC. Prints eayD. Compiler error

Latin.java:7: error: local variables referenced from a lambda expression must be final or effectively final

https://ocpjava.wordpress.com

Page 10: Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

ExplanationInside 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

Page 11: Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

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

Page 12: Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

AnswerWhich 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

Page 13: Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

ExplanationD. 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

Page 14: Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

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; // #1System.out.println(lambdaFunction.apply(10));

}}

A. This program results in a compiler error: interfaces cannot be defined insideclassesB. This program results in a compiler error: @FunctionalInterface used forLambdaFunction that defines two abstract methodsC. This program results in a compiler error in code marked with #1: syntax errorD. This program compiles without errors, and when run, it prints 100 in console

https://ocpjava.wordpress.com

Page 15: Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

AnswerPredict 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; // #1System.out.println(lambdaFunction.apply(10));

}}

A. This program results in a compiler error: interfaces cannot be defined insideclassesB. This program results in a compiler error: @FunctionalInterface used forLambdaFunction that defines two abstract methodsC. This program results in a compiler error in code marked with #1: syntax errorD. This program compiles without errors, and when run, it prints 100 in console

https://ocpjava.wordpress.com

Page 16: Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

ExplanationD. 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

Page 17: Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

Question Predict the output of below program:

interface DoNothing { default void doNothing() { System.out.println("doNothing"); }}@FunctionalInterfaceinterface 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: cannotoverride default method to be an abstract methodB. This program prints: be idleC. This program prints: doNothingD. This program results in a compiler error: DontDoAnything is not a functional interfacehttps://ocpjava.wordpress.com

Page 18: Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

AnswerPredict the output of below program:

interface DoNothing { default void doNothing() { System.out.println("doNothing"); }}@FunctionalInterfaceinterface 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: cannotoverride default method to be an abstract methodB. This program prints: be idleC. This program prints: doNothingD. This program results in a compiler error: DontDoAnything is not a functional interfacehttps://ocpjava.wordpress.com

Page 19: Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

ExplanationB. 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 calledD. DontDoNothing is a functional interface because it has an abstract method

https://ocpjava.wordpress.com

Page 20: Oracle Certified Professional Java Programmer, SE 8 exam - Sample Quiz Questions

20

• Check out our latest book for OCPJP 8 exam preparation

• http://amzn.to/1NNtho2

• www.apress.com/9781484218358 (download source code here)

• https://ocpjava.wordpress.com (more ocpjp 8 resource here)

http://facebook.com/ocpjava