Points to remember for C and C++ phone interviews

If you are new to technical interviews then you will notice that during phone interviews, conceptual and small questions are asked to gauge the abilities of an individual. Following is the list of some C and C++ questions that can be asked during technical phone interviews. This list shouldn't be referred if your basics of C and C++ are not clear. If you are programming in C or C++ from some time then you might be able to understand most of the points pretty quickly. Hope you find it useful.


  1. On some compilers:
    1. int is 4 bytes
    2. long int 4 bytes
    3. double int 8 bytes
    4. float = 4 bytes
    5. long float = 4 bytes
    6. char = 1 bytes
  2. signed and unsigned will not affect the size. They will affect the value range
  3. Pointer always has same size regardless of where it is pointing.
  4. Example of Enum:
    1. enum color{Black,white};
    2. color c = white;
    3. cout<<c<<endl; //c will have the value 0 or 1 for black or white respectively.
  5. break; breaks the inner loop only not the outer loop.
  6. These all math functions work on: double, float, long double. Since, these functions work on the floating point numbers, we have to use typecasting (int).
    1. fabs: Computes the absolute value of a floating point no.
    2. abs: Computes the absolute for the integers
    3. exp: To compute e^x, where x is a floating point value
    4. sqrt: Finds the sqrt
  7. Max and Min functions are present in the algorithm library.
  8. Memory allocated by malloc, calloc, realloc might not be compatible with the one allocated by new. So manipulation is needed. Hence, malloc and delete & new and free cannot be used interchangeably.
  9. void as a data type is not supported
  10. Dynamic memory is allocated from system heap
  11. If you call break before returning a value then a junk value is returned. 


This pointer basics

    1. this pointer stores the address of the class instance, to enable pointer access of the members to the member functions of the class.
    2. this pointer is not counted for calculating the size of the object.
    3. this pointers are not accessible for static member functions.
    4. this pointers are not modifiable. 
Example:
class A
{
  int i;
  public:
  int get()
  {
    return this->i;
  }

  void set(int k)
  {
    this->i = k;
  }
};


Shallow copy 
Shallow copy will just copy one pointer to another pointer
Ex:
void shallow_copy(test *s,test *t)
{
  s->ptr = t->ptr;
}

Deep Copy
Deep copy will allocate the space for the target then using memcpy to copy the value from the source to the target.
Ex:
void deep_copy(test *s,test *t)
{
   t->ptr = (char*)malloc(sizeof(s) + 1);
   memcpy(t->ptr,s->ptr,strlen(s->ptr)+1);
}


Name hiding
Name hiding occurs when:
There is overloading in the base class and inheritance involved.

Ex:
  base:
  display();
  display(int i);

  derived:
  display();

derived_instance.display(1); //It will not work since it will confuse the compiler


How does delete work
delete p: It will delete the memory for single character
delete []p: It will delete the memory for an array

Different ways to access pointers

  1. *(p+i)
  2. *(i+p)
  3. p[i]
  4. i[p]

Why do we use pointer to a pointer in BST?
  • Because we have the nodes and the traversal in the form of the pointers.
  • To traverse we need a pointer.

No comments:

Post a Comment

NoSQL

This one is reviewed but I need to delete its copy from hubpages or somewhere NoSQL Data models: key-value  Aggregate model.  key or i...