+ All Categories
Home > Software > OCP Java SE 8 Exam - Sample Questions - Generics and Collections

OCP Java SE 8 Exam - Sample Questions - Generics and Collections

Date post: 14-Jan-2017
Category:
Upload: ganesh-samarthyam
View: 259 times
Download: 3 times
Share this document with a friend
17
OCP Java SE 8 Exam Sample Questions Generics and Collec.ons Hari Kiran & S G Ganesh
Transcript

OCP Java SE 8 Exam Sample Questions

GenericsandCollec.ons

HariKiran&SGGanesh

Ques.onChoosethecorrectop.onbasedonthisprogram: importjava.u-l.*;classU-li-esTest{publicsta-cvoidmain(String[]args){List<int>intList=newArrayList<>();intList.add(10);intList.add(20);System.out.println("Thelistis:"+intList);}}A.Itprintsthefollowing:Thelistis:[10,20]B.Itprintsthefollowing:Thelistis:[20,10]C.ItresultsinacompilererrorD.Itresultsinarun-meexcep-on

h<ps://ocpjava.wordpress.com

Answer

h<ps://ocpjava.wordpress.com

Choosethecorrectop.onbasedonthisprogram: importjava.u-l.*;classU-li-esTest{publicsta-cvoidmain(String[]args){List<int>intList=newArrayList<>();intList.add(10);intList.add(20);System.out.println("Thelistis:"+intList);}}A.Itprintsthefollowing:Thelistis:[10,20]B.Itprintsthefollowing:Thelistis:[20,10]C.ItresultsinacompilererrorD.Itresultsinarun-meexcep-on

Explana.onC.ItresultsinacompilererrorYoucannotspecifyprimi-vetypesalongwithgenerics,soList<int>needstobechangedtoList<Integer>.

h<ps://ocpjava.wordpress.com

Ques.onChoosethecorrectop.onbasedonthisprogram:

importjava.u-l.*;classU-li-esTest{publicsta-cvoidmain(String[]args){List<Integer>intList=newLinkedList<>();List<Double>dblList=newLinkedList<>();System.out.println("Firsttype:"+intList.getClass());System.out.println("Secondtype:"+dblList.getClass());}}

A.Itprintsthefollowing:Firsttype:classjava.u-l.LinkedListSecondtype:classjava.u-l.LinkedListB.Itprintsthefollowing:Firsttype:classjava.u-l.LinkedList<Integer>Secondtype:classjava.u-l.LinkedList<Double>C.ItresultsinacompilererrorD.Itresultsinarun-meexcep-on

h<ps://ocpjava.wordpress.com

Answer

h<ps://ocpjava.wordpress.com

Choosethecorrectop.onbasedonthisprogram:

importjava.u-l.*;classU-li-esTest{publicsta-cvoidmain(String[]args){List<Integer>intList=newLinkedList<>();List<Double>dblList=newLinkedList<>();System.out.println("Firsttype:"+intList.getClass());System.out.println("Secondtype:"+dblList.getClass());}}

A.Itprintsthefollowing:Firsttype:classjava.u.l.LinkedListSecondtype:classjava.u.l.LinkedListB.Itprintsthefollowing:Firsttype:classjava.u-l.LinkedList<Integer>Secondtype:classjava.u-l.LinkedList<Double>C.ItresultsinacompilererrorD.Itresultsinarun-meexcep-on

Explana.onA.Itprintsthefollowing:Firsttype:classjava.u-l.LinkedListSecondtype:classjava.u-l.LinkedListDuetotypeerasure,aYercompila-onbothtypesaretreatedassameLinkedListtype.

h<ps://ocpjava.wordpress.com

Ques.onChoosethecorrectop.onbasedonthisprogram:

