用C++解决问题第十版Chapter 7.docx
《用C++解决问题第十版Chapter 7.docx》由会员分享,可在线阅读,更多相关《用C++解决问题第十版Chapter 7.docx(16页珍藏版)》请在冰豆网上搜索。
用C++解决问题第十版Chapter7
TRUE/FALSE
1.Theindexedvariables(members)ofanarraymustbeintegers.
ANSWER:
FALSE
2.Thelocationsofthevariousindexedvariablesinanarraycanbespreadoutalloverthememory.
ANSWER:
FALSE
3.Thefollowingarraydeclarationislegal
doublescores[]={0.1,0.2,0.3};
ANSWER:
true
4.Arrayscanbepassedtofunctions.
ANSWER:
TRUE
5.Arrayscanbereturnedfromafunction.
ANSWER:
FALSE
6.Ifafunctionisexpectingapassbyreferenceparameter,youcanpassanindexvariablefromanarrayofthesamebasetypetothatfunction.
ANSWER:
TRUE
7.Whenyouhaveafunctionthatexpectsanarray,itshouldalsoexpectthesizeofthearrayorthenumberofindexedvariableswithvaliddata.
ANSWER:
TRUE
8.Thefollowingfunctiondeclarationguaranteesthevaluesinthearrayargumentarenotchanged.
voidfunction1(intarray[],intnumElements);
ANSWER:
FALSE
9.Thefollowingfunctionwillworkwithanysizeintegerarray.
voidfunction1(intarray[],intnumElements);
ANSWER:
TRUE
10.Ifyouusetheconstmodifierinafunctiondeclaration,youdonotincludeitinthefunctiondefinition.
ANSWER:
FALSE
ShortAnswer
1.Writethecodetodeclareatwodimensionarrayofintegerswith10rowsand20columns.
ANSWER:
intarray[10][20];
2.Writethecodetodeclareanarrayof10doublesnamedlist;
ANSWER:
doublelist[10];
3.Themodifierthatguaranteesthatanarrayargumentwillnotbechangediscalled______.
ANSWER:
const
4.Howmanyindexedvariablesdoesthefollowingarrayhave?
intmyArray[]={1,2,3,6,5,4,7,1,2};
ANSWER:
9
5.Howmanyindexedvariablesdoesthefollowingarrayhave?
intmyArray[12]={1,2,3,6,5,4,7,1,2};
ANSWER:
12
6.Writethedeclarationforafunctionnamedfunct1thatexpectsanarrayoffloats,thenumberofelementsinthearrayanddoesnotreturnanyvalue.
ANSWER:
voidfunct1(floatmyArray[],intnumElements);
7.Ifyouputavalueinthesquarebracketsofaone-dimensionarrayparameter,thisvalueis_________bythecompiler.
ANSWER:
ignored
8.Ifyourindexusedtoaccesstheindexedvariablesofthearrayhasthevalueofanon-existentindex,thisiscalled_________
ANSWER:
Indexoutofrange,Indexoutofbounds,orillegal.
9.Thecomputerrememberstheaddressofwhichindexedvariable(s)inanarray?
______
ANSWER:
thefirst
10.Acomputer'smemoryconsistsofnumberedlocationscalled__________.
ANSWER:
bytes
11.Intheexpression
doublescore[10];
doubleiscalledthe___________ofthearray
ANSWER:
basetype
12.Intheexpression
cout<iiscalledthe
ANSWER:
indexorsubscript
13.An_______isusedtoprocessacollectionofdataallofwhichisthesametype
ANSWER:
array
14.Theindividualvariablesthatcompriseanarrayarecalled__________
ANSWER:
indexedvariables,subscriptedvariables,orelements.
15.Indexesarenumberedstartingat_________
ANSWER:
0
MultipleChoice
1.Whatarethevalidindexesforthearrayshownbelow?
intmyArray[25];
a.0-25
b.0-24
c.1-25
d.1-24
ANSWER:
B
2.Whatiswrongwiththefollowingcode?
floatscores[10],total;
a.Cannotdeclareregularandarrayvariablestogether.
b.Arraysmustbeintegers
c.The10shouldbereplacedwithavariablename,whosevalueisinputfromtheuser
d.Nothing.
ANSWER:
D
3.Givenanarraynamedscoreswith25elements,whatisthecorrectwaytoaccessthe25thelement?
a.scores+25
b.scores[24]
c.scores[25]
d.scores[last]
ANSWER:
B
4.Whyshouldyouuseanamedconstantforthesizeofanarray?
a.Readabilityofcode
b.Makeschangestotheprogrameasier
c.Helpsreducelogicerrors
d.Alloftheabove
ANSWER:
D
5.Givenanarrayofintegersofsize5,howdoesthecomputerknowwherethe3rdindexedvariableislocated?
a.Itadds3tothebaseaddressofthearray
b.Itaddsspacefor3integerstothebaseaddressofthearray
c.Itrememberswherealltheindexedvariablesofthearrayarelocated.
d.Noneoftheabove
ANSWER:
B
6.Whatiswrongwiththefollowingcodefragment?
constintSIZE=5;
floatscores[SIZE];
for(inti=0;i<=SIZE;i++)
{
cout<<"Enterascore\n";
cin>>scores[i];
}
a.Arrayindexesstartat1not0
b.Arraysmustbeintegers
c.Arrayindexesmustbelessthanthesizeofthearray
d.Shouldbecin>>scores[0];
ANSWER:
C
7.Whichofthefollowingdeclareanarrayof5characters,andinitializesthemtosomeknownvalues?
a.chararray[5]={'a','b','c','d','e'};
b.chararray[4]={'a','b','c','d','e'};
c.chararray[5]={''};
d.chararray[]={'a','b','d','e'};
e.AandC
f.BandD
g.alloftheabove
ANSWER:
E
8.Ifyoudeclareandinitializeanintegerarrayofsize10,butonlylist5values,whatvaluesarestoredintheremaining5indexedvariables?
a.0
b.garbage
c.0.0
d.'0'
ANSWER:
A
9.Arraysarealwayspassedtoafunctionusing
a.passbyvalue
b.passbyreference
c.passbyarray
d.youcannotpassarraystoafunction
ANSWER:
C
10.Givethefollowingdeclarations,whichofthefollowingisalegalcalltothisfunction?
intmyFunction(intmyValue);
intmyArray[1000];
a.cout<b.cout<c.myArray=myFunction(myArray);
d.myArray[1]=myFunction(myArray[0]);
e.AandB
f.AandC
g.BandD
ANSWER:
G
11.Whichofthefollowingfunctiondeclarationscorrectlyexpectanarrayasthefirstargument?
a.voidf1(intarray,intsize);
b.voidf1(int&array,intsize);
c.voidf1(intarray[100],intsize);
d.voidf1(floatarray[],intsize);
e.Alloftheabove
f.CandD
g.AandB
ANSWER:
F
12.Whichofthefollowingfunctiondeclarationscorrectlyguaranteethatthefunctionwillnotchangeanyvaluesinthearrayargument?
a.voidf1(intarray[],intsize)const;
b.voidf1(intarray[],intsize);
c.voidf1(int&array,intsize);
d.voidf1(constintarray[],intsize);
e.voidf1(intarray[],constintsize);
ANSWER:
D
13.Thefollowingfunctiondefinitionhasanerrorinit.Whatlineisthiserroron?
0.voidf1(constdoublearray[],intsize)
1.{
2.inti=0;
3.while(i4.{
5.array[i]+=2;
6.cout<7.i++;
8.}
9.}
a.0
b.2
c.5
d.6
e.2
ANSWER:
C
14.Whichofthefollowingfunctiondeclarationscouldbeusedtoinputdatafromthekeyboardintothearray?
a.voidinput(intarray[],int&numElements,intMAX_SIZE);
b.voidinput(intarray[],intnumElements,intMAX_SIZE);
c.voidinput(int&array[],intnumElements,intMAX_SIZE);
d.intarray[]input(intarray[],int&numElements,intMAX_SIZE);
ANSWER:
A
15.Ifwewantasearchfunctiontosearchanarrayforsomevalueandreturneithertheindexwherethevaluewasfound,or-1ifnotfound,whichofthefollowingprototypeswouldbeappropriate?
a.voidsearch(constintarray,inttarget,intnumElements);
b.voidsearch(constintarray,inttarget);
c.intsearch(constintarray[],intnumElements);
d.intsearch(constintarray[],inttarget,intnumElements);
ANSWER:
D
16.Giventhefollowingfunctiondefinitionforasearchfunction,andthefollowingvariabledeclarations,whichofthefollowingareappropriatefunctioninvocations?
constintSIZE=1000;
intsearch(constintarray[],inttarget,intnumElements);
intarray[SIZE],target,numberOfElements;
a.search(array[0],target,numberOfElements);
b.result=search(array[0],target,numberOfElements);
c.result=search(array,target,numberOfElements);
d.result=search(array,target,SIZE);
ANSWER:
C
17.Giventhefollowingfunctiondefinition,willrepeatedcallstothesearchfunctionforthesametargetfindalloccurrencesofthattargetinthearray?
intsearch(constintarray[],inttarget,intnumElements)
{
intindex=0;
boolfound=false;
while((!
found)&&(index{
if(array[index]==target)
found=true;
else
index++;
}
if(found==true)
returnindex;
else
return-1;
}
a.Yes
b.No
c.Impossibletotellwithoutlookingatthevaluesofthearray
d.Itdependsonthevalueoftarget.
ANSWER:
B
18.Giventhefollowingfunctiondefinition,whatmodificationsneedtobemadetothesearchfunctionsothatitfindsalloccurrencesoftargetinthearray?
intsearch(constintarray[],inttarget,intnumElements)
{
intindex=0;
boolfound=false;
while((!
found)&&(index{
if(array[index]==target)
found=true;
else
index++;
}
if(found==true)
returnindex;
else
return-1;
}
a.Addanotherparametertoindicatewheretostopsearching
b.Addanotherparametertoindicatewheretostartsearching
c.Thisalreadycanfindalloccurrencesofagiventarget
d.Havethefunctionreturnthewholearray
ANSWER:
B
19.Whichsortalgorithmdoesthefollowingoutlinedefine?
foribetween0andnumber_used-1inclusive
puttheithsmallestelementatarray[i]
a.sequential
b.selection
c.bubble
d.swap
ANSWER:
B
20.Whichofthefollowingarraydeclarationsarelegal?
a.intarray[10];
b.intsize;
cin>>size;
intarray[size];
c.intarray[]={0,0,0};
d.constintsize=9;
intarray[size];
e.Alloftheabove
f.AllbutC
g.AllbutB
ANSWER:
G
21.Whichofthefollowingfunctiondeclarationswillacceptthefollowingtwo-dimensionarray?
intpages[10][30];
a.voidf1(intpages[][],intsize);
b.voidf1(intpages[][30],intsize);
c.voidf1(intpages[10][],intsize);
d.voidf1(int&pages,intsize);
ANSWER:
B
22.Ifyouneedafunctionthatwillhandlemulti-dimensionalarrays,youmustspecifythefollowingsizesinsidethesquarebrackets.
a.Allthesizes
b.Allsizesexceptthelastdimension
c.Allsizesexceptthefirstdimension
d.Noneofthesizes
ANSWER:
C
23.Whatistheoutputofthefollowingcodefragment?
intarray[4][4],index1,index2;
for(index1=0;index1<4;index1++)
for(index2=0;index2<4;index2++)
array[index1][index2]=index1+index2;
for(index1=0;index1<4;index1++)
{
for(inde