Wednesday, February 18, 2009

C/C++ Common interview questions

Tech Interview C++

I got 2 years of experience and looking for a new job these days. I thought to write down the most commonly asked technical questions, they asked me.
Questions
  1. What is the difference between new/delete and malloc(), free()?
  2. What if we allocate memory using new and release it with free()?
  3. Why do we need virtual destructors?
  4. C++ compiler provides a copy costructor and overloaded assignment operator by default, why do we need to write our own copy constructors?
  5. Have you ever used STL, why do we use STL, if yes then what containers?
  6. What are function pointers?
  7. What happens when a callback funtion is called?
  8. Differences between C and C++?
  9. Write a program in C language which prints "Hello World!" on screen, without using even a single semi colon ';'.
  10. Swap two integers without using a third one.
  11. If we have a struct struct ABC{int n; char c;}; Suppose an integer takes 4 bytes. How many space in memory will be occupied by an instance of this structure?
  12. Why do we need pure virtual functions?
  13. What are friend functions?


Answers









  1. malloc() and free() are C language functions used to allocate and free memory. These funtions simply allocate memory for a variable. new/delete are C++ language operators, new operator allocates memory needed by an object and then calls the constructor function of that object to perform any initializations. Applying delete to an object causes a call to the destructor function of that object, and afterwards a release of memory. 
  2. The memory occupied by the object will be returned to Operating System, but the destructor of that object won't be called.
  3. Suppose you use delete with a base class pointer to a derived class object to destroy the derived-class object. If the base-class destructor is not virtual, then delete, like a normal member function, calls the destructor for the base class, not the destructor for the derived class. This will cause only the base part of the object to be destroyed.
  4. C++ default copy constructor and assignment operator perform a member to member copy of the instance variables contained by an object. If we have instance variables of pointer types in an object say object1, and we perform a straight member to member copy to object2. The pointer type instance variables in both object1 and object2 will be pointing to same memory locations. And if we delete one object, the pointers in other object will become invalid without that object ever knowing. This will cause a program crash.
  5. Standard Template Library is a standard peer revised and optimazed collection of containers and algorithms. Because it is standard, it is cross platform and any compiler supporting C++ standards must work with STL. Using STL cuts developement time, and increases program reliablity. I have personally  used vector, queue, and map containers.
  6. Callback funtions are also known as delegates. Callbacks are used when we want a method to be called from some other thread of execution. For example the boost::regex_grep function, its parameters are a large(or maybe small) string, a regular expression, and a call back funtion. The string can be very very long and finding the matches may take too much time, so whenever this boost::regex_grep funtion finds a match satisfying the supplied regular expression it will call the funtion in our code specified in callback argument.
  7. A callback funtion is basically a pointer to the starting location of some code, AKA funtion pointer. When a callback funciton is called, its parameters are pushed to stack. Then the control is transferred to the funtion pointer location and then the control resumes from the function pointer location.
  8. C is a functions based language, and it does not provide anything to group data with funtions which will manipulate that data. C++ is an Object Oriented language, and it provides rich constructs to group data with funtions which will manipulate it (AKA Data Encapsulation). It is possible to implement solutions close to real world using C++, by implementing heirarchical designs. C++ saves developement time by code resue through Inheritance as well.
  9. void main(void){if(printf("Hello World!")){}}
  10. int a = 10; int b = 20; a = a+b; b = a - b; a = a - b;
  11. An instance of this struct will take 8 bytes, because the C++ language compiler performs memory padding at every 4 bytes.It means, that a variable occupies at least 4 bytes in memory. 4 bytes is the basic unit of allocation in memory. This struct will take 4 + 1 = 5 bytes in memory, but the compiler will scale it up to 8 bytes by memory padding.
  12. If a class includes a pure virtual function, it can't be instantiated. A class which contains a pure virtual function is an abstract class. Abstract classes are also known as interfaces. In situations where we are defining interfaces, which will be used to contain pointers to child class objects only there must be some way to stop people from creating objects of these interfaces. Pure virtual functions server the purpose. Because an it compiler will stop us from creating an object of an Abstract class.
  13. Friend funtions are funtions which are not the members of a class, but can access the private members of an instance of that class.
Bonus:

A few unanswered questions as well, go figure!
1. Write a function such that if we input it 8, it returns 3 and if we input it 3 it returns 8. Don't use conditional statements.

2. Reverse a singly linked list in O(n) time and O(C) space complexity.

3. Find the third largest integer from an array, time constraints are O(n) and space complexity must be O(c).

4. INTERVIEW QUESTION: Malloc: Write an aligned malloc and free function that supports allocating memory such that the memory address returned is divisible by a specific power of two. CURRENT ANSWER: attached QUESTION FROM READER: I believe line 8 is wrong. It is assigning p2 equal to a (void **) and returning p2, but the function returns void *. I believe the corrected line should be p2[0] = (void * )(((size_t)(p1) + offset) & ~(alignment - 1)); line 10 should be return p2[0]; or could be return *p2;

I'm working as a freelance programmer through ODesk.com, I have a strong profile over there. Feel free to ping me if you need an Android app.

https://www.odesk.com/users/~012d73aa92fad47188



6 comments:

  1. Hi

    Tks very much for post:

    I like it and hope that you continue posting.

    Let me show other source that may be good for community.

    Source: Common interview questions

    Best rgs
    David

    ReplyDelete
  2. Hi,
    Thanks For Providing Interview Questions

    Regards
    L.Sabareesh

    ReplyDelete
  3. "1. Write a function such that if we input it 8, it returns 3 and if we input it 3 it returns 8. Don't use conditional statements."

    Assuming any other input the output doesn't matter....
    8 = 1000b
    3 = 0011b
    B = 1011b

    int func(const int &input)
    {
    return input^0xB;
    }

    ReplyDelete
  4. "1. Write a function such that if we input it 8, it returns 3 and if we input it 3 it returns 8. Don't use conditional statements."

    Assuming any other input the output doesn't matter....
    8 = 1000b
    3 = 0011b
    B = 1011b

    int func(const int &input)
    {
    return input^0xB;
    }

    ReplyDelete

Feel free to talk back...