codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

86 / 87 topics
84Projects & Programming Examples85Exercises, Quizzes & Code Challenges86Interview Questions87Learning Paths, Syllabus & Certification
Tutorials/C++ Programming/Interview Questions
⚡C++ Programming

Interview Questions

Updated 2026-05-12
40 min read

Interview Questions

Preparing for a C++ programming job interview? This section is designed to help you tackle some of the most common and challenging questions that you might encounter. Whether you're a beginner or an experienced developer, these questions will test your understanding of core concepts, problem-solving skills, and coding efficiency.

1. What is the difference between new and delete in C++?

Answer:
new is used to allocate memory for an object at runtime, while delete is used to deallocate that memory. Using new without a corresponding delete can lead to memory leaks.

memory.cpp
1#include <iostream>
2
3int main() {
4 int* ptr = new int(10);
5 std::cout << *ptr << std::endl;
6 delete ptr; // Don't forget to free the allocated memory
7 return 0;
8}
Output

3. What is the difference between std::vector and std::array?

Answer:
std::vector is a dynamic array that can resize itself during runtime, while std::array is a fixed-size array with a size known at compile time.

vector_vs_array.cpp
1#include <iostream>
2#include <vector>
3#include <array>
4
5int main() {
6 std::vector<int> vec = {1, 2, 3};
7 std::array<int, 3> arr = {4, 5, 6};
8
9 vec.push_back(4); // Can add elements to vector
10 // arr.push_back(7); // Error: push_back is not a member of std::array
11
12 return 0;
13}
Output

5. Explain the difference between std::unique_ptr, std::shared_ptr, and std::weak_ptr.

Answer:

  • std::unique_ptr: Owns the object it points to and does not allow copying.
  • std::shared_ptr: Allows multiple pointers to share ownership of an object, with reference counting.
  • std::weak_ptr: Does not own the object but provides a non-owning reference.
smart_pointers.cpp
1#include <iostream>
2#include <memory>
3
4int main() {
5 std::unique_ptr<int> uniquePtr = std::make_unique<int>(10);
6 // std::unique_ptr<int> anotherUniquePtr = uniquePtr; // Error: cannot copy
7
8 std::shared_ptr<int> sharedPtr = std::make_shared<int>(20);
9 std::shared_ptr<int> anotherSharedPtr = sharedPtr; // Valid, reference count increases
10
11 std::weak_ptr<int> weakPtr = sharedPtr;
12
13 return 0;
14}
Output

7. What is the difference between override and final keywords?

Answer:

  • override: Used to explicitly indicate that a member function is intended to override a base class function.
  • final: Used to prevent further overriding of a virtual function in derived classes.
override_final.cpp
1#include <iostream>
2
3class Base {
4public:
5 virtual void display() final { // 'display' cannot be overridden
6 std::cout << "Display from Base" << std::endl;
7 }
8};
9
10class Derived : public Base {
11public:
12 // void display() override; // Error: 'display' is final in base class
13};
14
15int main() {
16 Base* basePtr = new Base();
17 basePtr->display(); // Outputs: Display from Base
18 delete basePtr;
19 return 0;
20}
Output

9. What is the difference between std::move and std::forward?

Answer:

  • std::move: Converts an lvalue to an rvalue reference, effectively transferring ownership.
  • std::forward: Preserves the value category (lvalue or rvalue) of its argument.
move_forward.cpp
1#include <iostream>
2#include <utility>
3
4void print(int& x) { std::cout << "L-value: " << x << std::endl; }
5void print(int&& x) { std::cout << "R-value: " << x << std::endl; }
6
7template<typename T>
8void forward_example(T&& arg) {
9 print(std::forward<T>(arg));
10}
11
12int main() {
13 int a = 10;
14 forward_example(a); // Outputs: L-value
15 forward_example(20); // Outputs: R-value
16 return 0;
17}
Output

11. What is the difference between std::bind and lambda expressions?

Answer:

  • std::bind: Binds arguments to a function, creating a callable object.
  • Lambda expressions: Provide a more concise way to define inline functions.
bind_vs_lambda.cpp
1#include <iostream>
2#include <functional>
3
4void print(int x) {
5 std::cout << "Value: " << x << std::endl;
6}
7
8int main() {
9 int value = 10;
10
11 // Using std::bind
12 auto boundFunc = std::bind(print, value);
13 boundFunc(); // Outputs: Value: 10
14
15 // Using lambda expression
16 auto lambdaFunc = [value]() { print(value); };
17 lambdaFunc(); // Outputs: Value: 10
18
19 return 0;
20}
Output

13. What is a template in C++?

