C常见英文面试笔试题.docx

上传人:b****8 文档编号:9284760 上传时间:2023-02-04 格式:DOCX 页数:15 大小:126.30KB
下载 相关 举报
C常见英文面试笔试题.docx_第1页
第1页 / 共15页
C常见英文面试笔试题.docx_第2页
第2页 / 共15页
C常见英文面试笔试题.docx_第3页
第3页 / 共15页
C常见英文面试笔试题.docx_第4页
第4页 / 共15页
C常见英文面试笔试题.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

C常见英文面试笔试题.docx

《C常见英文面试笔试题.docx》由会员分享,可在线阅读,更多相关《C常见英文面试笔试题.docx(15页珍藏版)》请在冰豆网上搜索。

C常见英文面试笔试题.docx

C常见英文面试笔试题

ThefinalrevisionwasonNovember23,2020

 

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);Membertomembercopy(shallowcopy)

WhatarealltheimplicitmemberfunctionsoftheclassOrwhatareallthefunctionswhichcompilerimplementsforusifwedon'tdefineone.

defaultctorcopyctorassignmentoperatordefaultdestructoraddressoperator

Whatisconversionconstructor

constructorwithasingleargumentmakesthatconstructorasconversionctoranditcanbeusedfortypeconversion.

forexample:

classBoo{public:

Boo(inti);};

BooBooObject=10;forexample:

classBoo{doublevalue;public:

Boo(inti)operatordouble(){returnvalue;}};

BooBooObject;

doublei=BooObject;nowconversionoperatorgetscalledtoassignthevalue.

Whatisdiffbetweenmalloc()/free()andnew/delete

mallocallocatesmemoryforobjectinheapbutdoesn'tinvokeobject'sconstructortoinitiallizetheobject.

newallocatesmemoryandalsoinvokesconstructortoinitializetheobject.

malloc()andfree()donotsupportobjectsemanticsDoesnotconstructanddestructobjectsstring*ptr=(string*)(malloc(sizeof(string)))ArenotsafeDoesnotcalculatethesizeoftheobjectsthatitconstructReturnsapointertovoidint*p=(int*)(malloc(sizeof(int)));int*p=newint;Arenotextensiblenewanddeletecanbeoverloadedinaclass

"delete"firstcallstheobject'sterminationroutine.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

j)

template:

template

Tmin(Ti,Tj)

{

returni

j;}

WhatareC++storageclasses

autoregisterstaticextern

auto:

thedefault.Variablesareautomaticallycreatedandinitializedwhentheyaredefinedandaredestroyedattheendoftheblockcontainingtheirdefinition.Theyarenotvisibleoutsidethatblock

register:

atypeofautovariable.asuggestiontothecompilertouseaCPUregisterforperformance

static:

avariablethatisknownonlyinthefunctionthatcontainsitsdefinitionbutisneverdestroyedandretains=keepitsvaluebetweencallstothatfunction.Itexistsfromthetimetheprogrambeginsexecution

extern:

astaticvariablewhosedefinitionandplacementisdeterminedwhenallobjectandlibrarymodulesarecombined(linked)toformtheexecutablecodefile.Itcanbevisibleoutsidethefilewhereitisdefined.

WhatarestoragequalifiersinC++

Theyare..

constvolatilemutable

Constkeywordindicatesthatmemoryonceinitialized,shouldnotbealteredbyaprogram.

volatilekeywordindicatesthatthevalueinthememorylocationcanbealteredeventhoughnothingintheprogramcodemodifiesthecontents.forexampleifyouhaveapointertohardwarelocationthatcontainsthetime,wherehardwarechangesthevalueofthispointervariableandnottheprogram.Theintentofthiskeywordtoimprovetheoptimizationabilityofthecompiler.

mutablekeywordindicatesthatparticularmemberofastructureorclasscanbealteredevenifaparticularstructurevariable,class,orclassmemberfunctionisconstant.

structdata{charname[80];mutabledoublesalary;}

constdataMyStruct={"SatishShetty",1000};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=;

doublevalue=cube+temp);classparent{voidShow(){cout<<"i'mparent"<

classchild:

publicparent{voidShow(){cout<<"i'mchild"<

};

parent*parent_object_ptr=newchild;

parent_object_ptr->show().

classparent{virtualvoidShow(){cout<<"i'mparent"<

classchild:

publicparent{voidShow(){cout<<"i'mchild"<

};

parent*parent_object_ptr=newchild;

parent_object_ptr->show()Thisbaseclassiscalledabstractclassandclientwon'tabletoinstantiateanobjectusingthisbaseclass.

Youcanmakeapurevirtualfunctionorabstractclassthisway..

classBoo{voidfoo()=0;}

BooMyBoo;Soapointerwithtwobytealignmenthasazerointheleastsignificantbit.Andapointerwithfourbytealignmenthasazeroinboththetwoleastsignificantbits.Andsoon.Morealignmentmeansalongersequenceofzerobitsinthelowestbitsofapointer.

Whatproblemdoesthenamespacefeaturesolve

Multipleprovidersoflibrariesmightusecommonglobalidentifierscausinganamecollisionwhenanapplicationtriestolinkwithtwoormoresuchlibraries.Thenamespacefeaturesurroundsalibrary'sexternaldeclarationswithauniquenamespacethateliminates消除thepotentialforthosecollisions.

namespace[identifier]{namespace-body}

Anamespacedeclarationidentifiesandassignsanametoadeclarativeregion.Theidentifierinanamespacedeclarationmustbeuniqueinthedeclarativeregioninwhichitisused.Theidentifieristhenameofthenamespaceandisusedtoreferenceitsmembers.

Whatistheuseof'using'declaration

Ausingdeclarationmakesitpossibletouseanamefromanamespacewithoutthescope范围operator.

WhatisanIterator迭代器class

Aclassthatisusedtotraversethrough穿过theobjectsmaintainedbyacontainerclass.Therearefivecategoriesofiterators:

inputiterators,outputiterators,forwarditerators,bidirectionaliterators,randomaccess.Aniteratorisanentitythatgivesaccesstothecontentsofacontainerobjectwithoutviolatingencapsulationconstraints.Accesstothecontentsisgrantedonaone-at-a-timebasisinorder.Theordercanbestorageorder(asinlistsandqueues)orsomearbitraryorder(asinarrayindices)oraccordingtosomeorderingrelation(asinanorderedbinarytree).Theiteratorisaconstruct,whichprovidesaninterfacethat,whencalled,yieldseitherthenextelementinthecontainer,orsomevaluedenotingthefactthattherearenomoreelementstoexamine.Iteratorshidethedetailsofaccesstoandupdateoftheelementsofacontainerclass.Somethinglikeapointer.

Whatisadangling悬挂pointer

Adanglingpointerariseswhenyouusetheaddressofanobjectafteritslifetimeisover.Thismayoccurinsituationslikereturningaddressesoftheautomaticvariablesfromafunctiono

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1