OCP Java SE 8 Exam
Sample Questions	
Lambda	Expressions	
Hari	Kiran	&	S	G	Ganesh
Ques8on		
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	
	
hDps://ocpjava.wordpress.com
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	
hDps://ocpjava.wordpress.com
Explana8on	
A.	&	B.	are	correct	lambda	expressions.	
	
Why	other	op8ons	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		
	
hDps://ocpjava.wordpress.com
Ques8on		
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.	RunKme	error	(throws	excepKon)	
hDps://ocpjava.wordpress.com
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.	RunKme	error	(throws	excepKon)	
hDps://ocpjava.wordpress.com
Explana8on	
B.	is	the	correct	answer	as	the	expression	evaluates	input	
value	as	even	
	
Why	other	op8ons	are	wrong:	
	
A.	There	is	no	compilaKon	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	runKme	excepKons		
	
hDps://ocpjava.wordpress.com
Ques8on		
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	op8on	
A.	Prints	helloay	
B.	Prints	helloe	
C.	Prints	eay	
D.	Compiler	error	
hDps://ocpjava.wordpress.com
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	op8on	
A.	Prints	helloay	
B.	Prints	helloe	
C.	Prints	eay	
D.	Compiler	error	
LaKn.java:7:	error:	local	variables	referenced	from	a	lambda	expression	must	be	
	final	or	effecKvely	final	
hDps://ocpjava.wordpress.com
Explana8on	
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	opKons	
	
Snippet	
	
String word = "hello";
SuffixFunction suffixFunc = () -> System.out.println(word +
"ay");
word = "e"; 	
hDps://ocpjava.wordpress.com
Ques8on		
Which	of	the	following	has	correct	usage	of		
func8onal	interfaces	and	doesn’t	result	in	compila8on	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);
}
hDps://ocpjava.wordpress.com
Answer	
Which	of	the	following	has	correct	usage	of		
func8onal	interfaces	and	doesn’t	result	in	compila8on	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);
}
hDps://ocpjava.wordpress.com
Explana8on	
D.	This	interface	is	a	funcKonal	interface	though	it	declares	
two	abstract	methods:	compare()	and	equals()	methods.	
How	is	it	a	funcKonal	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	funcKonal	interface.	
	
Why	other	op8ons	are	wrong:	
	
A.	An	abstract	class	cannot	be	declared	with	annotaKon	
@FuncKonalInterface	
	
	
C.	and	B.	doesn’t	have	abstract	methods.	Hence	they	do	no	
qualify	to	be	declared	as	funcKonal	interfaces	
hDps://ocpjava.wordpress.com
Ques8on		
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:	@FuncKonalInterface	used	for	
LambdaFuncKon	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	
hDps://ocpjava.wordpress.com
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:	@FuncKonalInterface	used	for	
LambdaFuncKon	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	
hDps://ocpjava.wordpress.com
Explana8on	
D.	is	the	correct	answer	as	this	program	compiles	without	
errors,	and	when	run,	it	prints	100	in	console.		
Why	other	op8ons	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	funcKonal	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	LambdaFuncKon	abstract	
method	declaraKon	int	apply(int	j)	
hDps://ocpjava.wordpress.com
Ques8on		
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	funcKonal	
interface	
hDps://ocpjava.wordpress.com
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	funcKonal	
interface	
hDps://ocpjava.wordpress.com
Explana8on	
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	op8ons	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	funcKonal	interface	because	it	has	an	
abstract	method	
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

OCP Java SE 8 Exam - Sample Questions - Lambda Expressions