C++ Interview Questions Part 2

C++ interview question :what is the difference betwen wait() and delay()?
Answer :Wait() and delay() works same but works on different platforms. Wait(2) will wait processing fro 2 second on Linux/Unix while delay(2000) with wait for 2 second but on DOS or Windows. so wait(2) on linux == delay(2000) on DOS Delay() is under while one can directly use wait in his/her program. 

C++ interview question :Why always array starts with index 0
Answer :Array name is a constant pointer pointing to the base address(address of the first byte where the array begin) of the memory allocated. When you use arr[i], the compiler manipulates it as *(arr + i). Since arr is the address of the first element, the value of i must be 0 for accessing it. Hence all arrays begin with an index of 0. 

C++ interview question: Can main() be overridden 
Answer : In any application, there can be only one main function. In c++, main is not a member of any class. There is no chance of overriding 

C++ interview question :What is the difference between macro and inline()? 
Answer :
1. Inline follows strict parameter type checking, macros do not.
2. Macros are always expanded by preprocessor, whereas compiler may or may not replace the inline definitions. 

C++ interview question: How can double dimensional arrays be dynamically initialized in C++?
Answer :example of how to dynamically initialize double dimensional arrays:
int num[2][3] = {34,32,30,24,22,20};
num[1][1] = 34
num[1][2] = 32
num[1][3] = 30
num[2][1] = 24
num[2][2] = 22
num[2][3] = 20 

C++ interview question: Can destructor be private?
Answer: Yes destructors can be private. But according to Standard Programming practice it is not advisable to have destructors to be private.

Read more...

C++ Interview Questions Part 1

C++ interview question: What is a memory leak? How can we avoid it?
Answer :A memory leak can be avoided by making sure that whatever memory has been dynamically allocated will be cleared after the use of the same. for example 
int main() 
{ char *myCharData[20]; 
for (int nLoop =0;nLoop < 20; ++nLoop) { myCharData[nLoop ] = new char[256]; 
strcpy(myCharData[nLoop],"SABITH"); 
...... 
........................ 
/*Some manipulations here using myCharData*/ 
/*Now here we have to clear the data. The place can vary according to ur program. This being a simple program,u can clear at the end*/ 
for(int nLoop =0;nLoop < 20; ++nLoop) 
delete[] myCharData[nLoop ]; 
return 0;

C++ interview question: How do you write a program which produces its own source code as its output?
Answer :write this program and save it as pro.c....run....it will show the program to the consol and write the source code into data.txt file...................
#include
void main()
{
FILE *fp,*ft;
char buff[100];
fp=fopen("pro.c","r");
if(fp==NULL)
{
printf("ERROR");
}
ft=fopen("data.txt","w+");
if(ft==NULL)
{
printf("ERROR");
}
while(getc(fp)!=EOF)
{
fscanf(fp,"%s",buff);
printf("%s\n",buff);
fprintf(ft,"%s\n",buff);
}
}

C++ interview question: What are the things contains in .obj file ? ( compiled result of .cpp file )
Answer :C++ .obj file holds code and data suitable for linking with other object files to create an executable or a shared object file.

C++ interview question :What is difference between followin intialization.
int iVar1;
int iVar2 = int();
and which one of two should we prefer always and why?
Answer :In first case a variable will be create in memeory with the default base type value (depending upon compiler 2 compiler) bcoz it is not initialized. in second case the variable will be created in the memory with the value retuned by the function int() (if int is a user define function) the second statement should have been int *i = new int();

C++ interview question :What is the difference between Object and Instance?
Answer :An instance of a user-defined type (i.e., a class) is called an object. We can instantiate many objects from one class.An object is an instance or occurrence of a class.

C++ interview question: How is static variable stored in the memory?
(if there are 2 functions in a file, and the static variable name is same (ex var) in both the function. how is it keep separately in the memory).
Answer: C++ uses name mangling when storing both local and global static varibales at the same place. The local static variables have function name and the global variables will have file name. Essentially the compiler uses namespace to distinguish between local and global static variables.

Tips to bloggers :keyword density is more important in search engine

Read more...

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP