推荐C++常见英文面试笔试题 精品.docx

上传人:b****6 文档编号:6982536 上传时间:2023-01-14 格式:DOCX 页数:14 大小:24.58KB
下载 相关 举报
推荐C++常见英文面试笔试题 精品.docx_第1页
第1页 / 共14页
推荐C++常见英文面试笔试题 精品.docx_第2页
第2页 / 共14页
推荐C++常见英文面试笔试题 精品.docx_第3页
第3页 / 共14页
推荐C++常见英文面试笔试题 精品.docx_第4页
第4页 / 共14页
推荐C++常见英文面试笔试题 精品.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

推荐C++常见英文面试笔试题 精品.docx

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

推荐C++常见英文面试笔试题 精品.docx

推荐C++常见英文面试笔试题精品

C/C++Programminginterviewquestionsandanswers

BySatishShetty,July14th,20XX

Whatisencapsulation?

?

Containingandhidinginformationaboutanobject,suchasinternaldatastructuresandcode.Encapsulationisolatestheinternalplexityofanobject'soperationfromtherestoftheapplication.Forexample,aclientponentaskingfornetrevenuefromabusinessobjectneednotknowthedata'sorigin.

Whatisinheritance?

Inheritanceallowsoneclasstoreusethestateandbehaviorofanotherclass.Thederivedclassinheritsthepropertiesandmethodimplementationsofthebaseclassandextendsitbyoverridingmethodsandaddingadditionalpropertiesandmethods.

WhatisPolymorphism?

?

Polymorphismallowsaclienttotreatdifferentobjectsinthesamewayeveniftheywerecreatedfromdifferentclassesandexhibitdifferentbehaviors.

YoucanuseimplementationinheritancetoachievepolymorphisminlanguagessuchasC++andJava.

Baseclassobject'spointercaninvokemethodsinderivedclassobjects.

YoucanalsoachievepolymorphisminC++byfunctionoverloadingandoperatoroverloading.

Whatisconstructororctor?

Constructorcreatesanobjectandinitializesit.Italsocreatesvtableforvirtualfunctions.Itisdifferentfromothermethodsinaclass.

Whatisdestructor?

Destructorusuallydeletesanyextraresourcesallocatedbytheobject.

Whatisdefaultconstructor?

Constructorwithnoargumentsoralltheargumentshasdefaultvalues.

Whatiscopyconstructor?

Constructorwhichinitializestheit'sobjectmembervariables(byshallowcopying)withanotherobjectofthesameclass.Ifyoudon'timplementoneinyourclassthenpilerimplementsoneforyou.

forexample:

BooObj1(10);//callingBooconstructor

BooObj2(Obj1);//callingboocopyconstructor

BooObj2=Obj1;//callingboocopyconstructor

Whenarecopyconstructorscalled?

Copyconstructorsarecalledinfollowingcases:

a)whenafunctionreturnsanobjectofthatclassbyvalue

b)whentheobjectofthatclassispassedbyvalueasanargumenttoafunction

c)whenyouconstructanobjectbasedonanotherobjectofthesameclass

d)Whenpilergeneratesatemporaryobject

Whatisassignmentoperator?

Defaultassignmentoperatorhandlesassigningoneobjecttoanotherofthesameclass.Membertomembercopy(shallowcopy)

Whatarealltheimplicitmemberfunctionsoftheclass?

Orwhatareallthefunctionswhichpilerimplementsforusifwedon'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?

?

Thereisnowayforthepilertoverifythatthemacroparametersareofpatibletypes.Themacroisexpandedwithoutanyspecialtypechecking.

Ifmacroparameterhasapostincrementedvariable(likec++),theincrementisperformedtwotimes.

Becausemacrosareexpandedbythepreprocessor,pilererrormessageswillrefertotheexpandedmacro,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.asuggestiontothepilertouseaCPUregisterforperformance

static:

avariablethatisknownonlyinthefunctionthatcontainsitsdefinitionbutisneverdestroyedandretainsitsvaluebetweencallstothatfunction.Itexistsfromthetimetheprogrambeginsexecution

extern:

astaticvariablewhosedefinitionandplacementisdeterminedwhenallobjectandlibrarymodulesarebined(linked)toformtheexecutablecodefile.Itcanbevisibleoutsidethefilewhereitisdefined.

WhatarestoragequalifiersinC++?

Theyare..

const

volatile

mutable

Constkeywordindicatesthatmemoryonceinitialized,shouldnotbealteredbyaprogram.

volatilekeywordindicatesthatthevalueinthememorylocationcanbealteredeventhoughnothingintheprogram

codemodifiesthecontents.forexampleifyouhaveapointertohardwarelocationthatcontainsthetime,wherehardwarechangesthevalueofthispointervariableandnottheprogram.Theintentofthiskeywordtoimprovetheoptimizationabilityofthepiler.

mutablekeywordindicatesthatparticularmemberofastructureorclasscanbealteredevenifaparticularstructurevariable,class,orclassmemberfunctionisconstant.

structdata

{

charname[80];

mutabledoublesalary;

}

constdataMyStruct={"SatishShetty",1000};//initlizedbyplier

strcpy(MyStruct.name,"ShilpaShetty");//pilererror

MyStruct.salaray=2000;//plierishappyallowed

Whatisreference?

?

referenceisanamethatactsasanalias,oralternativename,forapreviouslydefinedvariableoranobject.

prependingvariablewith"&"symbolmakesitasreference.

forexample:

inta;

int&b=a;

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)Usingconstprotectsyouagainstprogrammingerrorsthatinadvertentlyalterdata.

b)Usingconstallowsfunctiontoprocessbothconstandnon-constactualarguments,whileafunctionwithoutconstintheprototypecanonlyacceptnonconstantarguments.

c)Usingaconstreferenceallowsthefunctiontogenerateanduseatemporaryvariableappropriately.

WhenaretemporaryvariablescreatedbyC++piler?

Providedthatfunctionparameterisa"constreference",pilergeneratestemporaryvariableinfollowing2ways.

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?

Whenyoudefineonlyfunctionprototypeinabaseclasswithoutimplementationanddothepleteimplementationinderivedclass.Thisbaseclassiscalledabstractclassandclientwon'tabletoinstantiateanobjectusingthisbaseclass.

Youcanmakeapurevirtualfunctionorabstractclassthisway..

classBoo

{

voidfoo()=0;

}

BooMyBoo;//pilationerror

WhatisMemoryalignment?

?

Thetermalignmentprimarilymeansthetendencyofanaddresspointervaluetobeamultipleofsomepoweroftwo.Soapointerwithtwobytealignmenthasazerointheleastsignificantbit.Andapointerwithfourbytealignmenthasazeroinboththetwoleast

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

当前位置:首页 > 高等教育 > 院校资料

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

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