SRM University

For Private circulation only Programming with C

C, C++ Interview Questions

1. How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8characters? Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16 and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns. 2. What is the difference between declaring a variable and defining a variable? In declaration we just mention the type of the variable and it's name. We do not initialize it. But defining means declaration + initialization. Example int i; is just a declaration where int i= 10; are definition. 3. What is the difference between end of line and end of file characters? End of Line refers to the '\n'( new line character). It will perform operations till it come across '\n'. EOF refers to End of File. It is the last character in the file (if it is not empty). If the file is empty then only EOF will be present. Some compilers give its value as -1. 4. What is the difference between break and continue statement? break statement: A statement that terminates the current loop iteration and causes execution to break out of a loop. continue statement : A statement that terminates the current loop iteration and persists the loop with the next iteration.

5. What will be the output? main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(" %d ",*c); for(j=0;j<5;j++){ printf(" %d ",*p); ++p; } } Answer: 2222223465 Explanation: Initially pointer c is assigned to both p and q. In the first loop, since only q is incremented and not c , the value 2 will be printed 5 times. In second loop p itself is incremented. So the values 2 3 4 6 5 will be printed. Compiled by H. Srimathi 1

++q;

}

SRM University

For Private circulation only

C, C++ Interview Questions

6. What will be the output? main() { char *p; printf("%d %d ",sizeof(*p),sizeof(p)); } Answer: 12 Explanation: The sizeof() operator gives the number of bytes taken by its operand. p is a character pointer, which needs one byte for storing its value (a character). Hence sizeof(*p) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof(p) gives 2. 7. What will be the output? main() { int i=3; switch(i) { default: printf("zero"); case 1: printf("one"); break; case 2: printf("two"); break; case 3: printf("three"); break; }} Answer : three Explanation : The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match.

8. What will be the output? main() { printf("%x",-1<<4); } Answer: fff0 Compiled by H. Srimathi 2

