0% found this document useful (0 votes)
25 views13 pages

C++ Interview Questions & Answers: BY HK2002

Uploaded by

NE-5437 Official
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views13 pages

C++ Interview Questions & Answers: BY HK2002

Uploaded by

NE-5437 Official
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Easy to learn

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.

2.What are the basic concepts of OOP in C++?


The basic concepts of Object-Oriented Programming (OOP) in C++ are:
 Encapsulation
 Inheritance
 Polymorphism
 Abstraction

3.What is a class in C++?


A class in C++ is a user-defined data type that contains data members
(variables) and member functions (methods) that operate on the data.

4.What is an object in C++?


An object is an instance of a class. It encapsulates data and functionality
pertaining to that class.

5.What is inheritance in C++?


Inheritance is a feature of OOP that allows a new class to inherit
properties and behaviors (data members and methods) from an existing
class.

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.

7.Explain the concept of function overloading.


Function overloading is the ability to create multiple functions with the
same name but with different parameters.

8.What is operator overloading?


Operator overloading allows you to define how an operator works with
user-defined types (e.g., classes).

9.What is a virtual function?


A virtual function is a function in a base class that can be overridden in
derived classes. It is used to achieve runtime polymorphism.

10.Explain the concept of a pure virtual function.


A pure virtual function is a virtual function with no implementation,
declared by assigning 0 in its declaration. It makes the class abstract.

11.What is a constructor in C++?


A constructor is a special member function of a class that initializes
objects of the class. It is called automatically when an object is created.

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.

13.What is the difference between stack and heap memory?


Stack memory is used for static memory allocation, while heap memory
is used for dynamic memory allocation. Stack is faster but limited in
size, whereas the heap is larger but slower.

14.What is a memory leak?


A memory leak occurs when dynamically allocated memory is not
properly deallocated, causing a program to consume more memory over
time.

15.How do you allocate and deallocate memory in C++?


Memory is allocated using new and deallocated using delete. For arrays,
new[] and delete[] are used.

16.What is a template in C++?


A template is a blueprint or formula for creating generic classes or
functions. It allows writing code that can work with any data type.

HK2002
17.What are the types of templates in C++?
The types of templates are:
 Function templates
 Class templates

18.What is the Standard Template Library (STL)?


The STL is a library of container classes, algorithms, and iterators. It
provides common programming data structures and functions like lists,
stacks, arrays, and so on.

19.Explain the concept of iterators in STL.


Iterators are objects that point to elements within a container and allow
traversal through the container.

20.What is the difference between vector and list in STL?


vector is a dynamic array, providing fast random access but slow
insertion/deletion in the middle. list is a doubly linked list, allowing fast
insertion/deletion but slow random access.

21.What is RAII in C++?


RAII (Resource Acquisition Is Initialization) is a programming idiom
where resources are acquired and released by objects, ensuring proper
cleanup and resource management.

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.

23.What is a lambda function?


A lambda function is an anonymous function defined within a single
statement. It can capture variables from its enclosing scope.

24.What are move semantics?


Move semantics allow the resources to be moved from one object to
another, avoiding deep copy and making the program more efficient.
This is achieved using move constructors and move assignment
operators.

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.

26.What is const in C++?


const is a keyword used to declare variables as constant, meaning their
value cannot be changed after initialization.

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.

28.What is a copy constructor?


A copy constructor is a constructor that creates a new object as a copy of
an existing object.

29.What is the use of explicit keyword?


The explicit keyword is used to prevent implicit conversions and copy-
initialization, making constructors explicit.

30.What is nullptr?
nullptr is a keyword introduced in C++11 to represent a null pointer,
replacing the older NULL.

31.How do you reverse a string in C++?


You can use the std::reverse function from the algorithm header:
#include <algorithm>
#include <string>
std::string str = "hello";
std::reverse(str.begin(), str.end());

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();

33.What is a map in STL?


A map is an associative container that stores key-value pairs, with
unique keys. It allows fast retrieval of values based on keys.

34.What is a set in STL?


A set is a container that stores unique elements in a specific order. It
allows fast retrieval of elements.

35.How do you sort a container in C++?


You can use the std::sort function:
#include <algorithm>
std::vector<int> vec = {4, 2, 3, 1};
std::sort(vec.begin(), vec.end());

36.What is a thread in C++?


A thread is a unit of execution within a program. C++11 introduced
threading support via the <thread> library.

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();

38.What is a mutex in C++?


A mutex (mutual exclusion) is a synchronization primitive used to
protect shared data from being simultaneously accessed by multiple
threads.

39.How do you use a mutex in C++?


You can use the std::mutex class:
#include <mutex>
std::mutex mtx;
mtx.lock();
// critical section
mtx.unlock();

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.

42.How do you handle resource cleanup in C++?


Use RAII (Resource Acquisition Is Initialization) to ensure resources are
properly cleaned up.

43.What are some common pitfalls in C++?


 Memory leaks due to improper memory management.
 Dangling pointers and references.
 Undefined behavior due to out-of-bounds array access or
uninitialized variables.

44.What is the rule of three/five/zero in C++?


 The rule of three: If a class requires a user-defined destructor, copy
constructor, or copy assignment operator, it likely requires all
three.
 The rule of five (C++11): If a class requires a user-defined
destructor, copy/move constructor, or copy/move assignment
operator, it likely requires all five.
 The rule of zero: Prefer designs that avoid the need for custom
destructors, copy/move constructors, and copy/move assignment
operators by using smart pointers and 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.

46.What is auto keyword?


The auto keyword allows the compiler to automatically deduce the type
of a variable from its initializer.

47.What are range-based for loops?


Range-based for loops provide a more readable way to iterate over
containers:
std::vector<int> vec = {1, 2, 3, 4};
for (int val : vec) {
std::cout << val << " ";
}

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

You might also like