收藏 分享(赏)

c++_常见英文面试笔试题.doc

上传人:无敌 文档编号:700738 上传时间:2018-04-18 格式:DOC 页数:13 大小:60KB
下载 相关 举报
c++_常见英文面试笔试题.doc_第1页
第1页 / 共13页
c++_常见英文面试笔试题.doc_第2页
第2页 / 共13页
c++_常见英文面试笔试题.doc_第3页
第3页 / 共13页
c++_常见英文面试笔试题.doc_第4页
第4页 / 共13页
c++_常见英文面试笔试题.doc_第5页
第5页 / 共13页
点击查看更多>>
资源描述

1、C/C+ Programming interview questions and answers By 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 applicatio

2、n. For example, a client component asking for net revenue from a business object need not know the datas origin.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 class an

3、d extends it by overriding methods and adding additional properties and methods.What is Polymorphism?Polymorphism allows a client to treat different objects in the same way even if they were created from different classes and exhibit different behaviors.You can use implementation inheritance to achi

4、eve polymorphism in languages such as C+ and Java.Base class objects pointer can invoke methods in derived class objects.You can also achieve polymorphism in C+ by function overloading and operator overloading.What is constructor or ctor?Constructor creates an object and initializes it. It also crea

5、tes vtable for virtual functions. It is different from other methods in a class.What is destructor?Destructor usually deletes any extra resources allocated by the object. What is default constructor?Constructor with no arguments or all the arguments has default values.What is copy constructor?Constr

6、uctor which initializes the its object member variables ( by shallow copying) with another 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); / calling boo copy constructorBoo Obj2

7、= Obj1;/ calling boo copy constructorWhen are copy constructors called? Copy constructors are called in following cases: a) when a function returns an object of that class by valueb) when the object of that class is passed by value as an argument to a functionc) when you construct an object based on

8、 another object of the same classd) When compiler generates a temporary objectWhat is assignment operator? Default assignment operator handles assigning one object to another of the same class. Member to member copy (shallow copy)What are all the implicit member functions of the class? Or what are a

9、ll the functions which compiler implements for us if we dont define one.?default ctorcopy ctorassignment operatordefault destructoraddress operatorWhat is conversion constructor?constructor with a single argument makes that constructor as conversion ctor and it can be used for type conversion.for ex

10、ample:class Boopublic:Boo( int i );Boo BooObject = 10 ; / assigning 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(int i )operator double() return value;Boo BooObject;double i = BooObject; /

11、assigning object to variable i of type double. now conversion operator gets called to assign the value.What is diff between malloc()/free() and new/delete?malloc allocates memory for object in heap but doesnt invoke objects constructor to initiallize the object.new allocates memory and also invokes

