InterviewQs

C++ Programming Interview Questions

Top 50 Questions & Answers

Top 50 C++ Interview Questions

1. What is the difference between `struct` and `class` in C++?

In C++, the primary difference is that `struct` members are public by default, while `class` members are private by default.

2. What is a virtual destructor in C++?

A virtual destructor ensures that when an object is deleted through a pointer to a base class, the destructor of the derived class is called, preventing resource leaks.

3. What is the `static` keyword in C++?

The `static` keyword is used to retain the value of a variable across function calls or to restrict the visibility of a variable or function to the file scope.

4. What is a copy constructor in C++?

A copy constructor is a special constructor that creates a new object as a copy of an existing object.

5. What is a move constructor in C++?

A move constructor transfers the resources of a temporary object to a new object, improving efficiency by avoiding unnecessary copying.

6. What is the difference between `std::vector` and `std::array` in C++?

`std::vector` is a dynamic array that can grow or shrink in size, while `std::array` is a fixed-size container.

7. What is the purpose of `nullptr` in C++?

`nullptr` is a constant pointer literal representing a null pointer, providing type safety as opposed to the `NULL` constant.

8. What is the use of `new` and `delete` operators in C++?

`new` is used for dynamic memory allocation, and `delete` is used to free memory allocated by `new`.

9. What is the difference between `new` and `new[]` in C++?

`new` allocates memory for a single object, while `new[]` allocates memory for an array of objects.

10. What is the difference between `delete` and `delete[]` in C++?

`delete` is used to deallocate memory allocated for a single object, while `delete[]` is used for an array of objects.

11. What is an inline function in C++?

An inline function is a function whose definition is substituted directly into the code at the point of call, reducing the overhead of function calls.

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

Pointers allow for dynamic memory allocation, pass-by-reference, and efficient handling of large amounts of data.

13. What is a smart pointer in C++?

A smart pointer is an object that acts as a pointer but automatically manages the memory of the object it points to, ensuring no memory leaks.

14. What is a `shared_ptr` in C++?

A `shared_ptr` is a smart pointer that allows multiple pointers to share ownership of a single object. The object is destroyed when the last `shared_ptr` is destroyed.

15. What is a `unique_ptr` in C++?

A `unique_ptr` is a smart pointer that has exclusive ownership of the object it points to, ensuring that no other `unique_ptr` can point to the same object.

16. What is a `weak_ptr` in C++?

A `weak_ptr` is a smart pointer that does not affect the reference count of the object, allowing safe access to an object without preventing it from being destroyed.

17. What are virtual functions in C++?

Virtual functions allow derived classes to override functions of the base class, enabling polymorphic behavior.

18. What is the purpose of `const` keyword in C++?

`const` is used to define constant values, ensuring that they cannot be modified after initialization.

19. What is the difference between `==` and `=` operators in C++?

`==` is the equality operator used for comparison, while `=` is the assignment operator used to assign values to variables.

20. What is a pure virtual function in C++?

A pure virtual function is a function in a base class that has no implementation and must be overridden in derived classes. It makes the base class abstract.

21. What is the role of `friend` keyword in C++?

The `friend` keyword allows non-member functions or other classes to access the private and protected members of a class.

22. What is the `typedef` keyword in C++?

`typedef` is used to create an alias for a data type, making code easier to understand and maintain.

23. What is a namespace in C++?

A namespace is used to organize code into logical groups and prevent name conflicts between identifiers.

24. What is the use of `using` keyword in C++?

The `using` keyword allows you to introduce a name from a namespace into the current scope, making it easier to access functions or classes without using the full namespace prefix.

25. What is the difference between `break` and `continue` in C++?

`break` exits the loop or switch statement entirely, while `continue` skips the current iteration and continues with the next iteration of the loop.

26. What are the differences between `std::vector` and `std::list` in C++?

`std::vector` provides fast random access, while `std::list` is a doubly linked list, providing fast insertions and deletions at both ends but slower random access.

27. What is the difference between stack and heap memory?

Stack memory is used for automatic memory allocation, like local variables, whereas heap memory is used for dynamic memory allocation, requiring explicit deallocation.

28. What is the purpose of `#include` in C++?

`#include` is a preprocessor directive that tells the compiler to include the contents of a specified file before the actual compilation starts.

29. What is a function pointer in C++?

A function pointer is a pointer that points to a function rather than a variable, allowing functions to be called dynamically.

30. What is RAII in C++?

RAII (Resource Acquisition Is Initialization) is a programming idiom where resources are acquired and released through the lifetime of objects, automatically managing resources.

31. What are references in C++?

A reference is an alias for an existing variable, allowing access to that variable without making a copy of it.

32. What are templates in C++?

Templates allow functions and classes to operate with generic types, enabling code to work with any data type without duplication.

33. What is the difference between `#define` and `const` in C++?

`#define` is a preprocessor directive that defines constants before compilation, while `const` is a keyword that defines constants during compilation and type-checking.

34. What is operator overloading in C++?

Operator overloading allows you to redefine the behavior of operators for user-defined data types.

35. What is function overloading in C++?

Function overloading allows you to define multiple functions with the same name but different parameter lists.

36. What are the advantages of using references over pointers in C++?

References are easier to use and less error-prone, as they cannot be null and must be initialized when declared.

37. What is dynamic polymorphism in C++?

Dynamic polymorphism occurs when a function call is resolved at runtime, typically through virtual functions, allowing derived classes to override base class behavior.

38. What are lambda functions in C++?

Lambda functions are anonymous functions defined inline, often used for short-term tasks or as arguments to algorithms.

39. What is the difference between deep copy and shallow copy in C++?

A shallow copy duplicates the pointer values, while a deep copy creates an independent copy of the entire object or array.

40. What is exception handling in C++?

Exception handling in C++ is used to handle runtime errors using `try`, `catch`, and `throw` blocks to manage exceptions gracefully.

41. What is `std::pair` in C++?

`std::pair` is a container that holds two heterogeneous objects, allowing you to store related data in a single object.

42. What are `std::map` and `std::unordered_map` in C++?

`std::map` stores key-value pairs in sorted order, while `std::unordered_map` stores key-value pairs using hash tables, providing faster lookups at the cost of order.

43. What are iterators in C++?

Iterators are objects that allow you to traverse through the elements of a container, such as a vector or list, in a consistent manner.

44. What is a destructor in C++?

A destructor is a special member function that is called when an object is destroyed, used to release resources and clean up memory.

45. What is `std::sort` in C++?

`std::sort` is a function in the standard library that sorts a range of elements using the quicksort algorithm by default.

46. What is the purpose of `std::find` in C++?

`std::find` is used to search for an element in a range and returns an iterator to the element if found.

47. What are the benefits of using `std::algorithm` in C++?

`std::algorithm` provides a set of functions for common tasks like sorting, searching, and modifying ranges of elements, improving code clarity and reusability.

48. What is a memory leak in C++?

A memory leak occurs when dynamically allocated memory is not properly deallocated, leading to wasted memory resources.

49. What are `std::deque` and `std::list` in C++?

`std::deque` is a double-ended queue that allows fast insertions and deletions at both ends, while `std::list` is a doubly linked list for efficient insertions and deletions at any position.

50. What is the purpose of `std::mutex` in C++?

`std::mutex` is used to protect shared data from concurrent access in multi-threaded applications, ensuring thread safety.