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

0 comments:

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP