Part-C of C++ interview questions along with their answers:
21. What is the difference between new
and delete
and malloc
and free
?
new
anddelete
are C++ operators used for dynamic memory allocation and deallocation, respectively. They call constructors and destructors for objects.malloc
andfree
are C functions from C standard library used for dynamic memory allocation and deallocation, respectively. They do not call constructors and destructors, so not suitable for C++ objects.
22. What is the this
pointer in C++?
- The
this
pointer is a special pointer available inside the member functions of a class. It points to the current object that the member function is being called on. It is used to access the members of the current object.
23. Explain the const
correctness in C++.
const
correctness refers to the practice of usingconst
properly to make sure that objects are not modified unintentionally. It involves usingconst
with member functions that do not modify the object, and usingconst
with parameters to prevent their modification within the function.
24. What is a virtual destructor? Why is it used?
- A virtual destructor is a destructor that is declared as
virtual
in the base class. When a derived class object is deleted through a pointer to the base class, a virtual destructor ensures that the correct destructor (including the derived class destructor) is called, preventing memory leaks.
25. How is function overloading different from function overriding?
- Function overloading allows multiple functions with the same name in the same class, but with different parameter lists.
- Function overriding occurs in a derived class when a virtual function in the base class is redefined with the same name and parameter list.
26. What are lambda expressions in C++?
- Lambda expressions are anonymous functions that can be defined inline. They provide a concise way of defining function objects (functors) on the fly, especially for short, one-off operations.
27. What are namespaces in C++?
- Namespaces are used to group related variables, functions, and classes together to avoid naming conflicts. They provide a way to organize code and improve code modularity.
28. Explain the concept of move semantics in C++11.
- Move semantics allow objects to be efficiently transferred (moved) from one location to another, rather than copied. It is particularly useful when dealing with expensive-to-copy objects like large containers or dynamically allocated memory.
29. What is the difference between std::map
and std::unordered_map
in the C++ STL?
std::map
is an ordered associative container that stores elements as key-value pairs in sorted order based on the keys.std::unordered_map
is an unordered associative container that stores elements as key-value pairs without any specific order, providing faster lookup times.
30. What is the role of the explicit
keyword in C++ constructors?
- The
explicit
keyword is used to prevent implicit conversions in constructors. When a constructor is marked asexplicit
, it cannot be used for implicit type conversions.