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

上传人:scccc 文档编号:12097248 上传时间:2021-12-01 格式:DOCX 页数:14 大小:128.35KB
返回 下载 相关 举报
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(14页珍藏版)》请在三一文库上搜索。

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 object's operation from the rest of the

2、 application. For example, a client component asking for net revenue(收益) from a business object need not know the data's 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

3、of the base class and 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 implementa

4、tion(实现) inheritance to achieve polymorphism in languages such as C+ and Java.Base class object's 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 ob

5、ject and initializes it. It also creates 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 defa

6、ult values.What is copy constructor?Constructor which initializes the it's object member variables ( by shallow copying) with another object of the same class. If you don't implement one in your class then compiler implements one for you.for example:Boo Obj1(10); / calling Boo constructorBoo

7、 Obj2(Obj1); / calling boo copy constructorBoo Obj2 = 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 t

8、o a functionc) when you construct an object based on 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 i

9、mplicit member functions of the class? Or what are all the functions which compiler implements for us if we don't define one.?default ctorcopy ctorassignment operatordefault destructoraddress operatorWhat is conversion constructor?constructor with a single argument makes that constructor as conv

10、ersion ctor and it can be used for type conversion.for example:class Boo public: 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 Boo double value; public: Boo(int i ) operat

11、or double() return value; ;Boo BooObject;double i = BooObject; / 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 doesn't invoke object's c

12、onstructor to initiallize the object.new allocates memory and also invokes 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

13、objects that it construct Returns a pointer to void int *p = (int *) (malloc(sizeof(int);int *p = new int;Are not extensible new and delete can be overloaded in a class "delete" first calls the object's termination routine (i.e. its destructor) and then releases the space the object oc

14、cupied on the heap memory. If an array of objects was created using new, then delete must 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

15、new" works like malloc.What is difference between template and macro?There is no way for the compiler to verify that the macro parameters are of compatible types. The macro is expanded without any special type checking.If macro parameter has a post-incremented variable ( like c+ ), the incremen

16、t is performed two times.Because macros are expanded by the preprocessor, compiler error messages will refer to the expanded 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 < j ? i : j)template:t

17、emplate<class T> T min (T i, T j) return i < j ? i : j;What are C+ storage classes?autoregisterstaticexternauto: the default. Variables are automatically created and initialized when they are defined and are destroyed at the end of the block containing their definition. They are not visible

18、 outside that blockregister: a type of auto variable. a suggestion to the compiler to use a CPU register for performancestatic: a variable that is known only in the function that contains its definition but is never destroyed and retains=keep its value between calls to that function. It exists from

19、the time the program begins executionextern: a static variable whose definition and placement is determined when all object and library modules are combined (linked) to form the executable code file. It can be visible outside the file where it is defined.What are storage qualifiers in C+ ?They are.c

20、onstvolatilemutableConstkeyword indicates that memory once initialized, should not be altered by a program.volatilekeyword indicates that the value in the memory location can be altered even though nothing in the programcode modifies the contents. for example if you have a pointer to hardware locati

21、on that contains the time, where hardware changes the value of this pointer variable and not the program. The intent of this keyword to improve the optimization ability of the compiler. mutable keyword indicates that particular member of a structure or class can be altered even if a particular struc

22、ture variable, class, or class member function is constant.struct datachar name80;mutable double salary;const data MyStruct = "Satish Shetty", 1000 ; /initlized by complierstrcpy ( MyStruct.name, "Shilpa Shetty"); / compiler errorMyStruct.salaray = 2000 ; / complier is happy allo

23、wedWhat is reference ?reference is a name that acts as an alias, or alternative name, for a previously defined variable or an object.prepending variable with "&" symbol makes it as reference.for example:int a;int &b = a; & 读 amp What is passing by reference?Method of passing ar

24、guments to a function which takes parameter of type reference.for example:void swap( int & x, int & y ) int temp = x; x = y; y = temp;int a=2, b=3;swap( a, b );Basically, inside the function there won't be any copy of the arguments "x" and "y" instead they refer to or

25、iginal variables a and b. so no extra memory needed to pass arguments and it is more efficient. When do use "const" reference arguments in function?a) Using const protects you against programming errors that inadvertently不经意的 alter data.b) Using const allows function to process both const

26、and non-const actual arguments, while a function without const in the prototype can only accept non constant arguments.c) Using a const reference allows the function to generate and use a temporary variable appropriately. When are temporary variables created by C+ compiler?Provided that function par

