C常见英文面试笔试题.docx
- 文档编号:23093147
- 上传时间:2023-04-30
- 格式:DOCX
- 页数:20
- 大小:128.65KB
C常见英文面试笔试题.docx
《C常见英文面试笔试题.docx》由会员分享,可在线阅读,更多相关《C常见英文面试笔试题.docx(20页珍藏版)》请在冰豆网上搜索。
C常见英文面试笔试题
C++-常见英文面试笔试题
C/C++Programminginterviewquestionsandanswers
BySatishShetty,July14th,2004
Whatisencapsulation?
?
Containingandhidinginformationaboutanobject,suchasinternaldatastructuresandcode.Encapsulationisolates(使隔离)theinternalcomplexityofanobject'soperationfromtherestoftheapplication.Forexample,aclientcomponentaskingfornetrevenue(收益)fromabusinessobjectneednotknowthedata'sorigin.
Whatisinheritance?
Inheritanceallowsoneclasstoreusethestateandbehaviorofanotherclass.Thederivedclassinheritsthepropertiesandmethodimplementationsofthebaseclassandextendsitbyoverridingmethodsandaddingadditionalpropertiesandmethods.
WhatisPolymorphism?
?
Polymorphismallowsaclienttotreatdifferentobjectsinthesamewayeveniftheywerecreatedfromdifferentclassesandexhibit(展现)differentbehaviors.
Youcanuseimplementation(实现)inheritancetoachievepolymorphisminlanguagessuchasC++andJava.
Baseclassobject'spointercaninvoke(调用)methodsinderivedclassobjects.
YoucanalsoachievepolymorphisminC++byfunctionoverloadingandoperatoroverloading.
Whatisconstructororctor?
Constructorcreatesanobjectandinitializesit.Italsocreatesvtable变量列表?
forvirtualfunctions.Itisdifferentfromothermethodsinaclass.
Whatisdestructor?
Destructorusuallydeletesanyextraresourcesallocatedbytheobject.
Whatisdefaultconstructor?
Constructorwithnoargumentsoralltheargumentshasdefaultvalues.
Whatiscopyconstructor?
Constructorwhichinitializestheit'sobjectmembervariables(byshallowcopying)withanotherobjectofthesameclass.Ifyoudon'timplementoneinyourclassthencompilerimplementsoneforyou.
forexample:
BooObj1(10);//callingBooconstructor
BooObj2(Obj1);//callingboocopyconstructor
BooObj2=Obj1;//callingboocopyconstructor
Whenarecopyconstructorscalled?
Copyconstructorsarecalledinfollowingcases:
a)whenafunctionreturnsanobjectofthatclassbyvalue
b)whentheobjectofthatclassispassedbyvalueasanargumenttoafunction
c)whenyouconstructanobjectbasedonanotherobjectofthesameclass
d)Whencompilergeneratesatemporaryobject
Whatisassignmentoperator?
Defaultassignmentoperatorhandlesassigningoneobjecttoanotherofthesameclass.Membertomembercopy(shallowcopy)
Whatarealltheimplicitmemberfunctionsoftheclass?
Orwhatareallthefunctionswhichcompilerimplementsforusifwedon'tdefineone.?
?
defaultctor
copyctor
assignmentoperator
defaultdestructor
addressoperator
Whatisconversionconstructor?
constructorwithasingleargumentmakesthatconstructorasconversionctoranditcanbeusedfortypeconversion.
forexample:
classBoo
{
public:
Boo(inti);
};
BooBooObject=10;//assigningint10Booobject
Whatisconversionoperator?
?
classcanhaveapublicmethodforspecificdatatypeconversions.
forexample:
classBoo
{
doublevalue;
public:
Boo(inti)
operatordouble()
{
returnvalue;
}
};
BooBooObject;
doublei=BooObject;//assigningobjecttovariableioftypedouble.nowconversionoperatorgetscalledtoassignthevalue.
Whatisdiffbetweenmalloc()/free()andnew/delete?
mallocallocatesmemoryforobjectinheapbutdoesn'tinvokeobject'sconstructortoinitiallizetheobject.
newallocatesmemoryandalsoinvokesconstructortoinitializetheobject.
malloc()andfree()donotsupportobjectsemantics
Doesnotconstructanddestructobjects
string*ptr=(string*)(malloc(sizeof(string)))
Arenotsafe
Doesnotcalculatethesizeoftheobjectsthatitconstruct
Returnsapointertovoid
int*p=(int*)(malloc(sizeof(int)));
int*p=newint;
Arenotextensible
newanddeletecanbeoverloadedinaclass
"delete"firstcallstheobject'sterminationroutine(i.e.itsdestructor)andthenreleasesthespacetheobjectoccupiedontheheapmemory.Ifanarrayofobjectswascreatedusingnew,thendeletemustbetoldthatitisdealingwithanarraybyprecedingthenamewithanempty[]:
-
Int_t*my_ints=newInt_t[10];
...
delete[]my_ints;
whatisthediffbetween"new"and"operatornew"?
"operatornew"workslikemalloc.
Whatisdifferencebetweentemplateandmacro?
?
Thereisnowayforthecompilertoverifythatthemacroparametersareofcompatibletypes.Themacroisexpandedwithoutanyspecialtypechecking.
Ifmacroparameterhasapost-incrementedvariable(likec++),theincrementisperformedtwotimes.
Becausemacrosareexpandedbythepreprocessor,compilererrormessageswillrefertotheexpandedmacro,ratherthanthemacrodefinitionitself.Also,themacrowillshowupinexpandedformduringdebugging.
forexample:
Macro:
#definemin(i,j)(i i: j) template: template Tmin(Ti,Tj) { returni i: j; } WhatareC++storageclasses? auto register static extern auto: thedefault.Variablesareautomaticallycreatedandinitializedwhentheyaredefinedandaredestroyedattheendoftheblockcontainingtheirdefinition.Theyarenotvisibleoutsidethatblock register: atypeofautovariable.asuggestiontothecompilertouseaCPUregisterforperformance static: avariablethatisknownonlyinthefunctionthatcontainsitsdefinitionbutisneverdestroyedandretains=keepitsvaluebetweencallstothatfunction.Itexistsfromthetimetheprogrambeginsexecution extern: astaticvariablewhosedefinitionandplacementisdeterminedwhenallobjectandlibrarymodulesarecombined(linked)toformtheexecutablecodefile.Itcanbevisibleoutsidethefilewhereitisdefined. WhatarestoragequalifiersinC++? Theyare.. const volatile mutable Constkeywordindicatesthatmemoryonceinitialized,shouldnotbealteredbyaprogram. volatilekeywordindicatesthatthevalueinthememorylocationcanbealteredeventhoughnothingintheprogram codemodifiesthecontents.forexampleifyouhaveapointertohardwarelocationthatcontainsthetime,wherehardwarechangesthevalueofthispointervariableandnottheprogram.Theintentofthiskeywordtoimprovetheoptimizationabilityofthecompiler. mutablekeywordindicatesthatparticularmemberofastructureorclasscanbealteredevenifaparticularstructurevariable,class,orclassmemberfunctionisconstant. structdata { charname[80]; mutabledoublesalary; } constdataMyStruct={"SatishShetty",1000};//initlizedbycomplier strcpy(MyStruct.name,"ShilpaShetty");//compilererror MyStruct.salaray=2000;//complierishappyallowed Whatisreference? ? referenceisanamethatactsasanalias,oralternativename,forapreviouslydefinedvariableoranobject. prependingvariablewith"&"symbolmakesitasreference. forexample: inta; int&b=a; &读amp Whatispassingbyreference? Methodofpassingargumentstoafunctionwhichtakesparameteroftypereference. forexample: voidswap(int&x,int&y) { inttemp=x; x=y; y=temp; } inta=2,b=3; swap(a,b); Basically,insidethefunctiontherewon'tbeanycopyofthearguments"x"and"y"insteadtheyrefertooriginalvariablesaandb.sonoextramemoryneededtopassargumentsanditismoreefficient. Whendouse"const"referenceargumentsinfunction? a)Usingconstprotectsyouagainstprogrammingerrorsthatinadvertently不经意的alterdata. b)Usingconstallowsfunctiontoprocessbothconstandnon-constactualarguments,whileafunctionwithoutconstintheprototypecanonlyacceptnonconstantarguments. c)Usingaconstreferenceallowsthefunctiontogenerateanduseatemporaryvariableappropriately. WhenaretemporaryvariablescreatedbyC++compiler? Providedthatfunctionparameterisa"constreference",compilergeneratestemporaryvariableinfollowing2ways. a)Theactualargumentisthecorrecttype,butitisn'tLvalue doubleCube(constdouble&num) { num=num*num*num; returnnum; } doubletemp=2.0; doublevalue=cube(3.0+temp);//argumentisaexpressionandnotaLvalue; b)Theactualargumentisofthewrongtype,butofatypethatcanbeconvertedtothecorrecttype longtemp=3L; doublevalue=cuberoot(temp);//longtodoubleconversion Whatisvirtualfunction? Whenderivedclassoverridesthebaseclassmethodbyredefiningthesamefunction,thenifclientwantstoaccessredefinedthemethodfromderivedclassthroughapointerfrombaseclassobject,thenyoumustdefinethisfunctioninbaseclassasvirtualfunction. classparent { voidShow() { cout<<"i'mparent"< } }; classchild: publicparent { voidShow() { cout<<"i'mchild"< } }; parent*parent_object_ptr=newchild; parent_object_ptr->show()//callsparent->show()i nowwegotovirtualworld... classparent { virtualvoidShow() { cout<<"i'mparent"< } }; classchild: publicparent { voidShow() { cout<<"i'mchild"< } }; parent*parent_object_ptr=newchild; parent_object_ptr->show()//callschild->show() Whatispurevirtualfunction? orwhatisabstractclass? Whenyoudefineonlyfunctionprototypeinabaseclasswithoutimplementationanddothecompleteimplementation实现inderivedclass.Thisbaseclassiscalledabstractclassandclientwon'tabletoinstantiateanobjectusingthisbaseclass. Youcanmakeapurevirtualfunctionorabstractclassthisway.. classBoo { voidfoo()=0; } BooMyBoo;//compilationerror WhatisMemoryalignment? ? Thetermalignmentprimarilymeansthetendency趋向ofanaddress
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 常见 英文 试笔 试题
![提示](https://static.bdocx.com/images/bang_tan.gif)