C++ Interview Questions and Answers for experienced
-
What is the difference between `const int* ptr` and `int* const ptr`?
- Answer: `const int* ptr` declares a pointer to a constant integer. The integer value it points to cannot be changed through the pointer, but the pointer itself can point to a different integer. `int* const ptr` declares a constant pointer to an integer. The pointer itself cannot be changed to point to a different integer, but the integer value it points to can be modified.
-
Explain the concept of RAII (Resource Acquisition Is Initialization).
- Answer: RAII is a fundamental C++ programming technique where resource management (like memory, files, network connections) is tied to the lifetime of an object. Resources are acquired in the object's constructor and released in its destructor. This ensures resources are always released, even in the event of exceptions.
-
What are smart pointers and why are they important?
- Answer: Smart pointers are classes that act like pointers but automatically manage the memory they point to, preventing memory leaks. They use RAII to ensure deallocation when the smart pointer goes out of scope. Common types include `unique_ptr`, `shared_ptr`, and `weak_ptr`. They are crucial for robust memory management in C++.
-
Describe the difference between a `vector` and a `deque` in the STL.
- Answer: `vector` provides dynamic array functionality with contiguous memory allocation. Insertion and deletion at the end are efficient, but operations in the middle are slow. `deque` (double-ended queue) allows efficient insertion and deletion at both ends but doesn't guarantee contiguous memory.
-
Explain polymorphism in C++.
- Answer: Polymorphism allows objects of different classes to be treated as objects of a common type. This is achieved through virtual functions and inheritance. It enables flexibility and extensibility in code design.
-
What is a virtual function and how does it work?
- Answer: A virtual function is a member function declared with the `virtual` keyword in the base class. It allows derived classes to override the function's behavior. The actual function called is determined at runtime (dynamic dispatch) based on the object's type, enabling polymorphism.
-
What is the difference between inheritance and composition?
- Answer: Inheritance represents an "is-a" relationship where a derived class inherits properties and methods from a base class. Composition represents a "has-a" relationship where a class contains instances of other classes as members. Composition often leads to more flexible and maintainable designs.
-
Explain the concept of operator overloading.
- Answer: Operator overloading allows you to define the behavior of operators (like +, -, *, /, etc.) for user-defined types. This makes working with custom classes more intuitive by allowing operators to be used in a natural way.
-
What is a lambda expression?
- Answer: A lambda expression is an anonymous function (a function without a name) that can be defined and used inline. They are often used with standard algorithms and other functional programming paradigms.
-
What is the difference between `std::move` and `std::forward`?
- Answer: `std::move` casts an lvalue to an rvalue reference, enabling move semantics. `std::forward` conditionally casts an argument to an rvalue or lvalue reference, preserving the original value category. `std::forward` is crucial for perfect forwarding in template functions.
-
Explain exception handling in C++.
- Answer: Exception handling in C++ uses `try`, `catch`, and `throw` keywords. A `try` block encloses code that might throw an exception. If an exception is thrown, the program control jumps to the appropriate `catch` block that handles the exception type. This mechanism prevents program crashes and allows for graceful error handling.
-
What is a template metaprogramming?
- Answer: Template metaprogramming is a technique that uses templates to perform computations at compile time. This allows for code generation and optimization during compilation, resulting in more efficient code.
-
What are the different types of inheritance in C++?
- Answer: C++ supports several types of inheritance: public, protected, and private. The access specifier determines how the base class members are accessed in the derived class. Public inheritance indicates an "is-a" relationship. Protected and private inheritance are used when the relationship is less direct.
-
Explain the concept of static polymorphism and dynamic polymorphism.
- Answer: Static polymorphism (compile-time polymorphism) is achieved through function overloading and operator overloading. The compiler determines which function to call based on the function signature. Dynamic polymorphism (runtime polymorphism) is achieved through virtual functions and inheritance. The function to call is determined at runtime based on the object's type.
-
What is the purpose of the `volatile` keyword?
- Answer: The `volatile` keyword is used to indicate that a variable can be modified by factors outside the control of the program (e.g., hardware, another thread). This prevents the compiler from optimizing access to the variable in ways that could lead to incorrect behavior.
-
What is the difference between a class and a struct in C++?
- Answer: The only difference between a `class` and a `struct` in C++ is the default access specifier: `class` defaults to `private` access for members, while `struct` defaults to `public`. Functionally, they are otherwise identical.
-
Explain the role of the Standard Template Library (STL) in C++.
- Answer: The STL provides a rich set of ready-to-use data structures (like vectors, lists, maps, sets) and algorithms (like sorting, searching, etc.). It promotes code reusability and simplifies development.
-
How do you handle memory leaks in C++?
- Answer: Memory leaks can be avoided by using smart pointers, ensuring proper resource management with RAII, and avoiding manual memory allocation (using `new` and `delete`) when possible. Tools like Valgrind can help detect memory leaks.
-
What is a dangling pointer? How can you prevent them?
- Answer: A dangling pointer points to memory that has been freed or is no longer valid. They can lead to crashes or unpredictable behavior. Dangling pointers can be prevented by careful resource management, using smart pointers, and ensuring that pointers are not used after the memory they point to has been released.
Thank you for reading our blog post on 'C++ Interview Questions and Answers for experienced'.We hope you found it informative and useful.Stay tuned for more insightful content!