C++ Interview Questions and Answers for 10 years experience
-
What is the difference between a pointer and a reference in C++?
- Answer: Pointers are variables that hold memory addresses. They can be reassigned to point to different memory locations, can be NULL, and require dereferencing (*) to access the value they point to. References, on the other hand, are aliases for existing variables. They must be initialized upon declaration and cannot be reassigned to refer to a different variable. They don't require dereferencing to access the value.
-
Explain polymorphism in C++ with examples.
- Answer: Polymorphism allows you to treat objects of different classes in a uniform way. This is achieved through virtual functions and inheritance. A virtual function is declared in a base class and overridden in derived classes. When a virtual function is called through a base class pointer, the correct version of the function (from the derived class) is executed at runtime (dynamic dispatch). Example: a base class `Animal` with a virtual function `speak()`, and derived classes `Dog` and `Cat` overriding `speak()` to produce different sounds.
-
What are smart pointers in C++ 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 typically use RAII (Resource Acquisition Is Initialization) to ensure that the memory is released when the smart pointer goes out of scope. Common smart pointers include `unique_ptr` (exclusive ownership), `shared_ptr` (shared ownership), and `weak_ptr` (non-owning observer).
-
Describe the difference between `new` and `malloc` in C++.
- Answer: `new` is a C++ operator that allocates memory and calls the constructor of the object being created. `malloc` is a C function that allocates raw memory without constructor calls. `new` is type-safe and handles exceptions; `malloc` is not. `new` should be preferred in C++.
-
Explain the concept of RAII (Resource Acquisition Is Initialization).
- Answer: RAII is a programming idiom where resource allocation is tied to object initialization, and resource deallocation is tied to object destruction. This ensures that resources are automatically managed, preventing leaks and simplifying error handling. Smart pointers are a prime example of RAII.
-
Explain the use of templates in C++. Give examples of function templates and class templates.
- Answer:Templates allow you to write generic code that can work with different data types without being rewritten for each type. Function templates operate on different data types, while class templates create generic classes. Example: a function template to find the maximum of two values, and a class template for a generic stack.
-
What is the difference between a static and a dynamic library?
- Answer: A static library is linked directly into the executable at compile time, increasing the executable's size but making it self-contained. A dynamic library (DLL or shared library) is loaded at runtime, reducing executable size but requiring the library to be present on the system.
-
What is the role of the Standard Template Library (STL) in C++?
- Answer: The STL provides a wide range of ready-to-use data structures (like vectors, lists, maps, sets) and algorithms (like sorting, searching) that greatly simplify C++ programming. It promotes code reusability and efficiency.
-
Explain the concept of operator overloading. Provide an example.
- Answer: Operator overloading allows you to redefine the behavior of operators (like +, -, *, /) for user-defined types. This makes code more intuitive. Example: overloading the + operator to add two complex numbers.
Thank you for reading our blog post on 'C++ Interview Questions and Answers for 10 years experience'.We hope you found it informative and useful.Stay tuned for more insightful content!