C++基础练习试题及答案英文版十二.docx
- 文档编号:2314656
- 上传时间:2023-05-03
- 格式:DOCX
- 页数:8
- 大小:15.71KB
C++基础练习试题及答案英文版十二.docx
《C++基础练习试题及答案英文版十二.docx》由会员分享,可在线阅读,更多相关《C++基础练习试题及答案英文版十二.docx(8页珍藏版)》请在冰点文库上搜索。
C++基础练习试题及答案英文版十二
Section12.2TemplatesBasics
12.1 Whichofthefollowingstatementsiscorrect?
A.Templatesprovidethecapabilitytoparameterizetypesinfunctionsandclasses.
B.Withtemplates,youcandefineonefunctionoroneclasswithagenerictypethatcanbesubstitutedforaconcretetypebythecompiler.
C.Templatesfacilitatesdevelopingreusablesoftware.
D.Templatesimprovesperformance.
12.2 Thetemplateprefixmaybedefinedas_________.
A.template
B.template
12.3 Atemplateprefixfortwoparametersmaybedefinedas_____________.
A.template
B.template
C.template
D.template
12.4 Supposeatemplatefunctionisdefinedasfollows:
template
T maxValue(const T& value1, const T& value2)
{
if(value1>value2)
returnvalue1;
else
returnvalue2;
}
Which of the following statements are correct?
A.cout< B.cout< C.cout< D.cout< E.cout< 12.5 Supposeatemplatefunctionisdefinedasfollows: template T1 maxValue(const T1& value1, const T2& value2) { if(value1>value2) returnvalue1; else returnvalue2; } Which of the following statements are correct? A.cout< B.cout< C.cout< D.cout< E.cout< 12.6 Ifyoudefinetheswapfunctionasfollows: template void swap(T& var1, T& var2) { Ttemp=var1; var1=var2; var2=temp; } You can invoke swap using ______. A.swap(1,2) B.intv1=1;intv2=2;swap(v1,v2); C.intv1=1;intv2=2;swap(&v1,&v2); D.intv1=1;doublev2=2;swap(v1,v2); Section12.3Example: AGenericSort 12.7 Supposeatemplatefunctionisdefinedasfollows: template void printArray(T list[], int arraySize) { for(inti=0;i { cout< } cout< } Which of the following statements are correct? A.intlist[]={1,2,3,4};printArray(list,4); B.intlist[]={1,2.5,3,4};printArray(list,4); C.doublelist[]={1,2,3,4};printArray(list,4); D.stringlist[]={"Atlanta","Dallas","Houston","Chicago"};printArray(list,4); Section12.4ClassTemplates 12.8 Supposeyoudefine template class Stack { Stack(); ... }; Which of the following statements are correct? A.Stack B.Stack C.Stack<>s; D.Stacks; E.Stack 12.9 Supposeyoudefine template class Stack { Stack(); ... private: Telements[capacity]; intsize; }; Which of the following statements are correct? A.Stack B.Stack C.Stack<50>s; D.Stack 12.10 Whichofthefollowingstatementsaretrue? A.Aclasstemplatecanbederivedfromaclasstemplate. B.Aclasstemplatecanbederivedfromanontemplateclass. C.Anontemplateclasscanbederivedfromaclasstemplatespecialization.d.Stack D.Friendsareusedexactlythesamefortemplateandnontemplateclasses. E.Youcandefinestaticmembersinatemplateclass.Eachtemplatespecializationhasitsowncopyofastaticdatafield. 12.11 IntheimplementationofImprovedStack.h,whichofthefollowingaretrue? A.sizeneverreduces. B.capacityneverreduces. C.InsideStack,aregulararrayisusedtostoreelements. D.Ifthecurrentcapacityequalstosize,capacityisdoubledwhenanewelementisaddedtoStack. Section12.6TheC++vectorClass 12.12 Whichofthefollowingstatementsaretrue? A.Thearraysizeisfixedintheclassdeclaration. B.C++providesthevectorclassandyoucancreatevectorobjects. C.Avectorobjectisjustlikeanarray,butavector? ssizecangrowautomaticallyifneeded. D.Avectorhasano-argconstructor. 12.13 Todeclareavectorforholdingintvalues,use__________. A.vector B.vectorv; C.vectorv D.vector 12.14 Toaddanintvalue5toavectorvofintegers,use_________. A.v.add(5); B.v.insert(5); C.v.push_back(5); D.v.append(5); 12.15 Toobtainthesizeofthevectorv,use_______. A.v.getSize(); B.v.length(); C.v.getLength(); D.v.size(); 12.16 Todeletealltheelementsinavectorv,use_______. A.v.deleteAll(); B.v.clear(); C.v.eraseAll(); D.v.delele(); 12.17 Toobtainthefirstelementinavectorv,use_______. A.v.at(0); B.v[0]; C.v.at (1); D.v[1]; 12.18 Whatiswronginthefollowingcode? #include #include usingnamespacestd; intmain() { vector cout< return0; } A.Theprogramhasacompileerroronv[0]. B.Theprogramhasaruntimeerroronv[0],becausethevectorisempty. C.Theprogramhasacompileerroronvector D.Theprogramhasaruntimeerroronvector 12.19 Whatiswronginthefollowingcode? vector v.push_back("Beijing"); v.push_back("Tokyo"); v.push_back("Shanghai"); v[3]="HongKong"; A.Thelastlineinthecodecausesaruntimeerrorbecausethereisnoelementatindex3inthevector. B.Thelastlineinthecodehasacompileerrorbecausethereisnoelementatindex3inthevector. C.Ifyoureplacethelastlinebyv[2]="HongKong",thecodewillcompileandrunfine. D.Ifyoureplacethelastlinebycout< E.Ifyoureplacethelastlinebycout<
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 基础 练习 试题 答案 英文 十二