Pages

Friday, November 13, 2009

C++ questions: Part 1






Here is my first blog in C++, hope you guys are going to enjoy this one as well.
C++ is more structured than C does in a way to build large scale application programming. It's growing and so am I. C++ is well written and is mucj complex than C is.
So, here we go.



   


C++ Q1: 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 <>
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 <>
{
delete[] myCharData[nLoop ];
}
return 0;

C++ Q2: 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++ Q3: 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++ Q4: 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++ Q5: 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++ Q6: 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.

C++ Q7: 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++ Q8: 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++ Q9: 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++ Q10: 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++ Q11:
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++ Q12: 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.


C++ Q13: What is memory leaking in c++ ? 
Answer: When a class uses dynamically allocated memory internally, all sorts of problems arise. If not properly used or handled, they can lead to memory leaks & corrupts Data Structures.


A memory leak is the situation that occurs when dynamically allocated memory is lost to the program.
Char * p;
p= new char[10000];
...
p= new char[5000];
Initially, 10000 bytes are dynamically allocated & the the address of those bytes is stored in p. later 5000 bytes are dynamically allocated & the address is stored in p. However, the original 10000 bytes’ve not been returned to the system using delete [] operator.


Memory leak actually depends on the nature of the program.

Question:

class A()
{
};
int main()
{
A a;
}
Whether there will be a default contructor provided by the compiler in above case ?
Answer :yes, if the designer of the class donot define any constructor in the class. then the compiler provides the default constructor in the class.




No comments: