Here are some more commonly asked C++ interview questions along with their answers:

11. What are virtual functions in C++?

  • Virtual functions are functions declared in the base class with the virtual keyword. They allow dynamic binding or late binding during runtime. When a derived class overrides a virtual function, the function called depends on the actual object’s type, not the reference or pointer’s type.

12. What is the difference between function overriding and function overloading?

  • Function overriding occurs when a derived class provides a specific implementation of a virtual function declared in the base class. The function signatures (return type, name, and parameters) must be identical.
  • Function overloading, as discussed earlier, involves providing multiple functions with the same name but different parameters in the same class.

13. What are inline functions in C++?

  • Inline functions are functions whose definition is placed directly into the calling code during compilation, rather than making a separate function call. This is done using the inline keyword. Inline functions reduce the overhead of function calls and can lead to improved performance for small, frequently used functions.

14. What is a reference in C++?

  • A reference is an alias or alternative name for an existing object. Once a reference is bound to an object, it acts as a pointer to that object, allowing you to manipulate the object indirectly through the reference.

15. Explain the const keyword in C++ and its usage.

  • The const keyword is used to make objects, function parameters, and function return types immutable (read-only).
  • A const object cannot be modified after initialization, and const member functions are not allowed to modify the object’s state.
  • const can also be used with pointers and references to enforce immutability.

16. What is the static keyword in C++?

  • The static keyword has different meanings depending on its context:
    • In the context of a class member, static means the member belongs to the class itself, not to instances of the class. It can be accessed using the class name.
    • For local variables in a function, static makes the variable retain its value between different invocations of the function.

17. Explain multiple inheritance in C++.

  • Multiple inheritance allows a class to inherit features from more than one base class. This means a derived class can have multiple immediate parent classes. C++ supports multiple inheritance, but it can lead to the “diamond problem” if not handled properly.

18. What are the differences between delete and delete[] operators?

  • delete is used to deallocate memory for a single object created with new.
  • delete[] is used to deallocate memory for arrays created with new[].
  • Using delete for an array or delete[] for a single object can lead to undefined behavior.

19. Explain the C++ Standard Template Library (STL).

  • The C++ STL is a collection of template classes and functions that provide common data structures (like vectors, lists, maps) and algorithms (like sorting, searching) to efficiently handle data. It simplifies code and promotes code reuse.

20. How is exception handling done in C++?

  • C++ supports exception handling using try, catch, and throw. Code that might raise an exception is placed inside the try block, and exceptions are caught and handled in the catch block.