Wednesday, February 18, 2009

C/C++ Common interview questions

Tech Interview C++

I got 2 years of experience(on the time of writing this blog post a decade ago, time flies!) and I am looking for a new programming job these days. I thought to write down the most commonly asked technical questions which are asked during a C++ job interview.

C++ Job Interview Questions
  1. What is the difference between new/delete and malloc(), free() method calls?
  2. What happens if we allocate memory using new and release it with free() in your C++ code?
  3. Why do we need virtual destructors in C++?
  4. C++ compiler provides a copy constructor and overloaded assignment operator by default, why do we need to write our own copy constructors?
  5. Have you ever used STL(Standard Template Library), why do we use STL, if yes then what containers?
  6. What are function pointers in C++?
  7. What happens when a callback function is called or invoded in your C++ code?
  8. What are the Differences between C and C++ programming language?
  9. Write a program in C language which prints "Hello World!" on screen, but twist is you must do it without using even a single semi colon ';'.
  10. How to Swap two int variables without using a third integer.
  11. If we have a struct defined as following struct ABC{int n; char c;}; Suppose an int takes 4 bytes of memory space. 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 to C++ Interview Questions









  1. malloc() and free() are C language functions used to allocate and free memory. These functions simply allocate memory for a variable. The 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 initialization. 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 optimized 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 development time, and increases program reliability. I have personally  used the std::vector, std::queue, and std::map containers.
  6. Callback functions are also known as delegates in C# world. 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 function. The string can be very very long and finding the matches may take too much time, so whenever this boost::regex_grep function finds a match satisfying the supplied regular expression it will call the function in our code specified in callback argument.
  7. A callback funtion is basically a pointer to the starting location of some code, AKA function pointer. When a callback funciton is called, its parameters are pushed to stack. Then the control is transferred to the function 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 functions which will manipulate that data. C++ is an Object Oriented language, and it provides rich constructs to group data with functions which will manipulate it (AKA Data Encapsulation). It is possible to implement solutions close to real world using C++, by implementing hierarchical designs. C++ saves development time by code reuse 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;

7 comments:

  1. Hi,
    Thanks For Providing Interview Questions

    Regards
    L.Sabareesh

    ReplyDelete
  2. "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
  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. Nice info. Thanks for sharing valuable info about about c++ Faqs. I want to learn c training this tutorial is really helpful. Thanks a lot. Keep sharing on updating tutorials……

    ReplyDelete
  5. Hello There,


    I learnt so much in such little time about A Programmer's Day. Even a toddler could become smart reading of your amazing articles.

    Here's an unusual but interesting job - write a program, let it run on its own for 3 months and earn $40k, 25k or 15k. It's a competition for programmers – Automated Trading Championship 2012.

    How to apply? Write a program (called a trading robot) and send it to the championship. Then it runs for three months, and if it's among the most profitable robots you earn real money. It's challenging, since you can't modify your program during the competition.
    Write a program in MQL5 - it's a specialized language. Haven't heard of it? Don't worry - it is a C like language with OOP, its syntax is very similar to C++. It comes with a special editor, debugger, tester and execution module. Also full documentation is available. It's a little harder with the program logic, but with tons of information available you can handle it.
    I am so grateful for your blog. Really looking forward to read more.


    Grazie,
    Reethu

    ReplyDelete

Feel free to talk back...