27、ameter is a "const reference", compiler generates temporary variable in following 2 ways.a) The actual argument is the correct type, but it isn't Lvaluedouble Cube(const double & num) num = num * num * num; return num;double temp = 2.0;double value = cube(3.0 + temp); / argument is

28、 a expression and not a Lvalue;b) The actual argument is of the wrong type, but of a type that can be converted to the correct typelong temp = 3L;double value = cuberoot ( temp); / long to double conversion What is virtual function?When derived class overrides the base class method by redefining the

29、 same function, then if client wants to access redefined the method from derived class through a pointer from base class object, then you must define this function in base class as virtual function.class parent void Show() cout << "i'm parent" << endl;class child: public pa

30、rent void Show() cout << "i'm child" << endl;parent * parent_object_ptr = new child;parent_object_ptr->show() / calls parent->show() i now we goto virtual world.class parent virtual void Show() cout << "i'm parent" << endl;class child: public

31、 parent void Show() cout << "i'm child" << endl;parent * parent_object_ptr = new child;parent_object_ptr->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 implementati

32、on and do the complete implementation实现 in derived class. This base class is called abstract class and client won't able to instantiate an object using this base class.You can make a pure virtual function or abstract class this way.class Boovoid foo() = 0;Boo MyBoo; / compilation error What is M

33、emory 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 zero in the least significant bit. And a pointer with four byte alignment has a zero in both the two least significant bits.

34、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 might use common global identifiers causing a name collision when an application tries to link with two or more such libraries.

35、 The namespace feature surrounds a library's external declarations with a unique namespace that eliminates消除 the potential for those collisions.namespace identifier namespace-body A namespace declaration identifies and assigns a name to a declarative region.The identifier in a namespace declarat

36、ion 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' declaration?A using declaration makes it possible to use a name from a namespace without the scope范围 operator. What is

37、 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 iterators, bidirectional iterators, random access. An iterator is an entity that gives access to the conten

38、ts 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 some arbitrary order (as in array indices) or according to some ordering relation (as in an ordered bi

39、nary 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 elements to examine. Iterators hide the details of access to and update of the elements of a container class

40、. 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 addresses of the automatic variables from a function or using the address of the memory block after it is f

41、reed.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 where it is caught.抛出异常与栈展开(stack unwinding)抛出异常时,将暂停当前函数的执行,开始查找匹配的catch子句。首先检查throw本身是否在try块内部,如果

42、是,检查与该try相关的catch子句,看是否可以处理该异常。如果不能处理,就退出当前函数,并且释放当前函数的内存并销毁局部对象,继续到上层的调用函数中查找,直到找到一个可以处理该异常的catch。这个过程称为栈展开(stack unwinding)。当处理该异常的catch结束之后,紧接着该catch之后的点继续执行。1. 为局部对象调用析构函数如上所述,在栈展开的过程中,会释放局部对象所占用的内存并运行类类型局部对象的析构函数。但需要注意的是,如果一个块通过new动态分配内存,并且在释放该资源之前发生异常,该块因异常而退出,那么在栈展开期间不会释放该资源,编译器不会删除该指针,这样就会造成

43、内存泄露。2. 析构函数应该从不抛出异常在为某个异常进行栈展开的时候,析构函数如果又抛出自己的未经处理的另一个异常,将会导致调用标准库terminate函数。通常terminate函数将调用abort函数,导致程序的非正常退出。所以析构函数应该从不抛出异常。3. 异常与构造函数如果在构造函数对象时发生异常,此时该对象可能只是被部分构造,要保证能够适当的撤销这些已构造的成员。4. 未捕获的异常将会终止程序不能不处理异常。如果找不到匹配的catch,程序就会调用库函数terminate。Name the operators that cannot be overloaded?sizeof, .,

44、.*, .->, :, ?: 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 container class has a predefined behavior and a well-known interface. A containe

45、r 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; when the container is holding a group of objects that are all th

46、e 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 occurs only at the compiler's discretion灵活性. For exam

47、ple, the compiler does not inline a function if its address is taken or if it is too large to inline. 使用预处理器实现,没有了参数压栈,代码生成等一系列的操作,因此,效率很高,这是它在C中被使用的一个主要原因What is overloading?With the C+ language, you can overload functions and operators. Overloading is the practice of supplying more than one defini

48、tion 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 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

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

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1