10 Important C++ Interview Questions with Solutions

c++
, ,

Preparing for a C++ interview can be challenging, especially with its vast and intricate nature. Here are ten crucial C++ interview questions along with detailed solutions:

  1. 1 . What is the difference between delete and delete[] in C++?
  • delete is used to deallocate memory for a single object created with new.
  • delete[] is used to deallocate memory for an array of objects created with new[].
   // Example
   int* single = new int;
   delete single;

   int* arr = new int[5];
   delete[] arr;
  1. 2. What is a virtual function?
  • A virtual function is a member function in a base class that is declared with the virtual keyword and is meant to be overridden in derived classes.
  • It allows the function to be dynamically bound at runtime based on the object’s actual type.
   // Example
   class Base {
   public:
       virtual void foo() {
           cout << "Base foo" << endl;
       }
   };

   class Derived : public Base {
   public:
       void foo() override {
           cout << "Derived foo" << endl;
       }
   };
  1. 3.Explain the difference between struct and class in C++.
  • In C++, struct and class are almost identical, with the only difference being the default access level.
  • Members of a struct are public by default, while members of a class are private by default.
   // Example
   struct Point {
       int x, y;  // public by default
   };

   class Circle {
       int radius;  // private by default
   public:
       void setRadius(int r) { radius = r; }
       int getRadius() { return radius; }
   };
  1. 4. What is the difference between std::map and std::unordered_map?
  • std::map is a sorted associative container that contains key-value pairs, where keys are unique and stored in sorted order based on the key.
  • std::unordered_map is an unordered associative container that contains key-value pairs, where keys are unique and stored in an unordered manner based on a hash function.
   // Example
   std::map<int, string> myMap;
   std::unordered_map<int, string> myUnorderedMap;
  1. 5. What is RAII (Resource Acquisition Is Initialization)?
  • RAII is a programming idiom in C++ where the resource allocation is tied to object lifetime.
  • Resources like memory, file handles, sockets, etc., are acquired in the constructor and released in the destructor of a class.
   // Example
   class File {
       FILE* file;
   public:
       File(const char* filename) {
           file = fopen(filename, "r");
       }

       ~File() {
           if (file) fclose(file);
       }
   };
  1. 6. What is the difference between reinterpret_cast, static_cast, dynamic_cast, and const_cast?
  • reinterpret_cast is used for type casting that changes the pointer type to an unrelated type.
  • static_cast is used for regular type conversions that do not involve polymorphic types.
  • dynamic_cast is used for safe downcasting in polymorphic types.
  • const_cast is used to add or remove const/volatile qualifiers from a variable.
   // Example
   void* ptr = reinterpret_cast<void*>(42);
   int i = static_cast<int>(3.14);
   Base* basePtr = dynamic_cast<Base*>(derivedPtr);
   const_cast<int&>(constVar) = 10;
  1. 7. Explain the difference between new and malloc in C++.
  • new is an operator in C++ used to allocate memory for a single object or an array of objects and calls the object’s constructor.
  • malloc is a function in C used to allocate a block of memory of a specified size and returns a void pointer.
   // Example using new
   int* single = new int;
   int* arr = new int[5];

   // Example using malloc (not recommended in C++)
   int* single = (int*)malloc(sizeof(int));
   int* arr = (int*)malloc(5 * sizeof(int));
  1. 8. What is the constexpr keyword in C++?
  • constexpr is a keyword in C++ used to indicate that a variable or function can be evaluated at compile time.
  • It allows the compiler to perform optimizations and enforce constant expressions.
   // Example
   constexpr int square(int x) {
       return x * x;
   }

   int main() {
       constexpr int result = square(5);  // evaluated at compile time
       return 0;
   }
  1. 9. What is the difference between override and final in C++?
  • override is used to explicitly declare that a function in a derived class overrides a virtual function in the base class.
  • final is used to indicate that a virtual function cannot be overridden in any further derived classes.
   // Example
   class Base {
   public:
       virtual void foo() {}
   };

   class Derived : public Base {
   public:
       void foo() override final {}
   };

   // Error: Cannot override 'foo' as it is marked 'final' in 'Derived'
   // class FurtherDerived : public Derived {
   // public:
   //     void foo() override {}
   // };
  1. 10 . Explain the concept of smart pointers in C++.
    • Smart pointers are objects that behave like pointers but provide additional features such as automatic memory management (like garbage collection).
    • std::unique_ptr is used for exclusive ownership, std::shared_ptr for shared ownership, and std::weak_ptr to observe an object without owning it.
// Example
std::shared_ptr<int> ptr1 = std::make_shared<int>(5);
std::shared_ptr<int> ptr2 = ptr1;

These questions cover a broad range of topics in C++ and can help you prepare for interviews effectively. Make sure to understand the concepts thoroughly and practice writing code to solidify your understanding.

Leave a Reply

Your email address will not be published. Required fields are marked *