Tải bản đầy đủ - 0 (trang)
Appendix J. ANSWERS TO REVIEW QUESTIONS

Appendix J. ANSWERS TO REVIEW QUESTIONS

Tải bản đầy đủ - 0trang

Chapter2

1: WhatarethemodulesofC++programscalled?



A: Theyarecalledfunctions.



Whatdoesthefollowingpreprocessordirectivedo?

2:

#include



A:



Itcausesthecontentsoftheiostreamfiletobesubstitutedforthisdirectivebefore

finalcompilation.



Whatdoesthefollowingstatementdo?

3:

usingnamespacestd;



A: Itmakesdefinitionsmadeinthestdnamespaceavailabletoaprogram.



4:



Whatstatementwouldyouusetoprintthephrase"Hello,world"andthenstartanew

line?



cout<<"Hello,world\n";

A: or

cout<<"Hello,world"<


5: Whatstatementwouldyouusetocreateanintegervariablewiththenamecheeses?



A: intcheeses;



6:



Whatstatementwouldyouusetoassignthevalue32tothevariablecheeses?



A: cheeses=32;



7:



Whatstatementwouldyouusetoreadavaluefromkeyboardinputintothevariable

cheeses?



A: cin>>cheeses;



8:



Whatstatementwouldyouusetoprint"WehaveXvarietiesofcheese,"wherethe

currentvalueofthecheesesvariablereplacesX?



A: cout<<"Wehave"<


9: Whatdoesthefollowingfunctionheadertellyouaboutthefunction?



A:



Ittellsusthatthefunctionfroop()expectstobecalledwithoneargument,whichwill

betypedouble,andthatthefunctionwillreturnatypeintvalue.



10: Whendoyounothavetousethekeywordreturnwhenyoudefineafunction?



Youdon'thavetousereturninafunctionwhenthefunctionhasreturntypevoid.

However,youcanuseitprovidingyoudon'tgiveareturnvalue:

A:

return;



Chapter3

1: WhydoesC++havemorethanoneintegertype?



Havingmorethanoneintegertypeletsyouchoosethetypebestsuitedtoaparticular

A: need.Forexample,youcoulduseshorttoconservespace,longtoguaranteestorage

capacity,orfindthataparticulartypespeedsupaparticularcalculation.



Definethefollowing:

a. Ashortintegerwiththevalue80

2:

Anunsignedintintegerwiththevalue42110

Anintegerwiththevalue3000000000



shortrbis=80;//orshortintrbis=80;

unsignedintq=42110;//orunsignedq=42110;

unsignedlongants=3000000000;



A: Note



Don'tcountonintbeinglargeenoughtohold3000000000.



3:



WhatsafeguardsdoesC++providetokeepyoufromexceedingthelimitsofaninteger

type?



A:



C++providesnoautomaticsafeguardstokeepyoufromexceedingintegerlimits;you

canusetheclimitsheaderfiletodeterminewhatthelimitsare.



4: Whatisthedistinctionbetween33Land33?



A: Theconstant33Listypelong,whereastheconstant33istypeint.



ConsiderthetwoC++statementsthatfollow.Aretheyequivalent?

5: chargrade=65;

chargrade='A';



Thetwostatementsarenotreallyequivalent,althoughtheyhavethesameeffecton

somesystems.Mostimportant,thefirststatementassignstheletterAtogradeonlyon

A:

asystemusingtheASCIIcode,whilethesecondstatementalsoworksforothercodes.

Second,65isatypeintconstant,while'A'isatypecharconstant.



6:



HowcouldyouuseC++tofindoutwhichcharacterthecode88represents?Comeup

withatleasttwoways.



Herearefourways:

charc=88;

cout<
A:

cout.put(char(88));//put()printscharascharacter

cout<
cout<<(char)88<<"\n";//old-styletypecastvaluetochar



7:



Assigningalongvaluetoafloatcanresultinaround-offerror.Whataboutassigning

longtodouble?



Theanswerdependsonhowlargethetwotypesare.Iflongis4bytes,thereisnoloss.

A: That'sbecausethelargestlongvaluewouldbeabout2billion,whichis10digits.

Becausedoubleprovidesatleast13significantfigures,noroundingwouldbeneeded.



EvaluatethefollowingexpressionsasC++would:

a. 8*9+2

6*3/4

8:

3/4*6



6.0*3/4

15%4



a. 8*9+2is72+2is74

6*3/4is18/4is4

A:



3/4*6is0*6is0

6.0*3/4is18.0/4is4.5

15%4is3



9:



Supposex1andx2aretwotypedoublevariablesthatyouwanttoaddasintegersand

assigntoanintegervariable.ConstructaC++statementfordoingso.



Eitherofthefollowingwork:

A: intpos=(int)x1+(int)x2;

intpos=int(x1)+int(x2);



Chapter4

Howwouldyoudeclareeachofthefollowing?

a. actorsisanarrayof30char.

1:



betsieisanarrayof100short.

chuckisanarrayof13float.

dipseaisanarrayof64longdouble.



a. charactors[30];

shortbetsie[100];

A:

floatchuck[13];

longdoubledipsea[64];



2: Declareanarrayoffiveintsandinitializeittothefirstfiveoddpositiveintegers.