Answer:
A template allows you to write generic code that can operate on different data types.

template.cpp
1#include <iostream>
2
3template<typename T>
4T add(T a, T b) {
5 return a + b;
6}
7
8int main() {
9 std::cout << "Sum of integers: " << add(3, 4) << std::endl;
10 std::cout << "Sum of doubles: " << add(2.5, 3.1) << std::endl;
11 return 0;
12}
Output

15. What is the difference between std::vector and std::list?

Answer:

  • std::vector: Provides fast random access but slower insertions/deletions.
  • std::list: Provides fast insertions/deletions but slower random access.
vector_vs_list.cpp
1#include <iostream>
2#include <vector>
3#include <list>
4
5int main() {
6 std::vector<int> vec = {1, 2, 3};
7 std::list<int> lst = {4, 5, 6};
8
9 // Inserting at the end
10 vec.push_back(7);
11 lst.push_back(8);
12
13 // Inserting at the front
14 vec.insert(vec.begin(), 0);
15 lst.insert(lst.begin(), 9);
16
17 return 0;
18}
Output

17. What is a friend class in C++?

Answer:
A friend class is a class whose members can access the private and protected members of another class.

friend_class.cpp
1#include <iostream>
2
3class SecretHolder {
4private:
5 int secret = 42;
6
7public:
8 friend class FriendClass;
9};
10
11class FriendClass {
12public:
13 void revealSecret(const SecretHolder& obj) {
14 std::cout << "The secret is: " << obj.secret << std::endl;
15 }
16};
17
18int main() {
19 SecretHolder holder;
20 FriendClass friendObj;
21 friendObj.revealSecret(holder); // Outputs: The secret is: 42
22 return 0;
23}
Output

19. What is a copy constructor in C++?

Answer:
A copy constructor is a special member function that initializes an object using another object of the same class.

copy_constructor.cpp
1#include <iostream>
2#include <string>
3
4class Person {
5public:
6 std::string name;
7
8 Person(const std::string& n) : name(n) {}
9
10 // Copy constructor
11 Person(const Person& other) : name(other.name) {
12 std::cout << "Copy constructor called" << std::endl;
13 }
14};
15
16int main() {
17 Person original("Alice");
18 Person copy = original; // Copy constructor is called
19
20 std::cout << "Original: " << original.name << ", Copy: " << copy.name << std::endl;
21
22 return 0;
23}
Output

21. What is the difference between std::string and char*?

Answer:

  • std::string: Provides a high-level interface for strings with automatic memory management.
  • char*: A pointer to a character array, requires manual memory management.
string_vs_char.cpp
1#include <iostream>
2#include <string>
3
4int main() {
5 std::string str = "Hello";
6 char* cstr = new char[6];
7 strcpy(cstr, "World");
8
9 std::cout << "String: " << str << std::endl;
10 std::cout << "Char array: " << cstr << std::endl;
11
12 delete[] cstr; // Don't forget to free the allocated memory
13 return 0;
14}
Output

23. What is the difference between std::set and std::unordered_set?

Answer:

  • std::set: Maintains elements in sorted order, with logarithmic time complexity for insertions and deletions.
  • std::unordered_set: Uses a hash table to store elements, providing average constant-time complexity for insertions and deletions.
set_vs_unordered_set.cpp
1#include <iostream>
2#include <set>
3#include <unordered_set>
4
5int main() {
6 std::set<int> s = {5, 3, 8};
7 std::unordered_set<int> us = {2, 7, 1};
8
9 // Inserting elements
10 s.insert(6);
11 us.insert(4);
12
13 // Iterating over sets
14 for (const auto& elem : s) {
15 std::cout << elem << " ";
16 }
17 std::cout << std::endl;
18
19 for (const auto& elem : us) {
20 std::cout << elem << " ";
21 }
22 return 0;
23}
Output

25. What is the difference between std::thread and std::async?

Answer:

  • std::thread: Creates a new thread of execution.
  • std::async: Launches a task asynchronously and returns a std::future.
thread_vs_async.cpp
1#include <iostream>
2#include <thread>
3#include <future>
4
5int compute() {
6 return 42;
7}
8
9int main() {
10 std::thread t(compute);
11 t.join();
12
13 auto future = std::async(std::launch::async, compute);
14 int result = future.get();
15 std::cout << "Result: " << result << std::endl; // Outputs: Result: 42
16
17 return 0;
18}
Output

27. What is the difference between std::shared_ptr and std::unique_ptr?

Answer:

  • std::shared_ptr: Allows multiple pointers to share ownership of an object.
  • std::unique_ptr: Owns the object exclusively, cannot be copied.
