常见英文面试笔试题目_第1页
常见英文面试笔试题目_第2页
常见英文面试笔试题目_第3页
常见英文面试笔试题目_第4页
常见英文面试笔试题目_第5页
已阅读5页,还剩7页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C/C+ Programmi ng in terviewquesti onsandan swersBy Satish Shetty, July 14th, 2004What is encapsulation?Containing and hiding information about an object, such as internal data structures and code. Encapsulation isolates the internal complexity of an objects operation from the rest of the applicati

2、on. For example, a clie nt comp onent ask ing for net revenue from a bus in ess object n eed not know the datas origi n.What is inheritance?Inheritance allows one class to reuse the state and behavior of another class. The derived class inherits the properties and method implementations of the base

3、class and extends it by overriding methods and addi ng additi onal properties and methods.What is Polymorphism?Polymorphism allows a clie nt to treat differe nt objects in the same way even if they were created from differe nt classes and exhibit differe nt behaviors.You can use implementation inher

4、itance to achieve polymorphism in Ianguages such as C+ and Java.Base class objects poin ter can in voke methods in derived class objects.You can also achieve polymorphism in C+ by fun cti on overloadi ng and operator overloadi ng.What is constructor or ctor?Constructor creates an object and initiali

5、zes it. It also creates vtable for virtual functions. It is differe nt from other methods in a class.What is destructor?Destructor usually deletes any extra resources allocated by the object.What is default constructor?Con structor with no argume nts or all the argume nts has default values.What is

6、copy constructor?Con structor which in itializes the its object member variables ( by shallow copy ing) with ano ther object of the same class. If you dont implement one in your class then compiler implements one for you.for example:Boo Obj1(10); / calling Boo constructorBoo Obj2(Obj1); / calli ng b

7、oo copy con structorBoo Obj2 = Obj1;/ call ing boo copy con structorWhen are copy constructors called?Copy con structors are called in follow ing cases:a) whe n a fun ctio n returns an object of that class by valueb) whe n the object of that class is passed by value as an argume nt to a functionc) w

8、he n you con struct an object based on ano ther object of the same classd) When compiler gen erates a temporary objectWhat is assignment operator?Default assig nment operator han dles assig ning one object to ano ther of the same class. Member to member copy (shallow copy)What are all the implicit m

9、ember functions of the class?Or what are all the functions which compiler implements for us if we dont define one.?default ctorcopy ctorassig nment operatordefault destructoraddress operatorWhat is conversion constructor?con structor with a sin gle argume nt makes that con structor as conversion cto

10、r and it can be used for type conversion.for example:class Boopublic:Boo( int i );Boo BooObject = 10 ; / assig ning int 10 Boo objectWhat is conversion operator?class can have a public method for specific data type conversions.for example:class Boodouble value;public:Boo(i nt i )operator double()ret

11、urn value;Boo BooObject;double i = BooObject; / assig ning object to variable i of type double. now conversion operator gets called to assig n the value.What is diff between malloc()/free() and new/delete?malloc allocates memory for object i n heap but does nt in voke objects con structor to in itia