importjava.u-l.Arrays;classDefaultSorter{publicsta-cvoidmain(String[]args){String[]brics={"Brazil","Russia","India","China"};Arrays.sort(brics,null);//LINEAfor(Stringcountry:brics){System.out.print(country+"");}}}A.ThisprogramwillresultinacompilererrorinlinemarkedwithcommentLINEAB.Whenexecuted,theprogramprintsthefollowing:BrazilRussiaIndiaChinaC.Whenexecuted,theprogramprintsthefollowing:BrazilChinaIndiaRussiaD.Whenexecuted,theprogramprintsthefollowing:RussiaIndiaChinaBrazilE.Whenexecuted,theprogramthrowsarun-meexcep-onofNullPointerExcep-onwhenexecu-ngthelinemarkedwithcommentLINEAF.Whenexecuted,theprogramthrowsarun-meexcep-onof

h<ps://ocpjava.wordpress.com

Answer

h<ps://ocpjava.wordpress.com

Choosethecorrectop.onbasedonthisprogram:

importjava.u-l.Arrays;classDefaultSorter{publicsta-cvoidmain(String[]args){String[]brics={"Brazil","Russia","India","China"};Arrays.sort(brics,null);//LINEAfor(Stringcountry:brics){System.out.print(country+"");}}}A.ThisprogramwillresultinacompilererrorinlinemarkedwithcommentLINEAB.Whenexecuted,theprogramprintsthefollowing:BrazilRussiaIndiaChinaC.Whenexecuted,theprogramprintsthefollowing:BrazilChinaIndiaRussiaD.Whenexecuted,theprogramprintsthefollowing:RussiaIndiaChinaBrazilE.Whenexecuted,theprogramthrowsarun-meexcep-onofNullPointerExcep-onwhenexecu-ngthelinemarkedwithcommentLINEAF.Whenexecuted,theprogramthrowsarun-meexcep-onof

Explana.onC.Whenexecuted,theprogramprintsthefollowing:BrazilChinaIndiaRussiaWhennullispassedasasecondargumenttotheArrays.sort()method,itmeansthatthedefaultComparable(i.e.,naturalorderingfortheelements)shouldbeused.ThedefaultComparatorresultsinsor-ngtheelementsinascendingorder.TheprogramdoesnotresultinaNullPointerExcep-onoranyotherexcep-onsoracompilererror.

h<ps://ocpjava.wordpress.com

Ques.onChoosethecorrectop.onbasedonthiscodesegment:"abracadabra".chars().dis-nct().peek(ch->System.out.prinb("%c",ch)).sorted();

A.Itprints:“abcdr”B.Itprints:“abrcd”C.Itcrashesbythrowingajava.u-l.IllegalFormatConversionExcep-onD.Thisprogramterminatesnormallywithoutprin-nganyoutputintheconsole

h<ps://ocpjava.wordpress.com

Answer

h<ps://ocpjava.wordpress.com

Choosethecorrectop.onbasedonthiscodesegment:"abracadabra".chars().dis-nct().peek(ch->System.out.prinb("%c",ch)).sorted();

A.Itprints:“abcdr”B.Itprints:“abrcd”C.Itcrashesbythrowingajava.u-l.IllegalFormatConversionExcep-onD.Thisprogramterminatesnormallywithoutprin.nganyoutputintheconsole

Explana.onD.Thisprogramterminatesnormallywithoutprin-nganyoutputintheconsoleAstreampipelineislazilyevaluated.Sincethereisnoterminalopera-onprovided(suchascount,forEach,reduce,orcollect),thispipelineisnotevaluatedandhencethepeekdoesnotprintanyoutputtotheconsole.

h<ps://ocpjava.wordpress.com

Ques.onChoosethecorrectop.onbasedonthisprogram:classConsonants{privatesta-cbooleanremoveVowels(intc){switch(c){case'a':case'e':case'i':case'o':case'u':returntrue;}returnfalse;}publicsta-cvoidmain(String[]args){

"avadakedavra".chars() .filter(Consonants::removeVovels) .forEach(ch->System.out.prinb("%c",ch));

}}A.ThisprogramresultsinacompilererrorB.Thisprogramprints:"aaaeaa"C.Thisprogramprints:"vdkdvr"D.Thisprogramprints:"avadakedavra"E.Thisprogramcrashesbythrowingajava.u-l.IllegalFormatConversionExcep-on

h<ps://ocpjava.wordpress.com

Answer

h<ps://ocpjava.wordpress.com

Choosethecorrectop.onbasedonthisprogram:classConsonants{privatesta-cbooleanremoveVowels(intc){switch(c){case'a':case'e':case'i':case'o':case'u':returntrue;}returnfalse;}publicsta-cvoidmain(String[]args){

"avadakedavra".chars() .filter(Consonants::removeVovels) .forEach(ch->System.out.prinb("%c",ch));

}}A.ThisprogramresultsinacompilererrorB.Thisprogramprints:"aaaeaa"C.Thisprogramprints:"vdkdvr"D.Thisprogramprints:"avadakedavra"E.Thisprogramcrashesbythrowingajava.u-l.IllegalFormatConversionExcep-on

Explana.onB.Thisprogramprints:“aaaeaa”BecausetheConsonants::removeVowelsreturnstruewhenthereisavowelpassed,onlythosecharactersareretainedinthestreambythefiltermethod.Hence,thisprogramprints“aaaeaa”.

h<ps://ocpjava.wordpress.com

•  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 resources here)

http://facebook.com/ocpjava


Recommended