+ All Categories
Home > Software > OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions

OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions

Date post: 15-Apr-2017
Category:
Upload: ganesh-samarthyam
View: 286 times
Download: 2 times
Share this document with a friend
17
OCP Java SE 8 Exam Sample Questions Excep&ons and Asser&ons Hari Kiran & S G Ganesh
Transcript
Page 1: OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions

OCP Java SE 8 Exam Sample Questions

Excep&onsandAsser&ons

HariKiran&SGGanesh

Page 2: OCP Java SE 8 Exam - Sample Questions - Exceptions and Assertions

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

•  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