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