A: intoddly[5]={1,3,5,7,9};



3:



Writeastatementthatassignsthesumofthefirstandlastelementsofthearrayin

question2tothevariableeven.



A: inteven=oddly[0]+oddly[4];



4:



Writeastatementthatdisplaysthevalueofthesecondelementinthefloatarray

ideas.



A: cout<


5: Declareanarrayofcharandinitializeittothestring"cheeseburger".



charlunch[13]="cheeseburger";//numberofcharacters+1

A: or

charlunch[]="cheeseburger";//letthecompilercountelements



6:



Deviseastructuredeclarationthatdescribesafish.Thestructureshouldincludethe

kind,theweightinwholeounces,andthelengthinfractionalinches.



structfish{

charkind[20];

A: intweight;

floatlength;

};



7: Declareavariableofthetypedefinedinquestion6andinitializeit.



fishpetes=

{

"trout",

A:

13,

12.25

};



8:



UseenumtodefineatypecalledResponsewiththepossiblevaluesofYes,No,and

Maybe.Yesshouldbe1,Noshouldbe0,andMaybeshouldbe2.



A: enumResponse{No,Yes,Maybe};



9:



Supposetedisadoublevariable.Declareapointerthatpointstotedandusethe

pointertodisplayted'svalue.



double*pd=&ted;

A:

cout<<*pd<<"\n";



Supposetreacleisanarrayof10floats.Declareapointerthatpointstothefirst

10: elementoftreacleandusethepointertodisplaythefirstandlastelementsofthe

array.



float*pf=treacle;//or=&treacle[0]

A: cout<
//oruse*pfand*(pf+9)



11:



Writeacodefragmentthataskstheusertoenterapositiveintegerandthencreatesa

dynamicarrayofthatmanyints.



unsignedintsize;

cout<<"Enterapositiveinteger:";

A:

cin>>size;

int*dyn=newint[size];



12: Isthefollowingvalidcode?Ifso,whatdoesitprint?



Yes,itisvalid.Theexpression"Homeofthejollybytes"isastringconstant,

henceitevaluatesastheaddressofthebeginningofthestring.Thecoutobject

A: interpretstheaddressofacharasaninvitationtoprintastring,butthetypecast

(int*)convertstheaddresstotypepointer-to-int,whichisthenprintedasan

address.Inshort,thestatementprintstheaddressofthestring.



13:



Writeacodefragmentthatdynamicallyallocatesastructureofthetypedescribedin

question6andthenreadsavalueforthekindmemberofthestructure.



structfish

{

charkind[20];

intweight;

A:



floatlength;

};

fish*pole=newfish;

cout<<"Enterkindoffish:";

cin>>pole->kind;



Listing4.6illustratesaproblemwiththefollowingnumericinputwithline-oriented

stringinput.Howwouldreplacing

cin.getline(address,80);

14:



with

cin>>address;

affecttheworkingofthisprogram?



Usingcin>>addresscausesaprogramtoskipoverwhitespaceuntilitfinds

nonwhitespace.Itthenreadscharactersuntilitencounterswhitespaceagain.Thus,it

A:

willskipoverthenewlinefollowingthenumericinput,avoidingthatproblem.Onthe

otherhand,itwillreadjustasingleword,notanentireline.



Chapter5

1:



What'sthedifferencebetweenanentry-conditionloopandanexit-conditionloop?

WhichkindiseachoftheC++loops?



Anentry-conditionloopevaluatesatestexpressionbeforeenteringthebodyofthe

loop.Iftheconditioninitiallyisfalse,theloopneverexecutesitsbody.Anexitconditionloopevaluatesatestexpressionafterprocessingthebodyoftheloop.Thus,

A:

theloopbodyisexecutedonceevenifthetestexpressioninitiallyisfalse.Thefor

andwhileloopsareentry-conditionloops,andthedowhileloopisanexit-condition

loop.



Whatwouldthefollowingcodefragmentprintifitwerepartofavalidprogram?

inti;

2: for(i=0;i<5;i++)

cout<
cout<<"\n";



Itwouldprintthefollowing:

A: 01234

Notethatthecout<<"\n";isnotpartoftheloopbody(nobraces).



Whatwouldthefollowingcodefragmentprintifitwerepartofavalidprogram?

intj;

3: for(j=0;j<11;j+=3)

cout<
cout<<"\n"<


Itwouldprintthefollowing:



A: 0369

12



Whatwouldthefollowingcodefragmentprintifitwerepartofavalidprogram?

intj=5;

4:

while(++j<9)

cout<


Itwouldprintthefollowing:

A: 6

8



Whatwouldthefollowingcodefragmentprintifitwerepartofavalidprogram?

intk=8;

5: do

cout<<"k="<
while(k++<5);



Itwouldprintthefollowing:

A:

k=8



6:



Writeaforloopthatprintsthevalues1248163264byincreasingthevalueofa

countingvariablebyafactorof2eachcycle.



It'ssimplesttousethe*=operator:

A: for(intnum=1;num<=64;num*=2)



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Appendix J. ANSWERS TO REVIEW QUESTIONS

Tải bản đầy đủ ngay(0 tr)

×