12、llize the object.new allocates memory and also in vokes con structor to in itialize the object.malloc() and free() do not support object sema nticsDoes not con struct and destruct objectsstri ng * ptr = (stri ng *)(malloc (sizeof(stri ng)Are not safeDoes not calculate the size of the objects that it

13、 con structRetur ns a poin ter to voidint *p = (int *) (malloc(sizeof(i nt);int *p = new int;Are not exte nsiblenew and delete can be overloaded in a classdelete first calls the objects term in atio n rout ine (i.e. its destructor) and the n releases the space the object occupied on the heap memory.

14、 If an array of objects was created using n ew, the n delete must be told that it is deali ng with an array by precedi ng the n ame with an empty :-Int_t *my_i nts = new In t_t1O;delete my_i nts;what is the diff between new and operator new ?operator n ew works like malloc.What is difference between

15、 template and macro?There is no way for the compiler to verify that the macro parameters are of compatible types. The macro is expa nded without any special type check ing.If macro parameter has a posti ncreme nted variable ( like c+ ), the in creme nt is performed two times.Because macros are expan

16、ded by the preprocessor, compiler error messages will refer to the expa nded macro, rather tha n the macro defi niti on itself. Also, the macro will show up in expa nded form duri ng debuggi ng.for example:Macro:#defi ne mi n(i, j) (i j ? i : j) template:templateT min (T i, T j)retur n i j ? i : j;W

17、hat are C+ storage classes?autoregisterstaticexternauto: the default. Variables are automatically created and in itialized whe n they are defi ned and are destroyed at the end of the block containing their definition. They are not visible outside that block register: a type of auto variable. a sugge

18、stion to the compiler to use a CPU register for performa neestatic: a variable that is known only in the function that contains its definition but is never destroyed and retai ns its value betwee n calls to that fun ctio n. It exists from the time the program begi ns executi onextern: a static varia

19、ble whose defi niti on and placeme nt is determ ined whe n all object and library modules are comb ined (li nked) to form the executable code file. It can be visible outside the file where it is defi ned.What are storage qualifiers in C+ ?They are.con stvolatilemutableConstkeyword in dicates that me

20、mory once in itialized, should not be altered by a program. volatilekeyword indicates that the value in the memory location can be altered even though no thi ng in the program code modifies the conten ts. for example if you have a poin ter to hardware locati on that contains the time, where hardware

21、 changes the value of this pointer variable and not the program. The intent of this keyword to improve the optimizatio n ability of the compiler.mutable keyword in dicates that particular member of a structure or class can be altered even if a particular structure variable, class, or class member fu

22、nction is con sta nt.struct datachar n ame80;mutable double salary;con st data MyStruct = Satish Shetty, 1000 ; /in itlized by complierstrcpy ( MyStruct .n ame, Shilpa Shetty); / compiler errorMyStruct.salaray = 2000 ; / complier is happy allowedWhat is reference ?reference is a n ame that acts as a

23、n alias, or alter native n ame, for a previously defi ned variable or an object.prepe nding variable with & symbol makes it as reference.for example:int a;int &b = a;What is passing by reference?Method of pass ing argume nts to a function which takes parameter of type refere nee.for example:void swa

24、p( int & x, int & y )int temp = x;x = y;y = temp;int a=2, b=3;swap( a, b );Basically, in side the function there wont be any copy of the argume nts x and y i nstead they refer to origi nal variables a and b. so no extra memory n eeded to pass argume nts and it is more efficie nt.When do use const re

25、ference arguments in function?a) Using const protects you aga inst program ming errors that in adverte ntly alter data.b) Using const allows function to process both const and non-const actual arguments, while a function without const in the prototype can only accept non con sta nt argume nts.c) Usi

26、ng a const reference allows the function to gen erate and use a temporary variable appropriately.When are temporary variables created by C+ compiler?Provided that fun cti on parameter is a const referen ce, compiler gen erates temporary variable in followi ng 2 ways.a) The actual argume nt is the co

27、rrect type, but it isnt Lvaluedouble Cube(c onst double & num)num = num * num * num;return num;double temp = 2.0;double value = cube(3.0 + temp); / argume nt is a expressi on and not a Lvalue;b) The actual argume nt is of the wrong type, but of a type that can be con verted to the correct type long

28、temp = 3L;double value = cuberoot ( temp); /long to double conversionWhat is virtual function?When derived class overrides the base class method by redefi ning the same function, the n if clie nt wants to access redefi ned the method from derived class through a poin ter from base class object, then

29、 you must defi ne this function in base class as virtual fun cti on.class pare ntvoid Show()cout im pare nt en dl;class child: public pare ntvoid Show()cout im child show() / calls pare nt-show() inow we goto virtual world.class pare ntvirtual void Show()cout im pare nt en dl;class child: public par

30、e ntvoid Show()cout im child show() / calls child-show()What is pure virtual function? or what is abstract class?When you define only function prototype in a base class without implementation and do the complete impleme ntati on in derived class. This base class is called abstract class and clie nt

31、wont able to in sta ntiate an object using this base class.You can make a pure virtual fun cti on or abstract class this way.class Boovoid foo() = 0;Boo MyBoo; / compilati on errorWhat is Memory alignment?The term alig nment primarily means the tendency of an address poin ter value to be a multiple

32、of some power of two. So a poin ter with two byte alig nment has a zero in the least sig ni fica nt bit.And a poin ter with four byte alig nment has a zero in both the two least sig ni fica nt bits. And so on. More alig nment mea ns a Ion ger seque nee of zero bits in the lowest bits of a poin ter.W

33、hat problem does the namespace feature solve? Multiple providers of libraries might use com mon global ide ntifiers caus ing a n ame collisi on whe n an applicati on tries to link with two or more such libraries. The n amespace feature surro unds a librarys external declarations with a unique namesp

34、ace that eliminates the potential for those collisi ons.n amespace ide ntifier n amespace-body A n amespace declaratio n ide ntifies and assig ns a n ame to a declarative regi on.The ide ntifier in a n amespace declarati on must be unique in the declarative regi on in which it is used. The ide ntifi

35、er is the n ame of the n amespace and is used to reference its members.What is the use of using declaration?A using declarati on makes it possible to use a n ame from a n amespace without the scope operator.What is an Iterator class?A class that is used to traverse through the objects maintained by

36、a container class. There are five categories of iterators: in put iterators, output iterators, forward iterators, bidirect ional iterators, random access. An iterator is an entity that gives access to the contents of a container object without violati ng en capsulati on con stra in ts. Access to the

37、 contents is gran ted on a on e-at-a-time basis in order. The order can be storage order (as in lists and queues) or some arbitrary order (as in array in dices) or accord ing to some orderi ng relati on (as in an ordered binary tree). The iterator is a con struct, which provides an in terface that,

