Post on 16-Apr-2017
transcript
OCP Java SE 8 Exam Sample Questions
LambdaExpressions
HariKiran&SGGanesh
Ques8onWhichofthesearevalidlambdaexpressions(selectALLthatapply):
A.(intx)->x+xB.x->x%xC.->7D.(arg1,intarg2)->arg1/arg2
hDps://ocpjava.wordpress.com
AnswerWhichofthesearevalidlambdaexpressions(selectALLthatapply):
A.(intx)->x+xB.x->x%xC.->7D.(arg1,intarg2)->arg1/arg2
hDps://ocpjava.wordpress.com
Explana8onA.&B.arecorrectlambdaexpressions.Whyotherop8onsarewrong:C.->7.ifnoparameters,thenemptyparenthesis()mustbeprovidedi.e.,()->7D.(arg1,intarg2)->arg1/arg2ifargumenttypesareprovided,thenitshouldbeprovidedforallthearguments,ornoneofthem
hDps://ocpjava.wordpress.com
Ques8onDeterminethebehaviourofthefollowingprogram: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.Compilererrorat#1B.PrintsevenC.PrintsoddD.RunKmeerror(throwsexcepKon)
hDps://ocpjava.wordpress.com
AnswerDeterminethebehaviourofthefollowingprogram: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.Compilererrorat#1B.PrintsevenC.PrintsoddD.RunKmeerror(throwsexcepKon)
hDps://ocpjava.wordpress.com
Explana8onB.isthecorrectanswerastheexpressionevaluatesinputvalueasevenWhyotherop8onsarewrong:A.ThereisnocompilaKonerror,thisiscorrectwayofdefiningblocklambdaC.Inputvaluepassedis10sotheexpressionreturnsevenD.Thisprogramdoesn’tthrownanyrunKmeexcepKons
hDps://ocpjava.wordpress.com
Ques8onPredicttheoutputofbelowprogram:
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(); } } Choosethecorrectop8on A.PrintshelloayB.PrintshelloeC.PrintseayD.Compilererror
hDps://ocpjava.wordpress.com
AnswerPredicttheoutputofbelowprogram:
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(); } } Choosethecorrectop8on A.PrintshelloayB.PrintshelloeC.PrintseayD.CompilererrorLaKn.java:7:error:localvariablesreferencedfromalambdaexpressionmustbefinaloreffecKvelyfinal
hDps://ocpjava.wordpress.com
Explana8onInsidethelambdaexpression,weareusingthelocalvariableword.Becauseitisusedinalambdaexpression,thisvariableisconsideredtobefinal(thoughitisnotexplicitlydeclaredfinal).HenceA,BandCareincorrectopKonsSnippet String word = "hello"; SuffixFunction suffixFunc = () -> System.out.println(word + "ay"); word = "e";
hDps://ocpjava.wordpress.com
Ques8onWhichofthefollowinghascorrectusageoffunc8onalinterfacesanddoesn’tresultincompila8onerror(selectallthatapply):
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); }
hDps://ocpjava.wordpress.com
AnswerWhichofthefollowinghascorrectusageoffunc8onalinterfacesanddoesn’tresultincompila8onerror(selectallthatapply):
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); }
hDps://ocpjava.wordpress.com
Explana8onD.ThisinterfaceisafuncKonalinterfacethoughitdeclarestwoabstractmethods:compare()andequals()methods.HowisitafuncKonalinterfacewhenithastwoabstractmethods?Becauseequals()methodsignaturematchesfromObject,andthecompare()methodistheonlyremainingabstractmethod,andhencetheComparatorinterfaceisafuncKonalinterface.Whyotherop8onsarewrong:A.AnabstractclasscannotbedeclaredwithannotaKon@FuncKonalInterfaceC.andB.doesn’thaveabstractmethods.HencetheydonoqualifytobedeclaredasfuncKonalinterfaces
hDps://ocpjava.wordpress.com
Ques8onPredicttheoutputofbelowprogram:
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.Thisprogramresultsinacompilererror:interfacescannotbedefinedinsideclassesB.Thisprogramresultsinacompilererror:@FuncKonalInterfaceusedforLambdaFuncKonthatdefinestwoabstractmethodsC.Thisprogramresultsinacompilererrorincodemarkedwith#1:syntaxerrorD.Thisprogramcompileswithouterrors,andwhenrun,itprints100inconsole
hDps://ocpjava.wordpress.com
AnswerPredicttheoutputofbelowprogram:
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.Thisprogramresultsinacompilererror:interfacescannotbedefinedinsideclassesB.Thisprogramresultsinacompilererror:@FuncKonalInterfaceusedforLambdaFuncKonthatdefinestwoabstractmethodsC.Thisprogramresultsinacompilererrorincodemarkedwith#1:syntaxerrorD.Thisprogramcompileswithouterrors,andwhenrun,itprints100inconsole
hDps://ocpjava.wordpress.com
Explana8onD.isthecorrectanswerasthisprogramcompileswithouterrors,andwhenrun,itprints100inconsole.
Whyotherop8onsarewrong:
A. Aninterfacecanbedefinedinsideaclass
B. ThesignatureoftheequalsmethodmatchesthatoftheequalmethodinObjectclass;henceitisnotcountedasanabstractmethodinthefuncKonalinterface
C. ItisacceptabletoomittheparametertypewhenthereisonlyoneparameterandtheparameterandreturntypeareinferredfromtheLambdaFuncKonabstractmethoddeclaraKonintapply(intj)
hDps://ocpjava.wordpress.com
Ques8onPredicttheoutputofbelowprogram:
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.ThisprogramresultsinacompilererrorforDontDoAnythinginterface:cannotoverridedefaultmethodtobeanabstractmethodB.Thisprogramprints:beidleC.Thisprogramprints:doNothingD.Thisprogramresultsinacompilererror:DontDoAnythingisnotafuncKonalinterface
hDps://ocpjava.wordpress.com
AnswerPredicttheoutputofbelowprogram:
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.ThisprogramresultsinacompilererrorforDontDoAnythinginterface:cannotoverridedefaultmethodtobeanabstractmethodB.Thisprogramprints:beidleC.Thisprogramprints:doNothingD.Thisprogramresultsinacompilererror:DontDoAnythingisnotafuncKonalinterface
hDps://ocpjava.wordpress.com
Explana8onB.isthecorrectanswerasthecallbeIdle.doNothing()callstheSystem.out.printlngiveninthelambdaexpressionandhenceitprints“beidle”ontheconsoleWhyotherop8onsarewrong:A. Adefaultmethodcanbeoverriddeninaderivedinterface
andcanbemadeabstractC.DoNothing.doNothing()willnotbecalledD.DontDoNothingisafuncKonalinterfacebecauseithasanabstractmethod
hDps://ocpjava.wordpress.com
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