50 C++ Interview Questions

I have compiled a list of repeatedly asking C++ interview questions here. Some of them are very important C++ questions asked by employers like Microsoft, Adobe and IBM. This list has been updated to include few more questions.

  1. How do you check whether a linked list is circular?
  2. How do you decide which integer type to use?
  3. How do you differentiate between a constructor and destructor?
  4. How do you differentiate between aggregation and association?
  5. How do you find out the size of a class?
  6. How do you implement an itoa function?
  7. How do you initialize a pointer to a function?
  8. How do you link a C++ program to C functions?
  9. How do you return a structure from functions?
  10. How do you write a function that can reverse a linked-list?
  11. What are the benefits of using exceptions in C++?
  12. What are the differences between a struct and a class in C++?
  13. What are the different types of Storage classes?
  14. What do you mean by inline function?
  15. What does extern mean in a function declaration?
  16. What does extern mean in a function declaration?
  17. What does it mean to declare a function or variable as static?
  18. What is a conversion constructor?
  19. What is a copy constructor?
  20. What is a namespace?
  21. What is a pure virtual function?
  22. What is a scope resolution operator?
  23. What is abstraction?
  24. What is difference between #define and const?
  25. What is difference between function overloading and overriding?
  26. What is encapsulation?
  27. What is function overloading?
  28. What is multiple inheritance?
  29. What is operator overloading?
  30. What is partial specialization or template specialization?
  31. What is polymorphism?
  32. What is the difference between “passing by value” and “passing by reference”?
  33. What is the difference between an object and a class?
  34. What is the difference between declaration and definition?
  35. What is the difference between declaration and definition?
  36. What is the difference between delete and delete[]?
  37. What is the difference between inner class and abstract class?
  38. What is the difference between persistent & non-persistent objects?
  39. What is the difference between structure and union?
  40. What is the difference between the deep copy and shallow copy?
  41. What is the difference between the private public and protected members?
  42. What is the maximum size that an array can hold?
  43. What is the most efficient way to reverse a linked-list?
  44. What is the size of an empty class?
  45. What is the use of virtual destructor?
  46. What is virtual class and friend class?
  47. What’s the best way to declare and define global variables?
  48. Where is memory for class-object allocated?
  49. Where is memory for struct allocated?
  50. Why an array always starts with index zero in C++?
  51. What is the block scope variable in C++?
  52. What is a class template?
  53. Does an abstract class in C++ need to hold all pure virtual functions?
  54. What is the order of objects destroyed in the memory?

References:

http://www.allinterview.com

http://www.coolinterview.com/

http://www.techinterviews.com/