38、whe n called, yields either the next eleme nt in the container, or some value deno ti ng the fact that there are no more eleme nts to exam in e. Iterators hide the details of access to and update of the elements of a container class. Something like a poi nter.What is a dangling pointer?A dan gli ng

39、poin ter arises whe n you use the address of an object after its lifetime is over. This may occur in situati ons like retur ning addresses of the automatic variables from a fun cti on or using the address of the memory block after it is freed.What do you mean by Stack unwinding?It is a process duri

40、ng excepti on han dli ng when the destructor is called for all local objects in the stack betwee n the place where the exceptio n was throw n and where it is caught.Name the operators that cannot be overloaded?sizeof, ., .*, .-, :, ?:What is a container class? What are the types of container classes

41、?A container class is a class that is used to hold objects in memory or exter nal storage. A container class acts as a gen eric holder. A container class has a predefi ned behavior and a well-k nown in terface. A container class is a support ing class whose purpose is to hide the topology used for m

42、ain tai ning the list of objects in memory. When a container class contains a group of mixed objects, the container is called a heteroge neous container; whe n the container is holdi ng a group of objects that are all the same, the container is called a homoge neous container.What is inline function

43、?The _in li ne keyword tells the compiler to substitute the code with in the function defi niti on for every in sta nee of a fun ctio n call. However, substitutio n occurs on ly at the compilers discreti on. For example, the compiler does not inline a function if its address is take n or if it is to

44、o large to inline.What is overloading?With the C+ Ian guage, you can overload functions and operators. Overloadi ng is the practice of suppl ying more tha n one defi niti on for a give n fun cti on n ame in the same scope.-Any two functions in a set of overloaded functions must have differe nt argum

45、e nt lists.-Overloadi ng fun cti ons with argume nt lists of the same types, based on retur n type alon e, is an error.What is Overriding?To override a method, a subclass of the class that origi nally declared the method must declare a method with the same n ame, retur n type (or a subclass of that

46、retur n type), and same parameter list. The defi niti on of the method overridi ng is:Must have same method n ame.Must have same data type.Must have same argume nt list.Overriding a method means that replacing a method functionality in child class. To imply overridi ng fun ctio nality we n eed pare

47、nt and child classes. In the child class you defi ne the same method sig nature as one defi ned in the pare nt class.What is this pointer?The this poin ter is a poin ter accessible only with in the member functions of a class, struct, or union type. It points to the object for which the member funct

48、ion is called. Static member functions do not have a this poin ter.When a non static member function is called for an object, the address of the object is passed as a hidde n argume nt to the fun ctio n. For example, the followi ng fun cti on call myDate.setMo nth( 3 );can be in terpreted this way:s

49、etMo nth( &m yDate, 3 );The objects address is available from within the member fun ctio n as the this poin ter. It is legal, though unn ecessary, to use the this poin ter when referri ng to members of the class.What happens when you make call delete this; ?The code has two built-i n pitfalls. First

50、, if it executes in a member function for an exter n, static, or automatic object, the program will probably crash as soon as the delete stateme nt executes. There is no portable way for an object to tell that it was instantiated on the heap, so the class cannot assert that its object is properly in

51、 sta ntiated. Second, whe n an object commits suicide this way, the using program might not know about its demise. As far as the in sta ntiati ng program is concern ed, the object rema ins in scope and continues to exist even though the object did itself in. Subseque nt derefere ncing of the poin te

52、r can and usually does lead to disaster.You should n ever do this. Si nee compiler does not know whether the object was allocated on the stack or on the heap, delete this could cause a disaster.How virtual functions are implemented C+?Virtual fun cti ons are impleme nted using a table of fun ctio n

53、poin ters, called the vtable. There is one en try in the table per virtual fun ctio n in the class. This table is created by the con structor of the class. When a derived class is con structed, its base class is con structed first which creates the vtable. If the derived class overrides any of the b

54、ase classes virtual functions, those en tries in the vtable are overwritte n by the derived class con structor. This is why you should n ever call virtual functions from a con structor: because the vtable en tries for the object may not have bee n set up by the derived class con structor yet, so you

55、 might end up call ing base class impleme ntati ons of those virtual fun ctio nsWhat is name mangling in C+?The process of en codi ng the parameter types with the fun ctio n/method n ame into a unique n ame is called n ame man gli ng. The in verse process is called dema ngli ng.For example Foo:bar(i

56、 nt, I ong) const is man gled as bar_C3Fooil.For a constructor, the method name is left out. That is Foo:Foo(int, long) const is mangled as _C3Fooil.What is the difference between a pointer and a reference?A refere nee must always refer to some object and, therefore, must always be in itialized; poi

57、 nters do not have such restrict ions. A poin ter can be reassig ned to point to differe nt objects while a refere nee always refers to an object with which it was in itialized.How are prefix and postfix versions of operator+() differentiated?The postfix version of operator+() has a dummy parameter of type int. The prefix version does not have dummy parameter.What is the difference between const char *myPointe

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论