C++ Interview Questions

C++ Interview Questions and Answers

1. What are the differences between C++ and C?

  • Object-Oriented Programming: C++ supports OOP, including classes and objects, while C is procedural.
  • Classes and Objects: C++ has classes and objects, whereas C does not.
  • Function Overloading: C++ supports function overloading, but C does not.
  • Memory Management: C++ uses constructors, destructors, and RAII for better memory management, while C requires manual memory management using malloc/free.

2. What is a class in C++?

A class in C++ is a blueprint for creating objects. It defines the attributes (data members) and behaviors (member functions) that an object created from the class can have. A class can have constructors, destructors, and member functions to perform operations on the object’s data.

3. What is a pointer in C++?

A pointer is a variable that stores the memory address of another variable. Pointers are powerful in C++ and are used for dynamic memory allocation, function argument passing by reference, and implementing data structures like linked lists.

4. What are constructors and destructors in C++?

  • Constructor: A special member function that is automatically called when an object is created. It initializes objects of a class.
  • Destructor: A special member function that is automatically called when an object goes out of scope. It is used to clean up resources.

5. What is a constructor initializer list in C++?

A constructor initializer list is a list of initializations provided after the constructor’s declaration and before its body. It is used to initialize data members or call the constructor of the base class in an efficient way.

Example:

class MyClass {
    int x;
public:
    MyClass(int val) : x(val) {} // Constructor initializer list
};

6. What is polymorphism in C++?

Polymorphism allows objects of different classes to be treated as objects of a common base class. It is achieved via function overriding (runtime polymorphism) and function overloading (compile-time polymorphism).

  • Runtime Polymorphism: Achieved through virtual functions.
  • Compile-time Polymorphism: Achieved through function overloading and operator overloading.

7. What are virtual functions in C++?

A virtual function is a function in a base class that can be overridden in a derived class. It is declared with the keyword virtual in the base class. It supports runtime polymorphism.

8. What is the difference between shallow copy and deep copy?

  • Shallow Copy: Copies the object’s values (but not pointed-to data). The copied object points to the same memory location as the original object.
  • Deep Copy: Creates a new copy of the object and allocates new memory for dynamically allocated objects, effectively creating a copy independent of the original object.

9. What is the difference between == and = in C++?

  • == is the equality operator, used to compare two values to check if they are equal.
  • = is the assignment operator, used to assign a value to a variable.

10. What is a friend function in C++?

A friend function is a function that is not a member of a class but has access to its private and protected members. It is declared using the friend keyword within the class.

11. Explain the concept of exception handling in C++.

Exception handling in C++ is done using try, catch, and throw blocks:

  • try block contains code that may throw an exception.
  • catch block handles the exception.
  • throw is used to throw an exception.
try {
    // code that may throw an exception
} catch (const std::exception& e) {
    // handle the exception
}

12. What is the difference between `new` and `malloc()` in C++?

  • new: An operator that allocates memory and automatically calls constructors for objects. It is type-safe and throws an exception (std::bad_alloc) if memory allocation fails.
  • malloc(): A C function that allocates raw memory without calling constructors. It returns a void pointer and doesn’t handle type safety or exception handling.

13. What is the purpose of the `static` keyword in C++?

  • Static variable: A variable declared as static retains its value between function calls. It has internal linkage and is initialized only once.
  • Static function: A function declared as static is limited in scope to the file in which it is declared, making it private to the file.

14. What is the difference between `private`, `protected`, and `public` access specifiers?

  • Private: Members are accessible only within the class and its friends.
  • Protected: Members are accessible within the class, its derived classes, and their friends.
  • Public: Members are accessible from anywhere.

15. What are the advantages of using pointers in C++?

  • Dynamic memory allocation: Allows allocation of memory at runtime.
  • Efficiency: Reduces memory overhead and allows efficient memory usage.
  • Data structures: Pointers are necessary for building complex data structures like linked lists, trees, etc.

16. What is the difference between `++i` and `i++`?

  • ++i (pre-increment): Increments the value of `i` before using it in an expression.
  • i++ (post-increment): Uses the value of `i` first, then increments it after the expression.

17. What are the different types of inheritance in C++?

  • Single inheritance: A class inherits from one base class.
  • Multiple inheritance: A class inherits from more than one base class.
  • Multilevel inheritance: A class inherits from a derived class.
  • Hierarchical inheritance: Multiple classes inherit from a single base class.
  • Hybrid inheritance: A combination of two or more types of inheritance.

18. Explain the concept of inheritance in C++.

Inheritance in C++ allows a new class (derived class) to inherit attributes and behaviors from an existing class (base class). This promotes code reusability and allows for creating a hierarchical relationship between classes. C++ supports multiple inheritance, which means a class can inherit from more than one base class.

19. What is RAII (Resource Acquisition Is Initialization) in C++?

RAII is a programming technique where resources (such as memory or file handles) are acquired during object construction and released during object destruction. This ensures proper resource management, especially for memory and file handles.

20. Explain the concept of `const` in C++.

  • Constant data: The const keyword is used to define constant values that cannot be modified.
  • Constant pointers: A pointer that cannot point to another address is declared as const int* ptr.
  • Pointer to constant: A pointer that can point to different addresses but cannot modify the data is declared as int* const ptr.

Conclusion:

Preparing for a C++ interview can be a daunting task, but with practice and a good understanding of C++ concepts, you’ll increase your chances of success. This page has provided you with essential C++ interview questions and their answers to help you get ready for your next interview.