C++ is still one of the most tested languages in Indian fresher interviews, particularly at IT services companies (TCS, Wipro, Infosys), embedded systems roles, and fintech companies that deal with low-latency infrastructure. Unlike Java or Python where most fresher questions are OOP fundamentals, C++ interviews go deeper: pointers, memory management, the STL, and the differences between C and C++ are all fair game. This guide covers what is actually asked in 2026, not a generic C++ textbook.
OOP in C++: Questions That Actually Come Up
OOP questions in C++ interviews differ from Java OOP questions because C++ adds complexity: multiple inheritance, virtual functions, and explicit memory management.
1. What is the difference between class and struct in C++? In C++, the only difference is the default access modifier: class members are private by default, struct members are public by default. Both can have methods, constructors, and inheritance. ```cpp struct Point { int x, y; }; // x, y are public by default class Circle { int radius; }; // radius is private by default ```
2. Explain virtual functions and vtable. A virtual function allows derived class methods to override base class methods called through a base class pointer. The vtable (virtual table) is a lookup table maintained by the compiler for each class with virtual functions. ```cpp class Animal { public: virtual void sound() { cout << "Generic sound"; } }; class Dog : public Animal { public: void sound() override { cout << "Bark"; } }; Animal* a = new Dog(); a->sound(); // Prints "Bark": runtime polymorphism ```
3. What is a pure virtual function? What is an abstract class? A pure virtual function has no implementation in the base class: `virtual void func() = 0;`. A class with at least one pure virtual function is an abstract class and cannot be instantiated.
4. What is the difference between function overloading and function overriding?
- Overloading: Same function name, different parameters, in the same class (compile-time polymorphism)
- Overriding: Derived class redefines a base class virtual function (runtime polymorphism)
5. Explain multiple inheritance in C++. What is the diamond problem? ```cpp class A { public: void func() {} }; class B : public A {}; class C : public A {}; class D : public B, public C {}; // D has two copies of A: diamond problem ``` Solution: Use virtual inheritance: `class B : virtual public A {};`
6. What is the difference between public, private, and protected inheritance?
- public inheritance: public and protected members of base stay accessible
- private inheritance: all base members become private in derived class
- protected inheritance: public base members become protected
Pointers and Memory Management
Pointers and memory management are C++-specific and account for a large fraction of C++ interview questions. These do not appear in Java or Python interviews.
1. What is the difference between a pointer and a reference?
- A pointer can be null, can be reassigned, and can be incremented
- A reference must be initialised, cannot be null, and cannot be reseated to another variable
- References are generally safer; pointers are needed for dynamic memory and optional arguments
2. What is the difference between stack and heap memory? - Stack: automatic allocation/deallocation, fixed size, fast, stores local variables - Heap: dynamic allocation (new/delete), larger, slower, programmer manages lifecycle ```cpp int x = 10; // stack int* p = new int(10); // heap: must be deleted delete p; // free heap memory ```
3. What is a memory leak? How do you avoid it? A memory leak occurs when heap-allocated memory is not freed. Avoid with:
- Always pair `new` with `delete`
- Use smart pointers: `std::uniqueptr`, `std::sharedptr` (C++11+)
- RAII (Resource Acquisition Is Initialization) pattern
4. What is a dangling pointer? A pointer that points to memory that has already been freed: ```cpp int p = new int(5); delete p; // p is now a dangling pointer: accessing p is undefined behaviour p = nullptr; // fix: set to nullptr after delete ```
5. What is the difference between shallow copy and deep copy? - Shallow copy: copies pointer addresses (both objects point to same memory) - Deep copy: copies the actual data to new memory (independent copies) If a class has pointer members, the default copy constructor does shallow copy: define your own copy constructor for deep copy.
6. What are smart pointers? Explain unique_ptr and shared_ptr.
- `unique_ptr`: exclusive ownership; cannot be copied, only moved; automatically frees memory when out of scope
- `sharedptr`: shared ownership via reference counting; memory freed when last sharedptr goes out of scope
- `weak_ptr`: non-owning reference; does not affect reference count; used to break circular dependencies
STL: Standard Template Library
STL questions are very common in product company C++ interviews. Know the containers, their time complexities, and when to use each.
Common STL Containers:
| Container | Underlying | Access | Insert/Delete | Use when | |---|---|---|---|---| | vector | Dynamic array | O(1) random | O(n) middle, O(1) amortised end | Default sequence container | | list | Doubly linked list | O(n) | O(1) anywhere | Frequent insert/delete in middle | | deque | Double-ended queue | O(1) | O(1) both ends | Queue + random access | | map | Red-Black tree | O(log n) | O(log n) | Sorted key-value pairs | | unorderedmap | Hash table | O(1) average | O(1) average | Fast lookup, order doesn't matter | | set | Red-Black tree | O(log n) | O(log n) | Unique sorted elements | | priorityqueue | Heap | O(1) top | O(log n) | Min/max queries | | stack | deque/vector | O(1) top | O(1) | LIFO operations | | queue | deque | O(1) front | O(1) | FIFO operations |
Frequently asked STL questions: 1. What is the difference between map and unorderedmap? 2. How does a priorityqueue work? Write code to find the Kth largest element. 3. What is the difference between vector::pushback and emplaceback? 4. How do you iterate over an unordered_map? 5. What happens if you access a map key that doesn't exist? Answer: It is default-inserted with a zero-initialised value: this is a common bug.
STL algorithms you should know:
- `sort()`, `find()`, `count()`, `binarysearch()`, `lowerbound()`, `upper_bound()`
- `accumulate()`, `maxelement()`, `minelement()`
- `reverse()`, `unique()`, `next_permutation()`
C++ technical rounds test both code correctness and your ability to explain memory management and OOP design decisions out loud. HireStepX's voice-based technical interview practice is specifically useful here: explaining why you chose a shared_ptr over a raw pointer, or why you used a map vs an unordered_map, is a skill you build by saying it, not just by knowing it.
Practice freeC++ Coding Problems Asked at Fresher Interviews
Fresher C++ coding questions test basic algorithms and C++ syntax simultaneously. Companies expect you to write runnable C++ code, not pseudocode.
Problem 1: Reverse a string without using library functions ```cpp #include <iostream> #include <string> using namespace std; string reverseStr(string s) { int l = 0, r = s.length() - 1; while (l < r) { swap(s[l], s[r]); l++; r--; } return s; } ```
Problem 2: Find if a number is a perfect number (A perfect number equals the sum of its divisors: 6 = 1+2+3) ```cpp bool isPerfect(int n) { int sum = 1; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { sum += i; if (i != n / i) sum += n / i; } } return sum == n && n != 1; } ```
Problem 3: Implement a stack using an array ```cpp class Stack { int arr[100], top = -1; public: void push(int x) { arr[++top] = x; } int pop() { return arr[top--]; } int peek() { return arr[top]; } bool isEmpty() { return top == -1; } }; ```
Problem 4: Count occurrences of each character (using map) ```cpp #include <unorderedmap> void charCount(string s) { unorderedmap<char, int> freq; for (char c : s) freq[c]++; for (auto& [ch, cnt] : freq) cout << ch << ": " << cnt << "\n"; } ```
Problem 5: Check if a string is a palindrome ```cpp bool isPalindrome(string s) { int l = 0, r = s.length() - 1; while (l < r) { if (s[l++] != s[r--]) return false; } return true; } ```
C vs C++: Questions That Trip Freshers
Many freshers use C for college coursework and switch to C++ for interviews. Interviewers frequently test the boundaries.
Key differences C vs C++ (commonly asked):
| Feature | C | C++ | |---|---|---| | Paradigm | Procedural only | Procedural + OOP | | Classes | Not supported | Core feature | | Function overloading | Not supported | Supported | | Templates | Not supported | Supported | | Exception handling | Not supported | try/catch/throw | | References | Not supported | Supported | | Default arguments | Not supported | Supported | | new/delete | Not available (use malloc/free) | Preferred way to allocate | | bool type | No native bool (use int) | Native bool | | Inline functions | Limited (macros) | Supported |
Why use malloc/free vs new/delete?
- malloc: allocates raw memory, does not call constructors; returns void*
- new: allocates memory AND calls the constructor; type-safe
- For C++ objects, always use new/delete. Never mix (don't use free() on memory allocated with new)
What is a namespace? Why is 'using namespace std' considered bad practice? Namespace groups names to avoid conflicts. `using namespace std` pulls all of std into the global scope, which can cause naming conflicts in large codebases. Best practice: use `std::cout`, `std::vector` explicitly.
Frequently asked questions
Practice these questions on HireStepX