OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions
-
Upload
ganesh-samarthyam -
Category
Software
-
view
282 -
download
2
Transcript of OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions
![Page 1: OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions](https://reader034.fdocuments.us/reader034/viewer/2022050800/587e6ed71a28ab38068b4eb5/html5/thumbnails/1.jpg)
OCP Java SE 8 Exam Sample Questions
Excep&onsandAsser&ons
HariKiran&SGGanesh
![Page 2: OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions](https://reader034.fdocuments.us/reader034/viewer/2022050800/587e6ed71a28ab38068b4eb5/html5/thumbnails/2.jpg)
Ques&onConsiderthefollowingprogram:classChainedExcep/on{publicsta/cvoidfoo(){try{thrownewArrayIndexOutOfBoundsExcep/on();}catch(ArrayIndexOutOfBoundsExcep/onoob){Run/meExcep/onre=newRun/meExcep/on(oob);re.initCause(oob);throwre;}}publicsta/cvoidmain(String[]args){try{foo();}catch(Excep/onre){System.out.println(re.getClass());}}}
h@ps://ocpjava.wordpress.com
Whenexecutedthisprogramfrommain()bycallingfoo()method,printswhichofthefollowing?A. class
java.lang.Run/meExcep/on
B. classjava.lang.IllegalStateExcep/on
C. classjava.lang.Excep/on
D. classjava.lang.ArrayIndexOutOfBoundsExcep/on
![Page 3: OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions](https://reader034.fdocuments.us/reader034/viewer/2022050800/587e6ed71a28ab38068b4eb5/html5/thumbnails/3.jpg)
AnswerConsiderthefollowingprogram:classChainedExcep/on{publicsta/cvoidfoo(){try{thrownewArrayIndexOutOfBoundsExcep/on();}catch(ArrayIndexOutOfBoundsExcep/onoob){Run/meExcep/onre=newRun/meExcep/on(oob);re.initCause(oob);throwre;}}publicsta/cvoidmain(String[]args){try{foo();}catch(Excep/onre){System.out.println(re.getClass());}}}
h@ps://ocpjava.wordpress.com
Whenexecutedthisprogramfrommain()bycallingfoo()method,printswhichofthefollowing?A. class
java.lang.Run/meExcep/on
B. classjava.lang.IllegalStateExcep&on
C. classjava.lang.Excep/on
D. classjava.lang.ArrayIndexOutOfBoundsExcep/on
![Page 4: OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions](https://reader034.fdocuments.us/reader034/viewer/2022050800/587e6ed71a28ab38068b4eb5/html5/thumbnails/4.jpg)
Explana&onB.classjava.lang.IllegalStateExcep/onIntheexpressionnewRun/meExcep/on(oob);,theexcep/onobjectoobisalreadychainedtotheRun/meExcep/onobject.ThemethodinitCause()cannotbecalledonanexcep/onobjectthatalreadyhasanexcep/onobjectchainedduringtheconstructorcall.Hence,thecallre.initCause(oob);resultsininitCause()throwinganIllegalStateExcep/on.
h@ps://ocpjava.wordpress.com
![Page 5: OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions](https://reader034.fdocuments.us/reader034/viewer/2022050800/587e6ed71a28ab38068b4eb5/html5/thumbnails/5.jpg)
Ques&onConsiderthefollowingprogram:
classEHBehavior{publicsta/cvoidmain(String[]args){try{inti=10/0;//LINEASystem.out.print("aXerthrow->");}catch(Arithme/cExcep/onae){System.out.print("incatch->");return;}finally{System.out.print("infinally->");}System.out.print("aXereverything");}}
Whichoneofthefollowingop/onsbestdescribesthebehaviourofthisprogram?A.Theprogramprintsthefollowing:incatch->infinally->aXereverythingB.Theprogramprintsthefollowing:aXerthrow->incatch->infinally->aXereverythingC.Theprogramprintsthefollowing:incatch->aXereverythingD.Theprogramprintsthefollowing:incatch->infinally->
h@ps://ocpjava.wordpress.com
![Page 6: OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions](https://reader034.fdocuments.us/reader034/viewer/2022050800/587e6ed71a28ab38068b4eb5/html5/thumbnails/6.jpg)
AnswerConsiderthefollowingprogram:
classEHBehavior{publicsta/cvoidmain(String[]args){try{inti=10/0;//LINEASystem.out.print("aXerthrow->");}catch(Arithme/cExcep/onae){System.out.print("incatch->");return;}finally{System.out.print("infinally->");}System.out.print("aXereverything");}}
Whichoneofthefollowingop/onsbestdescribesthebehaviourofthisprogram?A.Theprogramprintsthefollowing:incatch->infinally->aXereverythingB.Theprogramprintsthefollowing:aXerthrow->incatch->infinally->aXereverythingC.Theprogramprintsthefollowing:incatch->aXereverythingD.Theprogramprintsthefollowing:incatch->infinally->
h@ps://ocpjava.wordpress.com
![Page 7: OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions](https://reader034.fdocuments.us/reader034/viewer/2022050800/587e6ed71a28ab38068b4eb5/html5/thumbnails/7.jpg)
Explana&onD.Theprogramprintsthefollowing:incatch->infinally->Thestatementprintln("aXerthrow->");willneverbeexecutedsincethelinemarkedwiththecommentLINEAthrowsanexcep/on.ThecatchhandlesArithme/cExcep/on,soprintln("incatch->");willbeexecuted.Followingthat,thereisareturnstatement,sothefunc/onreturns.Butbeforethefunc/onreturns,thefinallystatementshouldbecalled,hencethestatementprintln("infinally->");willgetexecuted.So,thestatementprintln("aXereverything");willnevergetexecuted
h@ps://ocpjava.wordpress.com
![Page 8: OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions](https://reader034.fdocuments.us/reader034/viewer/2022050800/587e6ed71a28ab38068b4eb5/html5/thumbnails/8.jpg)
Ques&onConsiderthefollowingprogram:importjava.u/l.Scanner;classAutoCloseableTest{publicsta/cvoidmain(String[]args){try(ScannerconsoleScanner=newScanner(System.in)){consoleScanner.close();//CLOSEconsoleScanner.close();}}}
Whichoneofthefollowingstatementsiscorrect?A.Thisprogramterminatesnormallywithoutthrowinganyexcep/onsB.ThisprogramthrowsanIllegalStateExcep/onC.ThisprogramthrowsanIOExcep/onD.ThisprogramthrowsanAlreadyClosedExcep/on
h@ps://ocpjava.wordpress.com
![Page 9: OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions](https://reader034.fdocuments.us/reader034/viewer/2022050800/587e6ed71a28ab38068b4eb5/html5/thumbnails/9.jpg)
AnswerConsiderthefollowingprogram:importjava.u/l.Scanner;classAutoCloseableTest{publicsta/cvoidmain(String[]args){try(ScannerconsoleScanner=newScanner(System.in)){consoleScanner.close();//CLOSEconsoleScanner.close();}}}
Whichoneofthefollowingstatementsiscorrect?A.Thisprogramterminatesnormallywithoutthrowinganyexcep&onsB.ThisprogramthrowsanIllegalStateExcep/onC.ThisprogramthrowsanIOExcep/onD.ThisprogramthrowsanAlreadyClosedExcep/on
h@ps://ocpjava.wordpress.com
![Page 10: OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions](https://reader034.fdocuments.us/reader034/viewer/2022050800/587e6ed71a28ab38068b4eb5/html5/thumbnails/10.jpg)
Explana&onA.Thisprogramterminatesnormallywithoutthrowinganyexcep/onsThetry-with-resourcesstatementinternallyexpandstocalltheclose()methodinthefinallyblock.Iftheresourceisexplicitlyclosedinthetryblock,thencallingclose()againdoesnothaveanyeffect.Fromthedescrip/onoftheclose()methodintheAutoCloseableinterface:“Closesthisstreamandreleasesanysystemresourcesassociatedwithit.Ifthestreamisalreadyclosed,theninvokingthismethodhasnoeffect.”
h@ps://ocpjava.wordpress.com
![Page 11: OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions](https://reader034.fdocuments.us/reader034/viewer/2022050800/587e6ed71a28ab38068b4eb5/html5/thumbnails/11.jpg)
Ques&onConsiderthefollowingprogram:classAsser/onFailure{publicsta/cvoidmain(String[]args){try{assertfalse;}catch(Run/meExcep/onre){System.out.println("Run/meExcep/on");}catch(Excep/one){System.out.println("Excep/on");}catch(Errore){//LINEASystem.out.println("Error"+e);}catch(Throwablet){System.out.println("Throwable");}}}
h@ps://ocpjava.wordpress.com
Thisprogramisinvokedfromthecommandlineasfollows:javaAsser)onFailureChooseoneofthefollowingop/onsdescribesthebehaviourofthisprogram:A.Prints"Run/meExcep/on"inconsoleB.Prints"Excep/on"C.Prints"Error"D.Prints"Throwable"E.Doesnotprintanyoutputonconsole
![Page 12: OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions](https://reader034.fdocuments.us/reader034/viewer/2022050800/587e6ed71a28ab38068b4eb5/html5/thumbnails/12.jpg)
AnswerConsiderthefollowingprogram:classAsser/onFailure{publicsta/cvoidmain(String[]args){try{assertfalse;}catch(Run/meExcep/onre){System.out.println("Run/meExcep/on");}catch(Excep/one){System.out.println("Excep/on");}catch(Errore){//LINEASystem.out.println("Error"+e);}catch(Throwablet){System.out.println("Throwable");}}}
h@ps://ocpjava.wordpress.com
Thisprogramisinvokedfromthecommandlineasfollows:javaAsser)onFailureChooseoneofthefollowingop/onsdescribesthebehaviourofthisprogram:A.Prints"Run/meExcep/on"inconsoleB.Prints"Excep/on"C.Prints"Error"D.Prints"Throwable"E.Doesnotprintanyoutputonconsole
![Page 13: OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions](https://reader034.fdocuments.us/reader034/viewer/2022050800/587e6ed71a28ab38068b4eb5/html5/thumbnails/13.jpg)
Explana&onE.DoesnotprintanyoutputontheconsoleBydefault,asser/onsaredisabled.If-ea(orthe-enableasser/onsop/ontoenableasser/ons),thentheprogramwouldhaveprinted"Error"sincetheexcep/onthrowninthecaseofasser/onfailureisjava.lang.Asser/onError,whichisderivedfromtheErrorclass
h@ps://ocpjava.wordpress.com
![Page 14: OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions](https://reader034.fdocuments.us/reader034/viewer/2022050800/587e6ed71a28ab38068b4eb5/html5/thumbnails/14.jpg)
Ques&onConsiderthefollowingclasshierarchyfromthepackagejavax.security.auth.loginandanswertheques&ons.Whichofthefollowinghandlersthatmakesuseofmul/-catchexcep/onhandlerfeaturewillcompilewithouterrors?A.catch(AccountExcep/on|LoginExcep/onexcep/on)B.catch(AccountExcep/on|AccountExpiredExcep/onexcep/on)C.catch(AccountExpiredExcep/on|AccountNotFoundExcep/onexcep/on)D.catch(AccountExpiredExcep/onexcep/on1|AccountNotFoundExcep/onexcep/on2)
h@ps://ocpjava.wordpress.com
![Page 15: OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions](https://reader034.fdocuments.us/reader034/viewer/2022050800/587e6ed71a28ab38068b4eb5/html5/thumbnails/15.jpg)
AnswerConsiderthefollowingclasshierarchyfromthepackagejavax.security.auth.loginandanswertheques&ons.Whichofthefollowinghandlersthatmakesuseofmul/-catchexcep/onhandlerfeaturewillcompilewithouterrors?A.catch(AccountExcep/on|LoginExcep/onexcep/on)B.catch(AccountExcep/on|AccountExpiredExcep/onexcep/on)C.catch(AccountExpiredExcep&on|AccountNotFoundExcep&onexcep&on)D.catch(AccountExpiredExcep/onexcep/on1|AccountNotFoundExcep/onexcep/on2)
h@ps://ocpjava.wordpress.com
![Page 16: OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions](https://reader034.fdocuments.us/reader034/viewer/2022050800/587e6ed71a28ab38068b4eb5/html5/thumbnails/16.jpg)
Explana&onC.catch(AccountExpiredExcep/on|AccountNotFoundExcep/onexcep/on)ForAandB,thebasetypehandlerisprovidedwiththederivedtypehandler,hencethemul/-catchisincorrect.ForD,theexcep/onnameexcep/on1isredundantandwillresultinasyntaxerror.Cisthecorrectop/onandthiswillcompilefinewithouterrors.
h@ps://ocpjava.wordpress.com
![Page 17: OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions](https://reader034.fdocuments.us/reader034/viewer/2022050800/587e6ed71a28ab38068b4eb5/html5/thumbnails/17.jpg)
• 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