KEMBAR78
Core Java: Best practices and bytecodes quiz | PDF
Quiz
A fun way to learn more!
With	contributions	from:	
• Manjunathan
• Vaibhav
• Ganesh
www.codeops.tech
Question	
What	query	language	does	Elasticsearch use?
A.	SQL	
B.	Query	DSL	
C.	Query	SSL	
D.	ElasticClient
Answer
What	query	language	does	Elasticsearch use?
A.	SQL	
B.	Query	DSL	
C.	Query	SSL	
D.	ElasticClient
Explanation
B.	Query	DSL	- Query	DSL	is	Elasticsearch’s
native	Query	Domain	Specific	Language	(DSL)
Question	
In	Elasticsearch,	which	filter	can	be	used	to	combine	
multiple	filters?	
A.	term	
B.	range	
C.	exists	
D.	bool
Answer
In	Elasticsearch,	which	filter	can	be	used	to	combine	
multiple	filters?	
A.	term	
B.	range	
C.	exists	
D.	bool
Explanation
D.	bool,	which	is	used	to	combine	multiple	
filters	using	must/should
Question	
In	Elasticsearch,	"Synonym"	is	an	example	of:	
A.	tokenizer
B.	analyzer
C.	token	filter	
D.	character	filter
Answer
In	Elasticsearch,	"Synonym"	is	an	example	of:	
A.	tokenizer
B.	analyzer
C.	token	filter	
D.	character	filter
Explanation
C.	Token	filter
Here	is	the	usage	of	“synonym”	to	filter	based	
on	“synonyms”	example:
Question	
What	will	be	the	behavior of	running:
java	-server	-client	HelloWorld
A. Error.	
B. It	will	run	in	the	server	mode
C. It	will	run	in	the	client	mode
D. Can't	guess!
Answer
What	will	be	the	behavior of	running:
java	-server	-client	HelloWorld
A. Error.	
B. It	will	run	in	the	server	mode
C. It	will	run	in	the	client	mode
D. Can't	guess!
Question	
How	are	lambda	expressions	translated	by	the	Java	
compiler	and	executed	by	the	JVM?	
A.	Lambda	expressions	are	translated	to	
anonymous	classes
B.	Compiler	translates	to	“invokedynamic”	method	
instruction
C.	Compiler	translates	to	“invokevirtual”	method	
instruction
D.	Compiler	translates	to	“invokestatic”	method	
instruction
Answer
How	are	lambda	expressions	translated	by	the	Java	
compiler	and	executed	by	the	JVM?	
A.	Lambda	expressions	are	translated	to	
anonymous	classes
B.	Compiler	translates	to	“invokedynamic”	method	
instruction
C.	Compiler	translates	to	“invokevirtual”	method	
instruction
D.	Compiler	translates	to	“invokestatic”	method	
instruction
Question	
Which	of	the	following	was	NOT	introduced	in	Java	
version	8?
A. Lambda	functions
B. Streams	API
C. invokedynamic method	instruction	
D. Joda library	as	date/time	API
Answer
Which	of	the	following	was	NOT	introduced	in	Java	
version	8?
A. Lambda	functions
B. Streams	API
C. invokedynamic method	instruction	
D. Joda library	as	date/time	API
Explanation
C.	invokedynamic method	instruction	
Lambda	functions,	streams	API,	and	Joda library	as	
date/time	API	were	introduced	in	Java	version	8.	
The	invokedynamic method	instruction	was	introduced	in	
Java	7.	
[When	functional	programming	support	was	added	in	the	
form	of	lambda	functions	was	added	in	Java	8,	the	
compiler	and	JVM	were	enhanced	to	use	invokedynamic
instruction.]
Question	
class Base	{}
class DeriOne extends Base	{}
class DeriTwo extends Base	{}
class ArrayStore {
public	static	void	main(String	[]args)	{
Base	[]	baseArr =	new DeriOne[3];
baseArr[0]	=	new DeriOne();
baseArr[2]	=	new DeriTwo();
System.out.println(baseArr.length);
}
}
A.	This	program	prints	the	following:	3
B.	This	program	prints	the	following:	2
C.	This	program	throws	an	ArrayStoreException
D.	This	program	throws	an	ArrayIndexOutOfBoundsException
Answer
class Base	{}
class DeriOne extends Base	{}
class DeriTwo extends Base	{}
class ArrayStore {
public	static	void	main(String	[]args)	{
Base	[]	baseArr =	new DeriOne[3];
baseArr[0]	=	new DeriOne();
baseArr[2]	=	new DeriTwo();
System.out.println(baseArr.length);
}
}
A.	This	program	prints	the	following:	3
B.	This	program	prints	the	following:	2
C.	This	program	throws	an	ArrayStoreException
D.	This	program	throws	an	ArrayIndexOutOfBoundsException
Explanation
C.	This	program	throws	an	ArrayStoreException
The	variable	baseArr	is	of	type	Base[]	,	and	it	points	to	
an	array	of	type	DeriOne.	
However,	in	the	statement	baseArr[2]	=	new	
DeriTwo(),	an	object	of	type	DeriTwo	is	assigned	to	
the	type	DeriOne,	which	does	not	share	a	parent-child	
inheritance	relationship-they	only	have	a	common	
parent,	which	is	Base.	Hence,	this	assignment	results	
in	an	ArrayStoreException.
Discussion:	What	is	the	best	practice	here?
Question	
import	java.util.*;	
class UtilitiesTest {
public static void main(String	[]args)	{
List<Integer>	intList =	new LinkedList<>();
List<Double>	dblList =	new LinkedList<>();
System.out.println(intList.getClass()	==	dblList.getClass());
}
}
A.	It	prints:	true
B.	It	prints:	false	
C.	It	results	in	a	compiler	error
D.	It	results	in	a	runtime	exception
Answer
import	java.util.*;	
class UtilitiesTest {
public static void main(String	[]args)	{
List<Integer>	intList =	new LinkedList<>();
List<Double>	dblList =	new LinkedList<>();
System.out.println(intList.getClass()	==	dblList.getClass());
}
}
A.	It	prints:	true
B.	It	prints:	false	
C.	It	results	in	a	compiler	error
D.	It	results	in	a	runtime	exception
Explanation
A.	It	prints:	true
Due	to	type	erasure,	after	compilation	both	types	are	
treated	as	same	LinkedList	type.
First	type:	class	java.util.LinkedList
Second	type:	class	java.util.LinkedList
Discussion:	What	is	the	best	practice	here?
Question	
Consider	the	following	program:
class AutoCloseableTest {
public static void main(String	[]args)	{
try (Scanner	consoleScanner =	new Scanner(System.in))	{
consoleScanner.close();	//	CLOSE
consoleScanner.close();
}
}
}
Which	one	of	the	following	statements	is	correct?
A.	This	program	terminates	normally	without	throwing	any	exceptions
B.	This	program	throws	an	IllegalStateException
C.	This	program	throws	an	IOException
D.	This	program	throws	an	AlreadyClosedException
E.	This	program	results	in	a	compiler	error	in	the	line	marked	with	the
comment	CLOSE
Answer
Consider	the	following	program:
class AutoCloseableTest {
public static void main(String	[]args)	{
try (Scanner	consoleScanner =	new Scanner(System.in))	{
consoleScanner.close();	//	CLOSE
consoleScanner.close();
}
}
}
Which	one	of	the	following	statements	is	correct?
A.	This	program	terminates	normally	without	throwing	any	exceptions
B.	This	program	throws	an	IllegalStateException
C.	This	program	throws	an	IOException
D.	This	program	throws	an	AlreadyClosedException
E.	This	program	results	in	a	compiler	error	in	the	line	marked	with	the
comment	CLOSE
Explanation
A	.	This	program	terminates	normally	without	throwing	any	
exceptions.	
The	try-with-resources	statement	internally	expands	to	call	the	
close()	method	in	the	finally	block.	If	the	resource	is	explicitly	
closed	in	the	try	block,	then	calling	close()
again	does	not	have	any	effect.	From	the	description	of	the	
close()	method	in	the	AutoCloseable interface:	“Closes	this	
stream	and	releases	any	system	resources	associated	with	it.	If	
the	stream	is	already	closed,	then	invoking	this	method	has	no	
effect.”
Discussion:	What	is	the	best	practice	here?
Question	
There	are	two	kinds	of	streams	in	the	java.io	package:	character	streams
(i.e.,	those	deriving	from	Reader	and	Writer	interfaces)	and	byte	streams	(i.e.,	
those	deriving	from	InputStream and	OutputStream).	Which	of	the	following	
statements	is	true	regarding	the	differences	between	these	two	kinds	of	
streams?
A.	In	character	streams,	data	is	handled	in	terms	of	bytes;	in	byte	streams,	data
is	handled	in	terms	of	Unicode	characters.
B.	Character	streams	are	suitable	for	reading	or	writing	to	files	such	as
executable	files,	image	files,	and	files	in	low-level	file	formats	such	as	.zip,
.class
C.	Byte	streams	are	suitable	for	reading	or	writing	to	text-based	I/O	such	as
documents	and	text,	XML,	and	HTML	files.
D.	Byte	streams	are	meant	for	handling	binary	data	that	is	not	human-readable;
character	streams	are	meant	for	human-readable	characters.
Answer
There	are	two	kinds	of	streams	in	the	java.io	package:	character	streams
(i.e.,	those	deriving	from	Reader	and	Writer	interfaces)	and	byte	streams	(i.e.,	
those	deriving	from	InputStream and	OutputStream).	Which	of	the	following	
statements	is	true	regarding	the	differences	between	these	two	kinds	of	streams?
A.	In	character	streams,	data	is	handled	in	terms	of	bytes;	in	byte	streams,	data
is	handled	in	terms	of	Unicode	characters.
B.	Character	streams	are	suitable	for	reading	or	writing	to	files	such	as
executable	files,	image	files,	and	files	in	low-level	file	formats	such	as	.zip,
.class
C.	Byte	streams	are	suitable	for	reading	or	writing	to	text-based	I/O	such	as
documents	and	text,	XML,	and	HTML	files.
D.	Byte	streams	are	meant	for	handling	binary	data	that	is	not	human-readable;
character	streams	are	meant	for	human-readable	characters.
Explanation
D	.	Byte	streams	are	meant	for	handling	binary	data	that	is	not	
human	readable;	character	streams	are	for	human-readable	
characters.
In	character	streams,	data	is	handled	in	terms	of	Unicode	
characters,	whereas	in	byte	streams,	data	is	handled	in	terms	of	
bytes.	Byte	streams	are	suitable	for	reading	or	writing	to	files	such	
as	executable	files,	image	files,	and	files	in	low-level	file	formats
such	as	.zip,	.class,	and	.jar.	Character	streams	are	suitable	for	
reading	or	writing	to	text-based	I/O	such	as	documents	and	text,	
XML,	and	HTML	files.
Discussion:	What	is	the	best	practice	here?
Question	
Which	one	of	the	following	options	is	best	suited	for	generating	random
numbers	in	a	multi-threaded	application?
A.	Using	java.lang.Math.random()
B.	Using	java.util.concurrent.ThreadLocalRandom
C.	Using	java.util.RandomAccess
D.	Using	java.lang.ThreadLocal<T>
Answer
Which	one	of	the	following	options	is	best	suited	for	generating	random
numbers	in	a	multi-threaded	application?
A.	Using	java.lang.Math.random()
B.	Using	java.util.concurrent.ThreadLocalRandom
C.	Using	java.util.RandomAccess
D.	Using	java.lang.ThreadLocal<T>
Explanation
b)	Using	java.util.concurrent.ThreadLocalRandom
java.lang.Math.random()	is	not	efficient	for	
concurrent	programs.	Using	ThreadLocalRandom
results	in	less	overhead	and	contention	when	
compared	to	using	Random	objects	in	concurrent	
programs	(and	hence	using	this	class	type	is
the	best	option	in	this	case).
java.util.RandomAccess is	unrelated	to	random	
number	generation.
ThreadLocal<T>	class	provides	support	for	creating	
thread-local	variables.
Discussion:	What	is	the	best	practice	here?
Question	
Consider	the	following	code	segment:
while(	(ch =	inputFile.read())	!=	VALUE)	{
outputFile.write(	(char)ch );
}
Assume	that	inputFile is	of	type	FileReader ,	and	outputFile is	of	type
FileWriter ,	and	ch is	of	type	int .	The	method	read()	returns	the	character
if	successful,	or	VALUE	if	the	end	of	the	stream	has	been	reached.	What	is	
the	correct	value	of	this	VALUE	checked	in	the	while	loop	for	end-of-stream?
A.		-1
B.	0
C.	255
D.	Integer.MAX_VALUE
E.	Integer.MIN_VALUE
Answer
Consider	the	following	code	segment:
while(	(ch =	inputFile.read())	!=	VALUE)	{
outputFile.write(	(char)ch );
}
Assume	that	inputFile is	of	type	FileReader ,	and	outputFile is	of	type
FileWriter ,	and	ch is	of	type	int .	The	method	read()	returns	the	character
if	successful,	or	VALUE	if	the	end	of	the	stream	has	been	reached.	What	is	
the	correct	value	of	this	VALUE	checked	in	the	while	loop	for	end-of-stream?
A.		-1
B.	0
C.	255
D.	Integer.MAX_VALUE
E.	Integer.MIN_VALUE
Explanation
A.		-1
The	read()	method	returns	the	value	-1	if	end-of-
stream	(EOS)	is	reached,	which	is	checked	in	this	while	
loop.
Discussion:	What	is	the	best	practice	here?
Question	
Consider	the	following	program	and	determine	the	output:
class Test	{
public void print(Integer i)	{
System.out.println("Integer");
}
public void print(int i)	{
System.out.println("int");
}
public void print(long i)	{
System.out.println("long");
}
public static void main(String	args[])	{
Test	test =	new Test();
test.print(10);
}
}
A.	The	program	results	in	a	compiler	error	(“ambiguous	overload”)
B.	long
C.	Integer
D.	int
Answer
Consider	the	following	program	and	determine	the	output:
class Test	{
public void print(Integer i)	{
System.out.println("Integer");
}
public void print(int i)	{
System.out.println("int");
}
public void print(long i)	{
System.out.println("long");
}
public static void main(String	args[])	{
Test	test =	new Test();
test.print(10);
}
}
A.	The	program	results	in	a	compiler	error	(“ambiguous	overload”)
B.	long
C.	Integer
D.	int
Explanation
D.	int
If	Integer	and	long	types	are	specified,	a	literal	will	
match	to	int.	So,	the	program	prints	int	.
Discussion:	What	is	the	best	practice	here?
Meetups
• JavaScript-Meetup-Bangalore
• Core-Java-Meetup-Bangalore
• Software	Architects	Bangalore
• Container-Developers-Meetup
• CloudOps-Meetup-Bangalore
• Software-Craftsmanship-Bangalore
• Mobile-App-Developers-Bangalore
• Bangalore-SDN-IoT-NetworkVirtualization-Enthusiasts
Upcoming	Bootcamps
• Docker	Hands-on	Bootcamp - Oct	15th
• AngularJS Bootcamp – Oct	22nd
• Modern	Software	Architecture – Nov	5th
• SOLID	Principles	&	Design	Patterns – Nov	19th
Please	visit	CodeOps.tech	 Upcomings section	for	
more	details	such	as	agenda/cost/trainer	and	
registration.
Use	FLAT750	
coupon	code	to	
get	Rs 750	
discount
www.codeops.tech

Core Java: Best practices and bytecodes quiz