12、constructor to initialize the object.malloc() and free() do not support object semantics Does not construct and destruct objects string * ptr = (string *)(malloc (sizeof(string)Are not safe Does not calculate the size of the objects that it construct Returns a pointer to void int *p = (int *) (mallo

13、c(sizeof(int);int *p = new int;Are not extensible new and delete can be overloaded in a class “delete“ first calls the objects termination routine (i.e. its destructor) and then releases the space the object occupied on the heap memory. If an array of objects was created using new, then delete must

14、be told that it is dealing with an array by preceding the name with an empty :-Int_t *my_ints = new Int_t10;.delete my_ints;what is the diff between “new“ and “operator new“ ?“operator new“ works like malloc.What is difference between template and macro?There is no way for the compiler to verify tha

15、t the macro parameters are of compatible types. The macro is expanded without any special type checking.If macro parameter has a postincremented variable ( like c+ ), the increment is performed two times.Because macros are expanded by the preprocessor, compiler error messages will refer to the expan

16、ded macro, rather than the macro definition itself. Also, the macro will show up in expanded form during debugging.for example:Macro:#define min(i, j) (i T min (T i, T j) return i show() / calls parent-show() i now we goto virtual world.class parentvirtual void Show() cout show() / calls child-show(

17、) 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 implementation in derived class. This base class is called abstract class and client wont able to instantiate an object using this base class.

18、You can make a pure virtual function or abstract class this way.class Boovoid foo() = 0;Boo MyBoo; / compilation errorWhat is Memory alignment?The term alignment primarily means the tendency of an address pointer value to be a multiple of some power of two. So a pointer with two byte alignment has a

19、 zero in the least significant bit. And a pointer with four byte alignment has a zero in both the two least significant bits. And so on. More alignment means a longer sequence of zero bits in the lowest bits of a pointer.What problem does the namespace feature solve?Multiple providers of libraries m

20、ight use common global identifiers causing a name collision when an application tries to link with two or more such libraries. The namespace feature surrounds a librarys external declarations with a unique namespace that eliminates the potential for those collisions.namespace identifier namespace-bo

21、dy A namespace declaration identifies and assigns a name to a declarative region.The identifier in a namespace declaration must be unique in the declarative region in which it is used. The identifier is the name of the namespace and is used to reference its members.What is the use of using declarati

22、on?A using declaration makes it possible to use a name from a namespace without the scope operator. What is an Iterator class?A class that is used to traverse through the objects maintained by a container class. There are five categories of iterators: input iterators, output iterators, forward itera

23、tors, bidirectional iterators, random access. An iterator is an entity that gives access to the contents of a container object without violating encapsulation constraints. Access to the contents is granted on a one-at-a-time basis in order. The order can be storage order (as in lists and queues) or

24、some arbitrary order (as in array indices) or according to some ordering relation (as in an ordered binary tree). The iterator is a construct, which provides an interface that, when called, yields either the next element in the container, or some value denoting the fact that there are no more elemen

25、ts to examine. Iterators hide the details of access to and update of the elements of a container class. Something like a pointer. What is a dangling pointer?A dangling pointer arises when you use the address of an object after its lifetime is over. This may occur in situations like returning address

26、es of the automatic variables from a function or using the address of the memory block after it is freed.What do you mean by Stack unwinding?It is a process during exception handling when the destructor is called for all local objects in the stack between the place where the exception was thrown and

27、 where it is caught.Name the operators that cannot be overloaded?sizeof, ., .*, .-, :, ?: What is a container class? What are the types of container classes?A container class is a class that is used to hold objects in memory or external storage. A container class acts as a generic holder. A containe

28、r class has a predefined behavior and a well-known interface. A container class is a supporting class whose purpose is to hide the topology used for maintaining the list of objects in memory. When a container class contains a group of mixed objects, the container is called a heterogeneous container;

29、 when the container is holding a group of objects that are all the same, the container is called a homogeneous container. What is inline function?The _inline keyword tells the compiler to substitute the code within the function definition for every instance of a function call. However, substitution

30、occurs only at the compilers discretion. For example, the compiler does not inline a function if its address is taken or if it is too large to inline.What is overloading?With the C+ language, you can overload functions and operators. Overloading is the practice of supplying more than one definition

31、for a given function name in the same scope.- Any two functions in a set of overloaded functions must have different argument lists.- Overloading functions with argument lists of the same types, based on return type alone, is an error. What is Overriding?To override a method, a subclass of the class

32、 that originally declared the method must declare a method with the same name, return type (or a subclass of that return type), and same parameter list.The definition of the method overriding is: Must have same method name. Must have same data type. Must have same argument list. Overriding a method

33、means that replacing a method functionality in child class. To imply overriding functionality we need parent and child classes. In the child class you define the same method signature as one defined in the parent class.What is “this“ pointer?The this pointer is a pointer accessible only within the m

34、ember functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.When a nonstatic member function is called for an object, the address of the object is passed as a hidden argument to the function. F

35、or example, the following function callmyDate.setMonth( 3 );can be interpreted this way:setMonth( The objects address is available from within the member function as the this pointer. It is legal, though unnecessary, to use the this pointer when referring to members of the class.What happens when yo

36、u make call “delete this;“ ?The code has two built-in pitfalls. First, if it executes in a member function for an extern, static, or automatic object, the program will probably crash as soon as the delete statement executes. There is no portable way for an object to tell that it was instantiated on

37、the heap, so the class cannot assert that its object is properly instantiated. Second, when an object commits suicide this way, the using program might not know about its demise. As far as the instantiating program is concerned, the object remains in scope and continues to exist even though the obje

38、ct did itself in. Subsequent dereferencing of the pointer can and usually does lead to disaster.You should never do this. Since 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 f

39、unctions are implemented using a table of function pointers, called the vtable. There is one entry in the table per virtual function in the class. This table is created by the constructor of the class. When a derived class is constructed, its base class is constructed first which creates the vtable.

40、 If the derived class overrides any of the base classes virtual functions, those entries in the vtable are overwritten by the derived class constructor. This is why you should never call virtual functions from a constructor: because the vtable entries for the object may not have been set up by the d

41、erived class constructor yet, so you might end up calling base class implementations of those virtual functionsWhat is name mangling in C+?The process of encoding the parameter types with the function/method name into a unique name is called name mangling. The inverse process is called demangling.Fo

42、r example Foo:bar(int, long) const is mangled 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 reference must always refer to some object and, therefore, must always be i

43、nitialized; pointers do not have such restrictions. A pointer can be reassigned to point to different objects while a reference always refers to an object with which it was initialized.How are prefix and postfix versions of operator+() differentiated?The postfix version of operator+() has a dummy pa

44、rameter of type int. The prefix version does not have dummy parameter.What is the difference between const char *myPointer and char *const myPointer? Const char *myPointer is a non constant pointer to constant data; while char *const myPointer is a constant pointer to non constant data.How can I han

45、dle a constructor that fails?throw an exception. Constructors dont have a return type, so its not possible to use return codes. The best way to signal constructor failure is therefore to throw an exception.How can I handle a destructor that fails?Write a message to a log-file. But do not throw an ex

46、ception. The C+ rule is that you must never throw an exception from a destructor that is being called during the “stack unwinding“ process of another exception. For example, if someone says throw Foo(), the stack will be unwound so all the stack frames between the throw Foo() and the catch (Foo e) will get popped. This is called stack unwinding.

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

当前位置:首页 > 中等教育 > 试题课件

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报