shared_vs_unique.cpp
1#include <iostream>
2#include <memory>
3
4int main() {
5 std::unique_ptr<int> uniquePtr = std::make_unique<int>(10);
6 // std::unique_ptr<int> anotherUniquePtr = uniquePtr; // Error: cannot copy
7
8 std::shared_ptr<int> sharedPtr = std::make_shared<int>(20);
9 std::shared_ptr<int> anotherSharedPtr = sharedPtr; // Valid, reference count increases
10
11 return 0;
12}
Output

29. What is a functor in C++?

Answer:
A functor is an object that can be called like a function.

functor.cpp
1#include <iostream>
2
3class Adder {
4public:
5 int operator()(int a, int b) const {
6 return a + b;
7 }
8};
9
10int main() {
11 Adder adder;
12 std::cout << "Sum: " << adder(3, 4) << std::endl; // Outputs: Sum: 7
13 return 0;
14}
Output

31. What is a template specialization?

Answer:
Template specialization allows you to provide a specific implementation for a particular type.

template_specialization.cpp
1#include <iostream>
2
3template<typename T>
4T add(T a, T b) {
5 return a + b;
6}
7
8// Specialization for std::string
9template<>
10std::string add<std::string>(std::string a, std::string b) {
11 return a + " " + b;
12}
13
14int main() {
15 std::cout << "Sum of integers: " << add(3, 4) << std::endl;
16 std::cout << "Concatenation of strings: " << add("Hello", "World") << std::endl;
17 return 0;
18}
Output

33. What is a move constructor in C++?

Answer:
A move constructor is a special member function that transfers resources from one object to another.

move_constructor.cpp
1#include <iostream>
2#include <string>
3
4class Resource {
5public:
6 std::string data;
7
8 Resource(const std::string& d) : data(d) {
9 std::cout << "Resource created: " << data << std::endl;
10 }
11
12 // Move constructor
13 Resource(Resource&& other) noexcept : data(std::move(other.data)) {
14 std::cout << "Resource moved: " << data << std::endl;
15 }
16};
17
18int main() {
19 Resource original("Data");
20 Resource moved = std::move(original); // Move constructor is called
21
22 return 0;
23}
Output

35. What is a namespace in C++?

Answer:
A namespace is a declarative region that provides a scope to the identifiers (names of types, functions, variables) inside it.

namespace.cpp
1#include <iostream>
2
3namespace Math {
4 int add(int a, int b) {
5 return a + b;
6 }
7}
8
9int main() {
10 std::cout << "Sum: " << Math::add(3, 4) << std::endl; // Outputs: Sum: 7
11 return 0;
12}
Output

37. What is a mutex in C++?

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

mutex.cpp
1#include <iostream>
2#include <thread>
3#include <mutex>
4
5std::mutex mtx;
6int counter = 0;
7
8void increment() {
9 std::lock_guard<std::mutex> lock(mtx);
10 ++counter;
11}
12
13int main() {
14 std::thread t1(increment);
15 std::thread t2(increment);
16
17 t1.join();
18 t2.join();
19
20 std::cout << "Counter: " << counter << std::endl; // Outputs: Counter: 2
21 return 0;
22}
Output

39. What is a smart pointer in C++?

Answer:
A smart pointer is an object that manages another object, providing automatic memory management.

smart_pointer.cpp
1#include <iostream>
2#include <memory>
3
4int main() {
5 std::shared_ptr<int> sharedPtr = std::make_shared<int>(10);
6 std::cout << "Shared ptr count: " << sharedPtr.use_count() << std::endl; // Outputs: 1
7
8 {
9 std::shared_ptr<int> anotherSharedPtr = sharedPtr;
10 std::cout << "Shared ptr count inside block: " << sharedPtr.use_count() << std::endl; // Outputs: 2
11 }
12
13 std::cout << "Shared ptr count after block: " << sharedPtr.use_count() << std::endl; // Outputs: 1
14
15 return 0;
16}
Output

41. What is a function pointer in C++?

Answer:
A function pointer is a variable that stores the address of a function.

function_pointer.cpp
1#include <iostream>
2
3void greet() {
4 std::cout << "Hello, World!" << std::endl;
5}
6
7int main() {
8 void (*funcPtr)() = &greet; // Function pointer
9 funcPtr(); // Calls the function
10
11 return 0;
12}
Output

43. What is the difference between std::vector and std::deque?

Answer:

  • std::vector: Provides fast random access but slower insertions/deletions at the front.

PreviousExercises, Quizzes & Code ChallengesNext Learning Paths, Syllabus & Certification

Recommended Gear

Exercises, Quizzes & Code ChallengesLearning Paths, Syllabus & Certification