C++ Interview Questions & Answers: BY HK2002
C++ Interview Questions & Answers: BY HK2002
C++
INTERVIEW
QUESTIONS & ANSWERS
BY HK2002
C++ Interview Questions & Answers
1.What is C++?
C++ is a general-purpose programming language created by Bjarne
Stroustrup as an extension of the C programming language. It supports
object-oriented, procedural, and generic programming.
HK2002
6.What is polymorphism in C++?
Polymorphism allows methods to do different things based on the object
it is acting upon, typically achieved through method overloading and
method overriding.
HK2002
12.What is a destructor in C++?
A destructor is a special member function of a class that cleans up when
an object is destroyed. It is called automatically when an object goes out
of scope or is deleted.
HK2002
17.What are the types of templates in C++?
The types of templates are:
Function templates
Class templates
HK2002
22.Explain the concept of smart pointers.
Smart pointers are objects which store pointers to dynamically allocated
memory and ensure proper memory management by automatically
deleting the memory when the smart pointer goes out of scope.
Examples include std::unique_ptr and std::shared_ptr.
25.What is a namespace?
A namespace is a declarative region that provides a scope to the
identifiers inside it, helping to avoid name conflicts.
HK2002
27.What is the difference between struct and class in C++?
The primary difference is that members of a struct are public by default,
while members of a class are private by default.
30.What is nullptr?
nullptr is a keyword introduced in C++11 to represent a null pointer,
replacing the older NULL.
HK2002
32.How do you find the length of a string in C++?
You can use the length or size method of the std::string class:
std::string str = "hello";
size_t length = str.length();
HK2002
37.How do you create a thread in C++?
You can create a thread using the std::thread class:
#include <thread>
void function() { /*...*/ }
std::thread t(function);
t.join();
40.What is a deadlock?
A deadlock is a situation where two or more threads are blocked forever,
waiting for each other to release resources.
HK2002
41.What are best practices for exception handling in C++?
Use exceptions for error handling, not for control flow.
Catch exceptions by reference.
Always clean up resources using RAII.
HK2002
45.What are inline functions?
Inline functions are functions defined with the inline keyword,
suggesting to the compiler to insert the function's body where the
function call is made to reduce function call overhead.
48.What is std::move?
std::move is a standard library function that casts an object to an r-value
reference, enabling move semantics.
49.What is std::forward?
std::forward is a standard library function used to forward arguments
while preserving their value category (l-value or r-value).
HK2002
50.What are constexpr functions?
constexpr functions are functions evaluated at compile time, allowing
them to be used in constant expressions.
HK2002
Easy to learn
C++
INTERVIEW
QUESTIONS & ANSWERS
BY HK2002