Explanation: Function names are just addresses (just like array names are addresses). The evaluation is by popping out from the stack and also from right to left. 9.400 Explanation: printf takes the values of the first two assignments of the program.. main() is also a function.%d"). ++i . Any number of printf's may be given. %p in printf specifies that the argument is an address. So the address of function main will be printed. i). What will be the output? main() { int i=5.SRM University For Private circulation only C. 10. If more number of assignments given in the program. They are printed as hexadecimal numbers.j=300. 11. } Answer: 300. The %x format specifier specifies that the integer value be printed as a hexadecimal value.. C++ Interview Questions Explanation : -1 is internally represented as all 1's. printf("%d. printf("%d%d%d%d%d%d". What will be the output? main() { int i=400. What will be the output? main() { printf("%p". When left shifted four times the least significant 4 bits are filled with 0's. --i. } Answer: 45545 Explanation: The arguments in a function call are pushed into the stack from left to right. Srimathi 3 .main). Compiled by H. } Answer: Some address will be printed. i++. i--. hence the result. All of them take only the first two values.then printf will take garbage values.

3.printf("%d".a+3. printf(“\n %d %d %d”. printf(“\n %d %d %d”. printf(“\n %d %d %d”. int *p[ ] = {a. 14.i). /* value 10 is given as input here */ } Answer: 1 Explanation: Scanf returns number of items successfully read and not 10. What will be the output? main( ){ static int a[ ] = {0. *ptr++. printf(“\n %d %d %d”. What will be the output? main() { int i. for(. } Compiled by H.1. int **ptr = p.a+4}. Here 10 is given as input which should have been scanned successfully.i++. ptr-p. ptr-p. **ptr). ptr-p. ++*ptr.a+1. **ptr).scanf("%d". printf("%d". *ptr-a.&i)).i)) . Here it evaluates to 0 (false) and comes out of the loop. *ptr-a. } Answer: 1 Explanation: Before entering into the for loop the checking condition is "evaluated". ptr-p. *ptr-a. Srimathi 4 .SRM University For Private circulation only C. *ptr-a. So number of items read is 1.2.a+2. 13. ptr++. *++ptr. **ptr). printf("%d". C++ Interview Questions 12. and i is incremented (note the semicolon after the for loop). **ptr). What will be the output? main() {int i=0.4}.

**ptr = 2. printf("i = %d. 1002 has a value 102 so the value is (102 – 100)/(scaling factor) = 1. *ptr – a = 2. +i = -1 Explanation: Compiled by H. main() { int i=-1. Srimathi 5 What will be the output? . After execution of *++ptr increments value of the value in ptr by scaling factor. the outputs for the second printf are ptr – p = 2. the outputs for the fourth printf are ptr – p = 1006 – 1000 = 3. *ptr – a = 3. *ptr – a = value at address pointed by ptr – starting value of array a. Hence the output of the firs printf is 1. so it becomes1004. if scaling factor for integer is 2 bytes. the value pointed by the value is incremented by the scaling factor. Hence. Hence. C++ Interview Questions Answer: 111 222 333 344 Explanation: Let us consider the array and the two pointers with some address a 0 1 2 3 4 100 102 104 106 108 p 100 102 104 106 108 1000 1002 1004 ptr 1006 1008 1000 2000 After execution of the instruction ptr++ value in ptr becomes 1002. After execution of *ptr++ increments value of the value in ptr by scaling factor.SRM University For Private circulation only C. So the value in array p at location 1006 changes from 106 10 108. *ptr – a = 108 – 100 = 4. Hence. After execution of ++*ptr value in ptr remains the same. **ptr is the value stored in the location pointed by the pointer of ptr = value pointed by value pointed by 1002 = value pointed by 102 = 1. 15.i. so it becomes1004. } Answer: i = -1. **ptr = 3. (1002 – 1000) / (scaling factor) = 1. 1. +i = %d \n". Now ptr – p is value in ptr – starting location of array p. +i).. **ptr = 4. the outputs for the third printf are ptr – p = 3. +i. 1.

Where-ever it comes you can just ignore it just because it has no effect in the expressions (hence the name dummy operator). In second sizeof the name str2 indicates the name of the array whose size is 5 (including the '\0' termination character).sizeof(str2). Each time the function is called its return address is stored in the call stack. 17. } Answer: Runtime error : Stack overflow. main() { char *str1="abcd". The third sizeof is similar to the second one. So it terminates the program and results in an error. } Answer: 255 Explanation: In first sizeof. 16. Srimathi 6 What will be the output? .sizeof("abcd")). Answer : a: The SEEK_SET sets the file position marker to the starting of the file. What will be the position of the file marker? a: fseek(ptr. C++ Interview Questions Unary + is the only dummy operator in C. main() { main(). Compiled by H.SEEK_CUR).SRM University For Private circulation only C. b: The SEEK_CUR sets the file position marker to the current position of the file. str1 is a character pointer so it gives you the size of the pointer variable. printf("%d %d %d". Since there is no condition to terminate the function call.0.SEEK_SET). the call stack overflows at runtime. What will be the output? Explanation: main function calls itself again and again. b: fseek(ptr.0. 18.sizeof(str1). char str2[]="abcd".

i has value 20 and so printf prints 20. 20.10 Explanation: '{' introduces new block and thus new scope.i). So it prints i's value as 10. Srimathi 7 . printf("%d".20. After compilation is over the linker resolves it to global variable i (since it is the only variable visible there).i). not=!2. } printf("%d".i). and any non-zero value is considered to be the boolean value TRUE. main() { char not. printf("%d". !TRUE is FALSE (0) so it prints 0. What will be the output? int i=10. Compiled by H.SRM University 19. In the outermost block. so no storage space is allocated for it. i is declared as extern. main() { extern int i. In C the value 0 is considered to be the boolean value FALSE. { const volatile unsigned i=30. C++ Interview Questions What will be the output? ! is a logical operator. In the next block. So printf prints 30. } Answer: 0 Explanation: For Private circulation only C. { int i=20. In the innermost block i is declared as. Here 2 is a non-zero value so TRUE. } printf("%d". i is assumed of type int. } Answer: 30. const volatile unsigned which is a valid declaration.not).

But the lifetime of i is lifetime of the function so it lives upto the exit of main function. } printf("%d". What will be the output? #include<stdio. } Answer: Compiler error Explanation: i is a constant. printf("%d %f". Since the i is still allocated space. you cannot change the value of constant Compiled by H. *j prints the value stored in i since j points i. i. j=&i. { int i=10.SRM University For Private circulation only C. main() What will be the output? { int *j. C++ Interview Questions 21.++j).h> main() { const int i=4. Srimathi 8 . float j. } Answer: 10 Explanation: The variable i is a block level variable and the visibility is inside that block only.*j). 22. j = ++i.

c". 10 will be returned. What will be the output? main(){ unsigned int i."r").i>-2. char i. i. for(i=1. } int _l_abc(int i) { return(i++). } Answer: contents of zzz.--i). C++ Interview Questions What will be the output? main() { int i=_l_abc(10).i). } Answer: 9 Explanation: return(i++) it will first return i and then increments. it should be checked against NULL. Srimathi 9 .h> main() { FILE *ptr. 25. while((i=fgetch(ptr))!=EOF) printf("%c". 24.e. printf("%d\n".c followed by an infinite loop Explanation: The condition is checked against EOF. What will be the output? #include<stdio.SRM University 23. } Compiled by H.i--) printf("c aptitude"). For Private circulation only C. ptr=fopen("zzz.

so the value of static ‘i’ ie. The unsigned equivalent of -2 is a huge value so condition becomes false and control comes out of the loop. Compiled by H. hence memory for ‘i’ will be allocated for only once. Srimathi 10 . as it encounters the statement. 27. What will be the output? void main() { static int i=i++. 26. i. It is compared with a signed value. printf(“i = %d j = %d k = %d”. if(--i){ main(). C++ Interview Questions i is an unsigned integer.i).SRM University Explanation: For Private circulation only C. and since main() is recursively called. j. j=j++. What will be the output? void main() { static int i=5. Since the both types doesn't match. } Answer: i=1j=1k=1 Explanation: Since static variables are initialized to zero by default. k=k++. 0 will be printed every time the control is returned. signed is promoted to unsigned value. printf("%d ". } } Answer: 0000 Explanation: The variable "i" is declared as static.. k). The function main() will be called recursively unless ‘i’ becomes equal to 0.

} } Answer: Garbage values Explanation: The inner printf executes first to print some garbage value. else continue. The result is as simple as 1 + 3 = 4 Compiled by H.printf("%d"))) break. C++ Interview Questions 28.*a+1-*a+3).>=0 will always be true. What will be the output? void main() { while(1){ if(printf("%d". leading to an infinite loop. } Answer: 4 Explanation: *a and -*a cancels out. Still the outer printf prints something and so returns a non-zero value. Srimathi 11 . What will be the output? main() { unsigned int i=10.i).. The printf returns no of characters printed and this value also cannot be predicted. while(i-->=0) printf("%u ". Explanation: Since i is an unsigned integer it can never become negative. 30. What will be the output? main() { int a[10]. So it encounters the break statement and comes out of the while statement 29. printf("%d".SRM University For Private circulation only C. So the expression i-. } Answer: 10 9 8 7 6 5 4 3 2 1 0 65535 65534….

The condition in the for loop fails and so comes out of the for loop. while(i--!=0) which is false and so breaks out of while loop. while(+(+i--)!=0) i-=i++. What will be the output? main() { signed char i=0. THe initial value of the i is set to 0. } Answer -128 Explanation Notice the semicolon at the end of the for loop.i). Is the following statement a declaration/definition. printf("%d\n". 32. It prints the current value of i that is -128.i++) .x is a pointer to array of(size 10) integers. Find what does it mean? int (*x)[10]. C++ Interview Questions 31. Apply clock-wise rule to find the meaning of this definition Compiled by H. printf("%d". The value –1 is printed due to the post-decrement operator.i>=0. } Answer: -1 Explanation: Unary + is the only dummy operator in C. Srimathi 12 .i). Answer Definition. So it has no effect on the expression and now the while loop is. 33. The inner loop executes to increment the value from 0 to 127 (the positive range of char) and then it rotates to the negative value of -128. for(.SRM University For Private circulation only C. What will be the output? main() { int i=0.

y). What will be the output? main() { int i=5. } Answer x = 20 y = 10 Explanation This is one way of swapping two values. In the inner expression. Srimathi 13 . *b=*a-*b. What will be the output? int swap(int *a. } Compiled by H. 35. printf("x= %d y = %d\n".&y). printf(“%d”. printf(“%d”. while(i<=10) (i>2)?i++:i--. ++i is equal to 6 yielding true(1). i). } main() { int x=10. Simple checking will help understand this. C++ Interview Questions 34. What will be the output? void main() {static int i.SRM University For Private circulation only C.int *b) { *a=*a+*b. because == is of higher precedence than = operator.x. *a=*a-*b.i=++i ==6). } Answer: 1 Explanation: The expression can be treated as i = (++i==6). swap(&x.y=20. Hence the result 36.

39. struct a b.5. printf("\n%d %d %d".*f2). 38. f1=f2=&a. Because this will cause the structure declaration to be recursive without end. *f2+=*f2+=a+=2. } Answer: 16 16 16 Explanation: f1 and f2 both refer to the same memory location a.*f1. Srimathi 14 . What will be the output? main() { int a=2. This continues till the integer value rotates to positive value (32767). Inside the while loop the conditional operator evaluates to false.*f1. struct a *b.*f2. printing the i value 37. C++ Interview Questions Explanation: Since i is static it is initialized to 0. } Answer: No Explanation: Is it not legal for a structure to contain a member that is of the same type as in this case. } Compiled by H.a. The while condition becomes false and hence. Is the following code legal? struct a { int x.SRM University Answer: 32767 For Private circulation only C. So changes through f1 and f2 ultimately affects only the value of a. executing i--. comes out of the while loop. Is the following code legal? struct a { int x.

the compiler treats the operator sequence / and * as /* which happens to be the starting of comment. sizeof(double *)). } Answer : sizeof (void *) = 2 sizeof (int *) = 2 sizeof (double *) = 2 sizeof(struct unknown *) = 2 Explanation: The pointer to any type is of same size. the size of the pointer to a structure even before the size of the structure is determined(as you know the pointer to any type is of same size). sizeof(struct unknown *)). To force what is intended by the programmer. 40. For Private circulation only C. What will be the output? void main() { printf(“sizeof (void *) = %d \n“. but by the “maximum munch” rule. int k = *ip/*jp. Srimathi 15 . Explanation: The programmer intended to divide two integers. printf(“sizeof (double *) = %d \n”.k). printf(“sizeof (int *) = %d \n”.SRM University Answer: Yes. 41. This type of structures is known as ‘self-referencing’ structure.What will be the output? void main() { int i=10. } Answer: Compiler Error: “Unexpected end of file in comment started in line 5”. C++ Interview Questions Explanation: *b is a pointer to type struct a and so is legal. *jp = &j. sizeof( void *)). sizeof(int *)). j=2. printf(“sizeof(struct unknown *) = %d \n”. printf(“%d”. Compiled by H. The compiler knows. int *ip= &i.

void *k.What will be the output? main() { int a=10.a). j=k=&a. 43. No pointer arithmetic can be done on it and you cannot apply indirection operator (*) on void pointers.SRM University For Private circulation only C.k). So it prints the HELL correctly and continues to print garbage values till it accidentally comes across a NULL character. /* put braces to force the intention will solve the problem. What will be the output? main() { char a[4]="HELL". Compiled by H. C++ Interview Questions int k = *ip/ *jp. printf("%s". /* give space explicity separating / and * Or */ int k = *ip/(*jp). } Answer: HELL%@!~@!@???@~~! Explanation: The character array has the memory just enough to hold the string “HELL” and doesnt have enough space to store the terminating null character.j.*j. Srimathi 16 . j++. k++. */ 42. printf("\n %u %u ". } Answer: Compiler error: Cannot increment a void pointer Explanation: Void pointers are generic pointers and they can be used only when the type is not known and as an intermediate address storage type.

What is the difference between # include <file> and # include “file” #include<file> This will refer the given file in the standard input and output directory. most floating-point units are unable to explicitly calculate the square root of negative numbers. if((printf("hello"))==0) printf("hello"). if "condition" printf("Hello"). Srimathi 17 . 45.SRM University For Private circulation only C. but #include"file" This will first refers the given file in the current directory if this not found it will refers in the standard input and output directory. especially in floating-point calculations. so the output will be HelloWorld.What is TSR? Terminate and Stay Resident (TSR) is a system call in DOS operating systems that returned control to the system as if the program had quit. but kept the program in memory. and will instead indicate that the operation was invalid and return a NaN result. Illegal location means either the location is in the operating systems address space or in the other processes memory space. For example. 48. by transferring control back to the terminated program on automatic or externally-generated events 46.What is the function used to convert ASCII to Integer? atoi() is the function used to convert ASCII to Integer. Compiled by H. 49.what should be the condition. 47. It occurs due to various reasons one is that your program has tried to access an illegal memory location. So whenever your program tries to access 0th location the operating system kills your program with runtime assignment error because the 0th location is in the operating systems address space and operating system does not allow access to its address space by user program. NaN (Not a Number) is a value or symbol that is usually produced as the result of an operation on invalid input operands.What is NaN? In computing. In stdio. else printf("world"). Many software vendors use the call to create the appearance of multitasking.What is NULL pointer assignment? A NULL pointer assignment is a runtime error. C++ Interview Questions 44.h NULL is defined as 0. else printf("World") .

Is it possible to execute code even after the program exits the main() function? Answer 1: The standard C library provides a function named atexit() that can be used to perform cleanup operations when your program terminates. NUL is the name of the first character in the ASCII character set. 51. C++ Interview Questions 50. What is pragma? The #pragma preprocessor directive allows each compiler to implement compiler-specific features that can be turned on and off with the #pragma statement. Srimathi 18 . To implement this option using the #pragma directive.h> for the null pointer. any function can be executed after program exits the main function but function must be declared before pragma directive is reached. 52. For instance. There is no standard macro NUL in C. you can turn off loop optimization by inserting the following line into your code: #pragma loop_opt(off) Compiled by H. your compiler might support a feature called loop optimization. This feature can be invoked as a command-line option or as a #pragma directive. You can set up a set of functions you want to perform automatically when your program exits by passing function pointers to the at exit() function. you would put the following line into your code: #pragma loop_opt(on) Conversely. Answer 2: This can be done by using #pragma directive as: #pragma exit <function name> Using above code. It corresponds to a zero value. What is the difference between NULL and NUL? NULL is a macro defined in <stddef.SRM University For Private circulation only C.

Is using exit() the same as using return? No.c extention before compiling it. the return statement and exit() function are similar. you are essentially returning control to the calling function. 54. In this case. How can C programs run without header files? If we save our program with . Compiled by H. The exit() function is used to exit your program and return control to the operating system. C++ Interview Questions 53. If you issue a return from the main() function. The return statement is used to return from a function and return control to the calling function. Srimathi 19 . the compiler with automatically include the header files.SRM University For Private circulation only C. which is the operating system. It will not show any error.

So when PrintVal is called it meets up with ptr that has been freed. ptr = new int(i). } delete ptr. 2. while returning from the function. s1.Which is the parameter that is added to every non-static member function when it is called? Answer: ‘this’ pointer Compiled by H.} Answer: Say i am in someFunc Null pointer assignment(Run-time error) Explanation: As the object is passed by value to SomeFunc the destructor of the object is called when the control returns from the function.What will be the output? class Sample {public: int *ptr. Sample(int i) { ~Sample() { void PrintVal() { }. SomeFunc(s1).} int main() {Sample s1= 10. } because when we pass objects by refernece that object is not destroyed. 3.SRM University For Private circulation only C. Srimathi 20 . } cout << "The value is " << *ptr. and a class defaults to the private access specifier and private base class inheritance. What are the differences between a C++ struct and C++ class? The C++ struct has all the features of the class.PrintVal(). The only differences are that a struct defaults to public member access and public base class inheritance. C++ Interview Questions C++ 1.The solution is to pass the Sample object by reference to SomeFunc: void SomeFunc(Sample &x) {cout << "Say i am in someFunc " << endl. } void SomeFunc(Sample x) {cout << "Say i am in someFunc " << endl.

5). deri DeriArr[5]. cout<<endl. When Somefunc is called the second time the argument passed is the pointeer to an array of derived class objects and not the array of base class objects. so it works correctly and prints the bval of all the objects. } int main() { base BaseArr[5]. } Answer: 00000 01010 Explanation: The function SomeFunc expects two arguments. class deri : public base { public: int dval. int size) { for(int i=0. i<size.arr++) cout<<arr->bval. But that is what the function expects to be sent. So when arr++ is met.} }. Compiled by H.SRM University For Private circulation only C.The first one is a pointer to an array of base class objects and the second one is the sizeof the array.What will be the output? class base { public: int bval. C++ Interview Questions 4.5). base(){ bval=0. SomeFunc() knows nothing about this and just treats the pointer as an array of base class objects. deri(){ dval=1. the size of base class object is taken into consideration and is incremented by sizeof(int) bytes for bval (the deri class objects have bval and dval as members and so is of size >= sizeof(int)+sizeof(int) ). SomeFunc(DeriArr. So the derived class pointer is promoted to base class pointer and the address is sent to the function. SomeFunc(BaseArr. i++.} }. void SomeFunc(base *arr. Srimathi 21 .The first call of someFunc calls it with an array of bae objects.

SomeFunc expects a pointer to a base class.} }. Since a pointer to a derived class object is passed.} }. class deri:public base { public: void baseFun(){ cout<< "from derived"<<endl. } Answer: from base from base Explanation: As we have seen in the previous case.What will be the output? class base { public: virtual void baseFun(){ cout<<"from base"<<endl. Compiled by H. C++ Interview Questions 5. it treats the argument only as a base class pointer and the corresponding base function is called. class deri : public base { public: void baseFun(){ cout<< "from derived"<<endl.What will be the output? class base { public: void baseFun(){ cout<<"from base"<<endl. int main() {base baseObject. SomeFunc(&baseObject).SRM University For Private circulation only C. 6.} }. Srimathi 22 . SomeFunc(&deriObject).} void SomeFunc(base *baseObj) { baseObj->baseFun(). deri deriObject.} }.

So this code issues an error. Srimathi 23 . That means that it supports run-time polymorphism.} int main() { base baseObject. deri deriObject. cout <<"a="<<a <<"*pa="<<*pa <<"ra"<<ra . 7. C++ Interview Questions void SomeFunc(base *baseObj) { baseObj->baseFun(). } Answer : Compiler Error: 'ra'. One of the main differences is that the pointers can be both initialized and assigned. pa = &a.reference must be initialized Explanation : Pointers are different from references. So the function corresponding to the derived class object is called. ra = a. Compiled by H. } Answer: from base from derived Explanation: Remember that baseFunc is a virtual function. &ra. SomeFunc(&deriObject).SRM University For Private circulation only C.What will be the output? void main() { int a. *pa. whereas references can only be initialized. SomeFunc(&baseObject).

3. print(b). void print(int *ptr) { cout<<ptr[0]. } Answer: Compiler Error : function 'void print(int *)' already has a body Explanation: Arrays cannot be passed to functions. only pointers (for arrays. } void main() { int a[size] = {1. So the arguments int *ptr and int prt[size] have no difference as function arguments. } Answer: some's destructor some's destructor Explanation: Compiled by H.} void print(int ptr[size]) { cout<<ptr[0]. base addresses) can be passed. What will be the output? class some{ public: ~some() { cout<<"some's destructor"<<endl. Srimathi 24 . s.~some(). both the functoins have the same signature andso cannot be overloaded. C++ Interview Questions 8. void main() { some s. 9.2.SRM University For Private circulation only C. In other words. print(a). What will be the output? const int size = 5. int *b = new int(size).5}. } }.4.

Static member functions do not have a this pointer.setMonth( 3 ). pointers do not have such restrictions. struct. The function is not treated as a separate unit like other normal functions. Srimathi 25 .*. Compiled by H. the address of the object is passed as a hidden argument to the function. . . If the inline function is found to have larger chunk of code. But a compiler is free to decide. 10. Inline functions are treated like macro definitions by the C++ compiler. must always be initialized. It points to the object for which the member function is called. but as like other normal functions. therefore. It is legal.->. though unnecessary. They are declared with the keyword inline as follows. 11. Here 's.. Name the operators that cannot be overloaded?? sizeof.~some()' explicitly calls the destructor of 's'. A pointer can be reassigned to point to different objects while a reference always refers to an object with which it was initialized 12. What is "this" pointer? The “this” pointer is a pointer accessible only within the member functions of a class. For example. When a non-static member function is called for an object. myDate. if a function qualifies to be an inline function. the following function call. When main() returns. can be interpreted this way: setMonth( &myDate. . What is the difference between a pointer and a reference? A reference must always refer to some object and. 3 ). ::. What is inline function? When a function is declared inline. hence the result. The object’s address is available from within the member function as the this pointer. it will not be treated as an inline function. C++ Interview Questions Destructors can be called explicitly. the function is expanded at the calling block. to use the this pointer when referring to members of the class.SRM University For Private circulation only C. or union type. ?: 13. 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 freed. 14. destructor of s is called again. What is a dangling pointer? A dangling pointer arises when you use the address of an object after its lifetime is over.

You include the headers for each class library: #include "vendor1. 15. Compiled by H. }.. various stuff . the keyword inline is not necessary.. various stuff .int y). As implied. leading to a compile-time clash.h" #include "vendor2.. it will be automatically treated as inline by the compiler.} In fact... class String { .. and each library has some features that you'd like to use.. //Definition for C++ Tutorial inline sample: iinline int add(int x.. where two libraries contain some identically-named symbols. When such functions are treated inline.. there is the further problem of linktime clashes. Even if you could somehow get around this compile-time problem.h . C++ Interview Questions //Declaration for C++ Tutorial inline sample: int add(int x. each vendor has included a String class in the class library.int y) {return x+y.. If the function is defined with its body directly and the function has a smaller block of code. }. class String { .h" and then it turns out that the headers have this in them: // vendor1. In other words.SRM University For Private circulation only C. inline functions are meant to be used if there is a need to repetitively execute a small block of code. because class String is defined twice.. it might result in a significant performance difference. // vendor2.. which is smaller. This usage will trigger a compiler error. Srimathi 26 . What problem does the namespace feature solve? Suppose that you buy two different general-purpose class libraries from two different vendors.h .

. Srimathi 27 .org coolinterview. } // vendor2. C++ Interview Questions The namespace feature gets around this difficulty by means of separate named namespaces: // vendor1.. 2. but instead there are now classes named Vendor1::String and Vendor2::String. Technical Aptitude Questions by Vasant B Parmar geekinterview. 3.. In future discussions we will see how namespaces can be used in applications. 4...... } There are no longer two classes named String.h .com *Wish you all the Best* Compiled by H.com techinterview. }.SRM University For Private circulation only C. Main References / Source of Document: 1.. namespace Vendor1 { class String { . namespace Vendor2 { class String { .. }. various stuff . various stuff ...h .

Sign up to vote on this title
UsefulNot useful