3 Comments

  1. Answers.

    How do you check whether a linked list is circular?
    Traverse the list with two pointers, one moving ahead one node per iteration, the other moving ahead two.
    If the faster pointer reaches the end, return not circular.
    If the faster pointer reaches the slower pointer or one behind the slower pointer, return circular.
    Otherwise continue incrementing the pointers.

    How do you decide which integer type to use?
    Depends on the problem being solved. Need to have enough granularity to represent the problem space, but not so much
    that memory is wasted.

    How do you differentiate between a constructor and destructor?
    A constructor creates and initializes a new object. The destructor is called to destroy an object that is no longer needed.

    How do you differentiate between aggregation and association?
    Aggregation means containment. Aggregated objects are inside the aggregator. Association is a relationship to an external object.

    How do you find out the size of a class?
    Use sizeof(class) or sizeof(object).

    How do you implement an itoa function?
    Start with the integer, perform % 10. Add “0” to result, produces the ones digit.
    Next, divide by 10 and discard remainder. Again, % 10, add “0”, produces tens digit.
    Repeat until only a single digit left, complete the string of digits.

    How do you initialize a pointer to a function?
    For a function “void f(void)”, a pointer to this function would be “void (*f)() pf = f;”.

    How do you link a C++ program to C functions?
    Use ‘extern “C”‘ in the declaration.

    How do you return a structure from functions?
    Set the struct value in the function, then return by value. Typically structs are small, so copy overhead is not bad.
    If the struct is large, perhaps it should be a class, in which case you can use other semantics to avoid costly copying.

    How do you write a function that can reverse a linked-list?
    Iterate through the nodes from head to tail. At each stage, the head moves to the next node, a new pointer points to the one beyond that, and the current node points
    back to the previous.

    What are the benefits of using exceptions in C++?
    The try-catch blocks allow all the exception handling code to be separated from the normal successful-case code.

    What are the differences between a struct and a class in C++?
    Struct members are public by default. Class members are private.

    What are the different types of Storage classes?
    Auto, register, static, extern.

    What do you mean by inline function?
    A hint to the compiler to replace calls to the function with a copy of the function body. Improves performance but could cause code bloat. Compiler may choose to ignore.

    What does extern mean in a function declaration?
    The function is defined in a different source file.

    What does it mean to declare a function or variable as static?
    A static variables inside a function retain their value across calls
    Static members of a class have only one instance per class, not one per object. There does not need to be any objects instantiated; the static members exist at startup.
    Static functions of a class modify only static members.

    What is a conversion constructor?
    A constructor that takes one argument of a different type, that the compiler can use to perform implicit conversions from the parameter type to the class type.

    What is a copy constructor?
    A constructor that takes one argument of the same type being constructed. Should result in a copy of the passed-in object.

    What is a namespace?
    A mechanism to partition a set of related symbol names, and gives the set a unique name of its own.

    What is a pure virtual function?
    A pure virtual function must be overridden in a subclass. Cannot create an instance of a base class that has a pure virtual function.

    What is a scope resolution operator?
    “::”, Used to identify the context for an identifier. I.e. an identifiers declared within a class or namespace.

    What is abstraction?
    The ability to create and use objects without knowing any of the implementation details.

    What is difference between #define and const?
    #define is a preprocessor directive, purely a text replacement. const is relevant at compile time; the compiler can enforce type safety.

    What is difference between function overloading and overriding?
    Overloaded functions have the same name but differ in terms of parameters.
    Overriding refers to a non-pure virtual function in a base class, but a subclass has provided a different implementation of the virtual function.

    What is encapsulation?
    An object exposes only a public interface for callers. Implementation details are hidden as private data members or functions.

    What is function overloading?
    Multiple functions with the same name but different sets of parameters.

    What is multiple inheritance?
    A class can inherit from more that one base class.

    What is operator overloading?
    A class can define functions that allow the caller to use instances with arithmetic operators. I.e. objA = objB + objC.

    What is partial specialization or template specialization?
    This means providing a customized version of a function template or class template for a specific template type parameter.

    What is polymorphism?
    Instances of objects of a class or any of its subclasses can be used interchangeably.

    What is the difference between “passing by value” and “passing by reference”?
    By value, within the function being called, the parameter object is a copy of the object originally passed in.
    By reference, the parameter object is the same object originally passed in.

    What is the difference between an object and a class?
    A class is a definition that describes the content and structure of an object.
    Classes are types, and objects are instances of those types.

    What is the difference between declaration and definition?
    Declaration indicates the existence of a function or class. Definition provides the content and structure of the function or class.

    What is the difference between delete and delete[]?
    Use delete for single instances, and delete[] for primitive arrays of instances.

    What is the difference between inner class and abstract class?
    An inner class is scoped inside an outer class. The outer class can create instances of the inner class.
    An abstract class is not necessarily scoped within another class. Abstract classes cannot be instantiated, they must be subclassed.

    What is the difference between persistent & non-persistent objects?
    Persisent objects can be stored in persistent storage for later retrieval after the system has been stopped and restarted.
    Non-persistent objects are not stored in persistent storage.

    What is the difference between structure and union?
    Elements of a structure are laid out sequentially in memory.
    Union elements are laid out in parallel, i.e. occupy the same memory locations simultaneously.

    What is the difference between the deep copy and shallow copy?
    With shallow copy, copied pointers have the same address so they point to the same object.
    Deep copy will copy the objects that the pointers are pointint to, often recursively.

    What is the difference between the private public and protected members?
    Private members are visible only to methods of the class itself.
    Protected members are visible also to all subclasses.
    Public members are visible to all clients.

    What is the maximum size that an array can hold?
    Limited only by the memory available on the system.

    What is the size of an empty class?
    At minimum, 1. Even if there are no members, C++ standard requires objects to be addressable, thus must take up non-zero bytes.
    Therefore, at minimum, 1 byte.

    What is the use of virtual destructor?
    Required for any class that has virtual functions and subclasses.
    Ensures that a destructor called through a pointer to the base class will actually invoke the destructor of the derived class.

    What is virtual class and friend class?
    Base classes can be marked virtual to prevent multiple occurrences of the base class in a diamond-shaped inheritence hierarcy when using multiple inheritance.
    A class can indicate another class to be a friend to allow the friend class to have access to the private members of the original class.

    What’s the best way to declare and define global variables?
    Define variable outside of any namespace. Where needed, declare the variable using extern,
    or alternatively, declare the extern in a header file and include the header where needed.

    Where is memory for class-object allocated?
    Objects (of a class) are allocated on the stack for simple object declarations, or in the heap when using the new operator.

    Where is memory for struct allocated?
    Structs are also allocated on the stack for simple object declarations, or in the heap when using the new operator.

    *Note that the memory layout of a struct and an equivalent class are identical, provided that the class has no user-defined constructor, destructor, virtual functions,
    assignment operator, or base classes.

    Why an array always starts with index zero in C++?
    Similar to C, array indexing is analagous to pointer arithmetic.
    First instance in an array is addressable by a pointer to the start of the array.

Leave a Reply