QuestionWhatwillbethebehavior ofrunning:
java-server-clientHelloWorld
A. Error.B. ItwillrunintheservermodeC. ItwillrunintheclientmodeD. Can'tguess!
AnswerWhatwillbethebehavior ofrunning:
java-server-clientHelloWorld
A. Error.B. ItwillrunintheservermodeC. ItwillrunintheclientmodeD. Can'tguess!
QuestionHowarelambdaexpressionstranslatedbytheJavacompilerandexecutedbytheJVM?
A.LambdaexpressionsaretranslatedtoanonymousclassesB.Compilertranslatesto“invokedynamic”methodinstructionC.Compilertranslatesto“invokevirtual”methodinstructionD.Compilertranslatesto“invokestatic”methodinstruction
AnswerHowarelambdaexpressionstranslatedbytheJavacompilerandexecutedbytheJVM?
A.LambdaexpressionsaretranslatedtoanonymousclassesB.Compilertranslatesto“invokedynamic”methodinstructionC.Compilertranslatesto“invokevirtual”methodinstructionD.Compilertranslatesto“invokestatic”methodinstruction
QuestionWhichofthefollowingwasNOTintroducedinJavaversion8?
A. LambdafunctionsB. StreamsAPIC. invokedynamic methodinstructionD. Joda libraryasdate/timeAPI
AnswerWhichofthefollowingwasNOTintroducedinJavaversion8?
A. LambdafunctionsB. StreamsAPIC. invokedynamic methodinstructionD. Joda libraryasdate/timeAPI
ExplanationC.invokedynamic methodinstruction
Lambdafunctions,streamsAPI,andJoda libraryasdate/timeAPIwereintroducedinJavaversion8.
Theinvokedynamic methodinstructionwasintroducedinJava7.
[WhenfunctionalprogrammingsupportwasaddedintheformoflambdafunctionswasaddedinJava8,thecompilerandJVMwereenhancedtouseinvokedynamicinstruction.]
Questionclass Base{}class DeriOne extends Base{}class DeriTwo extends Base{}
class ArrayStore {publicstaticvoidmain(String[]args){
Base[]baseArr =new DeriOne[3];baseArr[0]=new DeriOne();baseArr[2]=new DeriTwo();System.out.println(baseArr.length);
}}
A.Thisprogramprintsthefollowing:3B.Thisprogramprintsthefollowing:2C.ThisprogramthrowsanArrayStoreExceptionD.ThisprogramthrowsanArrayIndexOutOfBoundsException
Answerclass Base{}class DeriOne extends Base{}class DeriTwo extends Base{}
class ArrayStore {publicstaticvoidmain(String[]args){
Base[]baseArr =new DeriOne[3];baseArr[0]=new DeriOne();baseArr[2]=new DeriTwo();System.out.println(baseArr.length);
}}
A.Thisprogramprintsthefollowing:3B.Thisprogramprintsthefollowing:2C.ThisprogramthrowsanArrayStoreExceptionD.ThisprogramthrowsanArrayIndexOutOfBoundsException
ExplanationC.ThisprogramthrowsanArrayStoreException
ThevariablebaseArrisoftypeBase[],anditpointstoanarrayoftypeDeriOne.
However,inthestatementbaseArr[2]=newDeriTwo(),anobjectoftypeDeriTwoisassignedtothetypeDeriOne,whichdoesnotshareaparent-childinheritancerelationship-theyonlyhaveacommonparent,whichisBase.Hence,thisassignmentresultsinanArrayStoreException.
Discussion:Whatisthebestpracticehere?
Questionimportjava.util.*;
class UtilitiesTest {public static voidmain(String[]args){
List<Integer>intList =new LinkedList<>();List<Double>dblList =new LinkedList<>();System.out.println(intList.getClass()==dblList.getClass());
}}
A.Itprints:trueB.Itprints:falseC.ItresultsinacompilererrorD.Itresultsinaruntimeexception
Answerimportjava.util.*;
class UtilitiesTest {public static voidmain(String[]args){
List<Integer>intList =new LinkedList<>();List<Double>dblList =new LinkedList<>();System.out.println(intList.getClass()==dblList.getClass());
}}
A.Itprints:trueB.Itprints:falseC.ItresultsinacompilererrorD.Itresultsinaruntimeexception
ExplanationA.Itprints:true
Duetotypeerasure,aftercompilationbothtypesaretreatedassameLinkedListtype.
Firsttype:classjava.util.LinkedListSecondtype:classjava.util.LinkedList
Discussion:Whatisthebestpracticehere?
QuestionConsiderthefollowingprogram:
class AutoCloseableTest {public static void main(String[]args){
try (ScannerconsoleScanner =new Scanner(System.in)){consoleScanner.close();//CLOSEconsoleScanner.close();
}}
}
Whichoneofthefollowingstatementsiscorrect?A.ThisprogramterminatesnormallywithoutthrowinganyexceptionsB.ThisprogramthrowsanIllegalStateExceptionC.ThisprogramthrowsanIOExceptionD.ThisprogramthrowsanAlreadyClosedExceptionE.ThisprogramresultsinacompilererrorinthelinemarkedwiththecommentCLOSE
AnswerConsiderthefollowingprogram:
class AutoCloseableTest {public static void main(String[]args){
try (ScannerconsoleScanner =new Scanner(System.in)){consoleScanner.close();//CLOSEconsoleScanner.close();
}}
}
Whichoneofthefollowingstatementsiscorrect?A.ThisprogramterminatesnormallywithoutthrowinganyexceptionsB.ThisprogramthrowsanIllegalStateExceptionC.ThisprogramthrowsanIOExceptionD.ThisprogramthrowsanAlreadyClosedExceptionE.ThisprogramresultsinacompilererrorinthelinemarkedwiththecommentCLOSE
ExplanationA.Thisprogramterminatesnormallywithoutthrowinganyexceptions.
Thetry-with-resourcesstatementinternallyexpandstocalltheclose()methodinthefinallyblock.Iftheresourceisexplicitlyclosedinthetryblock,thencallingclose()againdoesnothaveanyeffect.Fromthedescriptionoftheclose()methodintheAutoCloseable interface:“Closesthisstreamandreleasesanysystemresourcesassociatedwithit.Ifthestreamisalreadyclosed,theninvokingthismethodhasnoeffect.”
Discussion:Whatisthebestpracticehere?
QuestionTherearetwokindsofstreamsinthejava.iopackage:characterstreams(i.e.,thosederivingfromReaderandWriterinterfaces)andbytestreams(i.e.,thosederivingfromInputStream andOutputStream).Whichofthefollowingstatementsistrueregardingthedifferencesbetweenthesetwokindsofstreams?
A.Incharacterstreams,dataishandledintermsofbytes;inbytestreams,dataishandledintermsofUnicodecharacters.B.Characterstreamsaresuitableforreadingorwritingtofilessuchasexecutablefiles,imagefiles,andfilesinlow-levelfileformatssuchas.zip,.classC.Bytestreamsaresuitableforreadingorwritingtotext-basedI/Osuchasdocumentsandtext,XML,andHTMLfiles.D.Bytestreamsaremeantforhandlingbinarydatathatisnothuman-readable;characterstreamsaremeantforhuman-readablecharacters.
AnswerTherearetwokindsofstreamsinthejava.iopackage:characterstreams(i.e.,thosederivingfromReaderandWriterinterfaces)andbytestreams(i.e.,thosederivingfromInputStream andOutputStream).Whichofthefollowingstatementsistrueregardingthedifferencesbetweenthesetwokindsofstreams?
A.Incharacterstreams,dataishandledintermsofbytes;inbytestreams,dataishandledintermsofUnicodecharacters.B.Characterstreamsaresuitableforreadingorwritingtofilessuchasexecutablefiles,imagefiles,andfilesinlow-levelfileformatssuchas.zip,.classC.Bytestreamsaresuitableforreadingorwritingtotext-basedI/Osuchasdocumentsandtext,XML,andHTMLfiles.D.Bytestreamsaremeantforhandlingbinarydatathatisnothuman-readable;characterstreamsaremeantforhuman-readablecharacters.
ExplanationD.Bytestreamsaremeantforhandlingbinarydatathatisnothumanreadable;characterstreamsareforhuman-readablecharacters.
Incharacterstreams,dataishandledintermsofUnicodecharacters,whereasinbytestreams,dataishandledintermsofbytes.Bytestreamsaresuitableforreadingorwritingtofilessuchasexecutablefiles,imagefiles,andfilesinlow-levelfileformatssuchas.zip,.class,and.jar.Characterstreamsaresuitableforreadingorwritingtotext-basedI/Osuchasdocumentsandtext,XML,andHTMLfiles.
Discussion:Whatisthebestpracticehere?
QuestionWhichoneofthefollowingoptionsisbestsuitedforgeneratingrandomnumbersinamulti-threadedapplication?
A.Usingjava.lang.Math.random()B.Usingjava.util.concurrent.ThreadLocalRandomC.Usingjava.util.RandomAccessD.Usingjava.lang.ThreadLocal<T>
AnswerWhichoneofthefollowingoptionsisbestsuitedforgeneratingrandomnumbersinamulti-threadedapplication?
A.Usingjava.lang.Math.random()B.Usingjava.util.concurrent.ThreadLocalRandomC.Usingjava.util.RandomAccessD.Usingjava.lang.ThreadLocal<T>
Explanationb)Usingjava.util.concurrent.ThreadLocalRandom
java.lang.Math.random()isnotefficientforconcurrentprograms.UsingThreadLocalRandomresultsinlessoverheadandcontentionwhencomparedtousingRandomobjectsinconcurrentprograms(andhenceusingthisclasstypeisthebestoptioninthiscase).
java.util.RandomAccess isunrelatedtorandomnumbergeneration.
ThreadLocal<T>classprovidessupportforcreatingthread-localvariables.
Discussion:Whatisthebestpracticehere?
QuestionConsiderthefollowingcodesegment:
while((ch =inputFile.read())!=VALUE){outputFile.write((char)ch );
}
AssumethatinputFile isoftypeFileReader ,andoutputFile isoftypeFileWriter ,andch isoftypeint .Themethodread()returnsthecharacterifsuccessful,orVALUEiftheendofthestreamhasbeenreached.WhatisthecorrectvalueofthisVALUEcheckedinthewhileloopforend-of-stream?
A.-1B.0C.255D.Integer.MAX_VALUEE.Integer.MIN_VALUE
AnswerConsiderthefollowingcodesegment:
while((ch =inputFile.read())!=VALUE){outputFile.write((char)ch );
}
AssumethatinputFile isoftypeFileReader ,andoutputFile isoftypeFileWriter ,andch isoftypeint .Themethodread()returnsthecharacterifsuccessful,orVALUEiftheendofthestreamhasbeenreached.WhatisthecorrectvalueofthisVALUEcheckedinthewhileloopforend-of-stream?
A.-1B.0C.255D.Integer.MAX_VALUEE.Integer.MIN_VALUE
ExplanationA.-1
Theread()methodreturnsthevalue-1ifend-of-stream(EOS)isreached,whichischeckedinthiswhileloop.
Discussion:Whatisthebestpracticehere?
QuestionConsiderthefollowingprogramanddeterminetheoutput:
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(Stringargs[]){
Testtest =new Test();test.print(10);
}}
A.Theprogramresultsinacompilererror(“ambiguousoverload”)B.longC.IntegerD.int
AnswerConsiderthefollowingprogramanddeterminetheoutput:
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(Stringargs[]){
Testtest =new Test();test.print(10);
}}
A.Theprogramresultsinacompilererror(“ambiguousoverload”)B.longC.IntegerD.int
ExplanationD.int
IfIntegerandlongtypesarespecified,aliteralwillmatchtoint.So,theprogramprintsint.
Discussion:Whatisthebestpracticehere?
Meetups
• JavaScript-Meetup-Bangalore• Core-Java-Meetup-Bangalore• SoftwareArchitectsBangalore• Container-Developers-Meetup• CloudOps-Meetup-Bangalore• Software-Craftsmanship-Bangalore• Mobile-App-Developers-Bangalore• Bangalore-SDN-IoT-NetworkVirtualization-Enthusiasts
UpcomingBootcamps
• DockerHands-onBootcamp - Oct15th• AngularJS Bootcamp – Oct22nd• ModernSoftwareArchitecture – Nov5th• SOLIDPrinciples&DesignPatterns – Nov19th
PleasevisitCodeOps.tech Upcomings sectionformoredetailssuchasagenda/cost/trainerandregistration.
UseFLAT750couponcodeto
getRs 750discount