Select chapter



constructors (2.papers)
inheritance (1.papers)
overriding (1.papers)
basics (4.papers)
pointers (2.papers)
Hide

Sponsored Links


  

c++ interview questions and answers

1
What is this pointer?
  1. a pointer to the current object which has called a member method.
  2. a pointer to the current class which has called a method.
  3. it is a pointer, not necessarily that it should be the current object which is calling some functions.
  4. none
Show Answer
Share Facebook gmail Twitter
2
what is new operator in c++? 
  1. It is used to allocate memory, create object, and initialize object by calling constructor of that class (if it is an object).
  2. it is only used to allocate memory. It won't call constructor.
  3. it is used to allocate memory, and initialize object by calling constructor of that class (if it is an object).
  4. none
Show Answer
Share Facebook gmail Twitter
3
What is the difference between malloc and calloc?
  1. both are same.
  2. malloc & calloc allocates memory, but calloc initializes that memory block with zeros, where as malloc won't.
  3. both are completely different.
  4. none
Show Answer
Share Facebook gmail Twitter
4
Can I use free operator in place of delete?
  1. no it’s a compile time error.
  2. yes, why not.
  3. no, you can't predict the behavior.
  4. No, free will not call destructor if it is an object.
Show Answer
Share Facebook gmail Twitter
5
What is the difference between delete and delete[] 
  1. delete is used when you allocate memory with new. Delete[] is used when you allocate memory with new[].
  2. generally delete[] is used when you want to release the memory of objects or elements allocated with new[] operator.
  3. both option 1 & 2 are true
  4. none
Show Answer
Share Facebook gmail Twitter
6
how free() function know how many bytes to release or free?
  1. it don't actually.
  2. o.s will maintain that list.
  3. generally malloc will add a header at the starting location, on how many bytes its going to allocate. Based on this free will work.
  4. we don't know how it works.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
9
In the below program what is m?
int **m;
int main()
{
    m = new int*[4];
    
    for(int i=0; i<4; i++){
        m[i] = new int[3];
    }
    
    for(int i=0 ;i<4; i++){
        for(int j=0; j<3; j++){
            m[i][j] = i+j;
        }
    }
    
}
  1. m is a pointer to an array which contains 4 integers
  2. m is a pointer to an array which contains 4 integer pointers
  3. m is an array of arrays.
  4. m is a matrix which contains integer pointers.
Show Answer
Share Facebook gmail Twitter
10
Can I use realloc() on a pointer which was allocated using new operator? Is it legal?
  1. no, its illegal to do so. You can't predict the behavior.
  2. yes, its perfectly legal.
  3. No, though its legal.
  4. yes, though it is illegal.
Show Answer
Share Facebook gmail Twitter
11
Should I check if a pointer is null, before deleting it by delete p; ?
  1. yes, it is standard way to check if that pointer is already null before deleting it.
  2. no, it is wrong to check for null. Delete operator will automatically take care of it.
  3. we can't say
  4. none
Show Answer
Share Facebook gmail Twitter
12
Which of the below statements are true about a delete operator when used on an object of a class which has destructor?
  1. first it releases the memory held by that object, and then calls destructor.
  2. first it calls destructor for that class, and then frees the memory allocated to that object.
  3. delete operator will only release the memory, and won't call destructor.
  4. delete operator will call only destructor, and won't release the memory held by that object.
Show Answer
Share Facebook gmail Twitter
13
In below program how d you release memory allocated to pointer p?
int *p = new int[10];
  1. delete p;
  2. delete p[10];
  3. delete []p;
  4. can use either option 1 or 3. both are same.
Show Answer
Share Facebook gmail Twitter
14
What is a dangling pointer concept?
  1. holding the address of an object, whose life time is already over.
  2. you have dynamically allocated some heap, and you are trying to free it more than once.
  3. you have dynamically allocated some heap, but you don't have any pointer reference to it to free.
  4. a pointer which is assigned with null.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
16
What will happen if I create an object using new and free it using free instead of delete?
  1. it gives compile time error.
  2. we can't predict
  3. it leads to memory leaks.
  4. none
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
18
Can I use this pointer in my constructor?
  1. yes always, it works perfectly fine for all the desired things you expect.
  2. no, it’s a compile time error.
  3. you can use, but should be careful. Virtual functions won't work the way you want.
  4. no you can't, though it won't give any compile time error.
Show Answer
Share Facebook gmail Twitter
19
Can I use malloc in place of new while creating object for my class in c++?
  1. yes, as both are same.
  2. no, because malloc don't know to call constructors.
  3. no, because it gives compile time error.
  4. none
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Read more papers :