,DOWNLOADgTHEgTestgBankgforgAbsolutegC++g6thgEditiongSavitch
Chapterg2g-gTestgQuestions
Thesegtestgquestionsgaregtrue-
false,gfillgingthegblank,gmultiplegchoice,gandgfreegformgquestionsgthatgmaygrequiregcode.gT
hegmultiplegchoicegquestionsgmayghavegmoregthangonegcorrectganswer.gYougaregrequiredgt
ogmarkgandgcommentgongcorrectganswers..gMarkgallgofgthegcorrectganswersgforgfullgcredit.
gThegtruegfalsegquestionsgrequiregangexplanationgingadditiongtogthegtrue/falsegresponse,gan
d,gifgfalse,galsogrequiregagcorrection.
TruegFalse:
1. Theg if,gwhilegandgforgstatementsgcontrolgonlygonegstatement.g
Answer:gTrue
Explanation:gThegonegstatementgmaygbegagblockg(statementsgthatgaregenclosedgwithgcu
rlygbracesg{g})gorgagsimplegstatement.
2. Givengthegdeclaration
intgxg=g0;
Thegfollowinggexpressiongcausesgagdividegbygzerogerror:
(xg!=0)g||g(2/xg<g1);
Answer:gFalse.
Explanation:gTheg||goperatorgusesgshort-
circuitgevaluation.gThegfirstgmembergofgthisgexpressiongisgtrue;gthegtruthgvaluegofgt
hegcompletegexpressiongcangbegdeterminedgfromgthis;gconsequently,gthegsecondgexp
ressiongisgnotgevaluated.gTheregisgnogdivide-gby-zerogerror.
3. Supposegweghavegthesegdeclarations,
intgxg=g-1,gyg=g0,gzg=g1;
ThisgBooleangexpressiongisgcorrectgandgitgdoesgwhatgthegprogrammergintends.
xg<gyg<gz
Answer:gFalse
mynursytest.store
,DOWNLOADgTHEgTestgBankgforgAbsolutegC++g6thgEditiongSavitch
TestgBankgforgSavitchgAbsolutegC++g 6e Pageg2
Explanation:gUnfortunately,gthegexpressiongcompilesgwithoutgerrorgandgruns.gTheg<
operatorgassociatesg(groups)gleftgtogright,gsogthegexpressiongevaluatesgas
(xg<gy)g<gz
Thegleftghandgexpressiongevaluatesgtogtrue,gwhich,gifgcomparedgtogagnumericgtype,gco
nvertsgtog1.gWhengcomparedgtog1,gthisgisgfalse.gWhatgthegprogrammergintends,gexpr
essedgasgmathematacsgmightgisg-1g<g0<g1,gagresultgthatgisgclearlygtrue.
4. Yougwantgtogdeterminegwhethergtimeghasgrungout.gThegfollowinggcodegcorrectl
ygimplementsgthis.
!timeg>glimit
Answer:gFalse.
Explanation:gThegexpressiongalwaysgevaluatesgtogfalse.gThisgcannotgbegwhatgth
egprogrammergintended.gThegcompilergdoesn’tgcatchgthegproblemgbecausegthegcod
egisglegal,gcorrectgC++.gCorrectedgcodegisg!(timeg>glimit)
Codegexecutiongproceedsgasgfollows:gThegoperatorg!gtakesgagboolgargument.gItgreturn
sgthegoppositegboolgvalue.gThegvaluegofgtimegisgconvertedgtogagbool.gThegvaluegofg
timegisgcertainlygnonzero,ghenceg!timegisg!true,gi.e.,gfalse.gTheg>gcomparesgt
hisgresultgwithgagnumericgvalue,glimit,g(angintgorgperhapsgsomegkindgofgfloatinggpo
int).gThegvaluegongthegleftg(false)gisgconvertedgtogag0gvaluegofgthatgtype.gThegvalueg
ofglimitgisgunlikelygtogbegagnegativegnumbergandgwegaregconcernedgaboutgtimegrunni
nggout,gsogitgisgunlikelygthatgtimegisgzero.gConsequently,gtheginequalitygbecomesg0>l
imit,gwhereglimitgisgnonzero.gThisgisgfalse.
5. Thegvaluegofgcountgisg0;glimitgisg10.gEvaluate:
(countg==g0)&&(limitg<g20)
Answer:gtrue
6. Thegvaluegofgcountgisg0;glimitgisg10.gEvaluate:
countg==g0g&&glimitg<g20
Answer:gtrue
©2016gPearsongEducation,gInc.gUppergSaddlegRiver,gNJ.gAllgRightsgReserve
d.
mynursytest.store
, DOWNLOADgTHEgTestgBankgforgAbsolutegC++g6thgEditiongSavitch
TestgBankgforgSavitchgAbsolutegC++g 6e Pageg3
Explanation:gThegoperatorsg ==gandg <ghaveghighergprecedencesgthang &&,ghencegtheg
expressions,gcountg==g0gandg limitg<g10garegevaluatedg(togtrue)gthengtheg&&gi
sgexecuted.
7. Thegvaluegofgcountgisg0;glimitgisg10.gEvaluate:
(countg!=g0)||(limitg<g20)
Answer:gtrue.
Explanation:gThegfirstgexpressiongevaluatesgtogfalse,gthegvaluegofgtheg||gexpression
gisgdeterminedgbygthegsecondgexpression.gThegsecondgexpressiongisgtruegsogtheg ||g
expressiongevaluatesgtogtrue.
8. Ingagwhilegloop,gthegBoolean_Expressiongisgexecutedgbeforegeachgexecutiongofgth
egloopgbody.
Answer:gtrue
9. Ingagdo-
whilegloop,gagcontinuegstatementgterminatesgthegloop.gAnswer:
gFalse
Explanation:gThegcontinuegstatementgcausesgthegBoolean_Expressiongtogbegexecute
d.gIfgtrue,gthegbodygexecutes,gotherwisegthegloopgterminates.
10. Agbreakgstatementgisgusedgingloopsgonly.
gAnswer:gFalse.
Explanation:gIngadditiongtogitsgusegingloops,gagbreakgstatementgisgusedgingthe
switchgstatementgtogtransfergcontrolgtogthegnextgstatementgaftergthegswitchgblock.
11. Whengagloopgisgnestedgingsideganothergloop,gagbreakgorgcontinuegstatement
gterminatesgorgrestartsgthegoutermostgloopgofgthegnestedgloopgstructure.
Answer:gFalse
Explanation:gAgbreakgorgcontinuegterminatesgorgrestartsgonlygtheginnermostgloopgcontaini
nggthegbreakgorgcontinue.
©2016gPearsongEducation,gInc.gUppergSaddlegRiver,gNJ.gAllgRightsgReserve
d.
mynursytest.store
Chapterg2g-gTestgQuestions
Thesegtestgquestionsgaregtrue-
false,gfillgingthegblank,gmultiplegchoice,gandgfreegformgquestionsgthatgmaygrequiregcode.gT
hegmultiplegchoicegquestionsgmayghavegmoregthangonegcorrectganswer.gYougaregrequiredgt
ogmarkgandgcommentgongcorrectganswers..gMarkgallgofgthegcorrectganswersgforgfullgcredit.
gThegtruegfalsegquestionsgrequiregangexplanationgingadditiongtogthegtrue/falsegresponse,gan
d,gifgfalse,galsogrequiregagcorrection.
TruegFalse:
1. Theg if,gwhilegandgforgstatementsgcontrolgonlygonegstatement.g
Answer:gTrue
Explanation:gThegonegstatementgmaygbegagblockg(statementsgthatgaregenclosedgwithgcu
rlygbracesg{g})gorgagsimplegstatement.
2. Givengthegdeclaration
intgxg=g0;
Thegfollowinggexpressiongcausesgagdividegbygzerogerror:
(xg!=0)g||g(2/xg<g1);
Answer:gFalse.
Explanation:gTheg||goperatorgusesgshort-
circuitgevaluation.gThegfirstgmembergofgthisgexpressiongisgtrue;gthegtruthgvaluegofgt
hegcompletegexpressiongcangbegdeterminedgfromgthis;gconsequently,gthegsecondgexp
ressiongisgnotgevaluated.gTheregisgnogdivide-gby-zerogerror.
3. Supposegweghavegthesegdeclarations,
intgxg=g-1,gyg=g0,gzg=g1;
ThisgBooleangexpressiongisgcorrectgandgitgdoesgwhatgthegprogrammergintends.
xg<gyg<gz
Answer:gFalse
mynursytest.store
,DOWNLOADgTHEgTestgBankgforgAbsolutegC++g6thgEditiongSavitch
TestgBankgforgSavitchgAbsolutegC++g 6e Pageg2
Explanation:gUnfortunately,gthegexpressiongcompilesgwithoutgerrorgandgruns.gTheg<
operatorgassociatesg(groups)gleftgtogright,gsogthegexpressiongevaluatesgas
(xg<gy)g<gz
Thegleftghandgexpressiongevaluatesgtogtrue,gwhich,gifgcomparedgtogagnumericgtype,gco
nvertsgtog1.gWhengcomparedgtog1,gthisgisgfalse.gWhatgthegprogrammergintends,gexpr
essedgasgmathematacsgmightgisg-1g<g0<g1,gagresultgthatgisgclearlygtrue.
4. Yougwantgtogdeterminegwhethergtimeghasgrungout.gThegfollowinggcodegcorrectl
ygimplementsgthis.
!timeg>glimit
Answer:gFalse.
Explanation:gThegexpressiongalwaysgevaluatesgtogfalse.gThisgcannotgbegwhatgth
egprogrammergintended.gThegcompilergdoesn’tgcatchgthegproblemgbecausegthegcod
egisglegal,gcorrectgC++.gCorrectedgcodegisg!(timeg>glimit)
Codegexecutiongproceedsgasgfollows:gThegoperatorg!gtakesgagboolgargument.gItgreturn
sgthegoppositegboolgvalue.gThegvaluegofgtimegisgconvertedgtogagbool.gThegvaluegofg
timegisgcertainlygnonzero,ghenceg!timegisg!true,gi.e.,gfalse.gTheg>gcomparesgt
hisgresultgwithgagnumericgvalue,glimit,g(angintgorgperhapsgsomegkindgofgfloatinggpo
int).gThegvaluegongthegleftg(false)gisgconvertedgtogag0gvaluegofgthatgtype.gThegvalueg
ofglimitgisgunlikelygtogbegagnegativegnumbergandgwegaregconcernedgaboutgtimegrunni
nggout,gsogitgisgunlikelygthatgtimegisgzero.gConsequently,gtheginequalitygbecomesg0>l
imit,gwhereglimitgisgnonzero.gThisgisgfalse.
5. Thegvaluegofgcountgisg0;glimitgisg10.gEvaluate:
(countg==g0)&&(limitg<g20)
Answer:gtrue
6. Thegvaluegofgcountgisg0;glimitgisg10.gEvaluate:
countg==g0g&&glimitg<g20
Answer:gtrue
©2016gPearsongEducation,gInc.gUppergSaddlegRiver,gNJ.gAllgRightsgReserve
d.
mynursytest.store
, DOWNLOADgTHEgTestgBankgforgAbsolutegC++g6thgEditiongSavitch
TestgBankgforgSavitchgAbsolutegC++g 6e Pageg3
Explanation:gThegoperatorsg ==gandg <ghaveghighergprecedencesgthang &&,ghencegtheg
expressions,gcountg==g0gandg limitg<g10garegevaluatedg(togtrue)gthengtheg&&gi
sgexecuted.
7. Thegvaluegofgcountgisg0;glimitgisg10.gEvaluate:
(countg!=g0)||(limitg<g20)
Answer:gtrue.
Explanation:gThegfirstgexpressiongevaluatesgtogfalse,gthegvaluegofgtheg||gexpression
gisgdeterminedgbygthegsecondgexpression.gThegsecondgexpressiongisgtruegsogtheg ||g
expressiongevaluatesgtogtrue.
8. Ingagwhilegloop,gthegBoolean_Expressiongisgexecutedgbeforegeachgexecutiongofgth
egloopgbody.
Answer:gtrue
9. Ingagdo-
whilegloop,gagcontinuegstatementgterminatesgthegloop.gAnswer:
gFalse
Explanation:gThegcontinuegstatementgcausesgthegBoolean_Expressiongtogbegexecute
d.gIfgtrue,gthegbodygexecutes,gotherwisegthegloopgterminates.
10. Agbreakgstatementgisgusedgingloopsgonly.
gAnswer:gFalse.
Explanation:gIngadditiongtogitsgusegingloops,gagbreakgstatementgisgusedgingthe
switchgstatementgtogtransfergcontrolgtogthegnextgstatementgaftergthegswitchgblock.
11. Whengagloopgisgnestedgingsideganothergloop,gagbreakgorgcontinuegstatement
gterminatesgorgrestartsgthegoutermostgloopgofgthegnestedgloopgstructure.
Answer:gFalse
Explanation:gAgbreakgorgcontinuegterminatesgorgrestartsgonlygtheginnermostgloopgcontaini
nggthegbreakgorgcontinue.
©2016gPearsongEducation,gInc.gUppergSaddlegRiver,gNJ.gAllgRightsgReserve
d.
mynursytest.store