C++ INTERVIEW TIPS Part 1

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 < nloop ="0;nLoop" h="">
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);
}
}

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.
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();
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.

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.

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.

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.

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

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.

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

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.