1、推荐C+常见英文面试笔试题 精品C/C+ Programming interview questions and answers By Satish Shetty, July 14th, 20XXWhat is encapsulation?Containing and hiding information about an object, such as internal data structures and code. Encapsulation isolates the internal plexity of an objects operation from the rest of t
2、he application. For example, a client ponent 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 bas
3、e 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 implementation inheritan
4、ce to achieve 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
5、 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 default values.What is copy construc
6、tor?Constructor 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 piler implements one for you.for example:Boo Obj1(10); / calling Boo constructorBoo Obj2(Obj1); / calling boo copy constructorBo
7、o 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 to a functionc) when you construct an object b
8、ased on another object of the same classd) When piler 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 a
9、re all the functions which piler 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 e
10、xample: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 ) operator double() return value; ;Boo BooObject;double i = BooO
11、bject; / 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
12、 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 objects that it construct Returns a pointer to void int *p = (int
13、*) (malloc(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 delet
14、e 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 new works like malloc.What is difference between template and macro?There is no way for the piler to verify that
15、the macro parameters are of patible 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, piler error messages will refer to the expanded macr
16、o, 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:template T min (T i, T j) return i j ? i : j;What are C+ storage classes?autoregisterstaticexternauto: the default. Variables are autom
17、atically created and initialized when they are defined and are destroyed at the end of the block containing their definition. They are not visible outside that blockregister: a type of auto variable. a suggestion to the piler to use a CPU register for performancestatic: a variable that is known only
18、 in the function that contains its definition but is never destroyed and retains its value between calls to that function. It exists from the time the program begins executionextern: a static variable whose definition and placement is determined when all object and library modules are bined (linked)
19、 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.constvolatilemutableConstkeyword indicates that memory once initialized, should not be altered by a program.volatilekeyword indicates that the value in the memory loc
20、ation can be altered even though nothing in the programcode modifies the contents. for example if you have a pointer to hardware location 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 abil
21、ity of the piler. mutable keyword indicates that particular member of a structure or class can be altered even if a particular structure variable, class, or class member function is constant.struct datachar name80;mutable double salary;const data MyStruct = Satish Shetty, 1000 ; /initlized by pliers
22、trcpy ( MyStruct.name, Shilpa Shetty); / piler errorMyStruct.salaray = 2000 ; / plier is happy allowedWhat 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:
23、int a;int &b = a; What is passing by reference?Method of passing arguments 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 wont be any copy of the arguments x
24、and y instead they refer to original 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 proce
25、ss both const 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+ piler?Provided that f
26、unction parameter is a const reference, piler generates temporary variable in following 2 ways.a) The actual argument is the correct type, but it isnt Lvaluedouble Cube(const double & num) num = num * num * num; return num;double temp = 2.0;double value = cube(3.0 + temp); / argument is a expression
27、 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 same functio
28、n, 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 im parent endl;class child: public parent void Show() cout im child show() / calls pa
29、rent-show() i now we goto virtual world.class parent virtual void Show() cout im parent endl;class child: public parent void 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 imple
30、mentation and do the plete implementation in derived class. This base class is called abstract class and client wont 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; / pilation error What 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 zero in the least significant bit. And a pointer with four byte alignment has a zero in both the two least
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1