Part-H of C++ interview questions with new answers:
71. What are C++ variadic templates, and how do they enable functions with a variable number of arguments?
- C++ variadic templates allow you to define functions or classes that can accept a variable number of arguments. They use template parameter packs (using
...
) to handle an arbitrary number of arguments during compile-time.
72. Explain the usage of std::forward
in C++ templates.
std::forward
is used to preserve the value category (lvalue or rvalue) of function arguments when forwarding them to other functions within a template. It is typically used in perfect forwarding scenarios to ensure that arguments are forwarded in the same state as they were received.
73. How do you implement a custom hash function for user-defined types in C++ unordered containers (std::unordered_map
, std::unordered_set
)?
- To use user-defined types in C++ unordered containers, you need to provide a custom hash function by defining a specialization of
std::hash
for your user-defined type. The hash function should take the object as input and return a hash value of typestd::size_t
.
74. Explain the use of the decltype
keyword in C++.
- The
decltype
keyword is used to deduce the type of an expression at compile time. It allows you to declare variables with the same type as the expression without explicitly specifying the type.
75. How do you reverse a singly linked list in C++?
- To reverse a singly linked list in C++, you need to modify the pointers of the nodes to change the direction of the list. You can use three pointers (current, previous, next) to traverse and reverse the list iteratively.
76. What are C++ function templates, and how are they used for generic programming?
- C++ function templates allow you to define generic functions that can work with various data types. They are defined using the
template
keyword followed by template parameters. The function template is instantiated for different data types when called, allowing for code reuse and generic programming.
77. What is the difference between std::find
and std::binary_search
in C++ STL algorithms?
std::find
: It is used to find the first occurrence of a specific value in a range. It performs a linear search and has a time complexity of O(n).std::binary_search
: It is used to determine if a specific value exists in a sorted range. It performs a binary search and has a time complexity of O(log n).
78. How do you implement the Sieve of Eratosthenes algorithm in C++ to find prime numbers efficiently?
- The Sieve of Eratosthenes algorithm is used to find all prime numbers up to a given limit. It works by marking non-prime numbers in a boolean array and eliminating multiples of each prime number.
79. What is the role of the override
keyword when dealing with virtual functions in C++11 and later versions?
- In C++11 and later versions, the
override
keyword is used to explicitly indicate that a function is intended to override a virtual function in the base class. It helps catch potential errors at compile-time when the function signature does not match any virtual function in the base class.
80. How can you implement a thread-safe singleton pattern in C++11 and later versions using std::call_once
?
- To implement a thread-safe singleton pattern in C++11 and later, you can use
std::call_once
to ensure that the singleton instance is created only once, even in a multi-threaded environment.
Continue reviewing these questions and their answers to strengthen your C++ knowledge for interviews. Keep practicing coding problems to build confidence in your skills. Good luck!