Interview Preparation
💼 C++ & Systems
Deep-dive into memory management, low-level pointers, and core C++ interview rounds.
No questions match your search. Try a different keyword.
All C++ & Systems Interview Questions (40 total)
The full set of C++ and systems-level interview questions from the practice tool above is listed here, covering memory management, pointers, and core language internals. Expand a question to view its complete answer.
1. What is C++ and how does it differ from C? (Easy)
C++ is a general-purpose programming language created as an extension of C, adding support for object-oriented programming, templates, exception handling, and a rich standard library. While C is purely procedural, C++ supports multiple paradigms, including procedural, object-oriented, and generic programming.
2. What is the difference between a declaration and a definition in C++? (Medium)
A declaration introduces a name and its type to the compiler, telling it that something exists, such as 'int add(int, int);' for a function. A definition provides the actual implementation or allocates storage, such as the full function body, and a declaration can appear multiple times while a definition can typically appear only once (the One Definition Rule).
3. What is the difference between the stack and the heap in C++? (Medium)
The stack is a region of memory used for static allocation of local variables and function call information, managed automatically with very fast allocation and deallocation, but limited in size. The heap is used for dynamic allocation via 'new' or malloc, offering more flexibility and larger capacity, but requires manual deallocation with 'delete' or free, and is generally slower to allocate from.
4. What is function overloading in C++? (Easy)
Function overloading allows multiple functions to share the same name as long as they have different parameter lists, either in type, number, or order. The compiler determines which version to call at compile time based on the arguments provided, which is a form of compile-time (static) polymorphism.
5. What is the difference between a header file (.h) and a source file (.cpp) in C++? (Easy)
A header file typically contains declarations, such as function prototypes, class definitions, and constants, that need to be shared across multiple source files. A source file contains the actual implementation (definitions) of those functions and is compiled independently into an object file, with header files included via #include to make declarations visible.
6. What is a template in C++ and why is it useful? (Medium)
A template allows you to write generic, type-independent functions or classes, where the actual data type is specified when the template is used, for example template <typename T> T max(T a, T b). Templates let you write one implementation that works with multiple types (int, double, custom classes) without duplicating code, forming the basis of generic programming and the STL.
7. What is the Standard Template Library (STL) in C++? (Easy)
The STL is a collection of generic, reusable template-based classes and functions in C++, organized into containers (like vector, map, and set), algorithms (like sort and find), and iterators, which provide a way to traverse containers. It allows developers to use well-tested, efficient data structures and algorithms without implementing them from scratch.
8. What is the difference between std::vector and std::array in C++? (Medium)
std::vector is a dynamically resizable array whose size can grow or shrink at runtime, with elements stored on the heap. std::array is a fixed-size array whose size must be known at compile time, stored on the stack (when declared locally), offering slightly better performance for small, unchanging collections due to lower overhead.
9. What is the difference between std::map and std::unordered_map? (Medium)
std::map stores key-value pairs in sorted order based on keys, implemented as a balanced binary search tree (typically red-black tree), giving O(log n) operations. std::unordered_map stores elements in no particular order using a hash table internally, giving average O(1) operations but no guaranteed ordering.
10. What is an iterator in the STL? (Medium)
An iterator is an object that acts like a generalized pointer, providing a uniform way to traverse elements of any STL container, such as begin() and end(). Different iterator categories (input, output, forward, bidirectional, random access) support different levels of traversal capability depending on the container they belong to.
11. What is the difference between std::vector and std::list in the STL? (Medium)
std::vector stores elements contiguously in memory, providing fast O(1) random access but slower O(n) insertion/deletion in the middle since elements must shift. std::list is a doubly linked list, providing fast O(1) insertion and deletion at any known position but only sequential O(n) access, with no direct indexing.
12. What are the four main principles of OOP as applied in C++? (Easy)
The four principles are encapsulation, bundling data and methods within a class while controlling access using access specifiers; abstraction, exposing only essential details through public interfaces while hiding implementation; inheritance, allowing a derived class to reuse and extend a base class; and polymorphism, allowing the same function call to behave differently depending on the object type, typically through virtual functions.
13. What is the difference between public, private, and protected access specifiers in C++? (Easy)
Members declared public are accessible from anywhere the object is visible. Members declared private are accessible only within the class itself, not even by derived classes. Members declared protected are accessible within the class and by any derived classes, but not from outside code, making them useful for controlled inheritance-based access.
14. What is constructor overloading in C++? (Medium)
Constructor overloading means a class can have multiple constructors with different parameter lists, allowing objects to be initialized in different ways depending on which constructor is invoked at object creation, similar to regular function overloading but specifically for the class's initialization logic.
15. What is operator overloading in C++? (Medium)
Operator overloading allows you to redefine the behavior of standard operators, such as +, -, ==, or <<, for user-defined types like classes, making objects behave more intuitively with familiar syntax, for example enabling 'obj1 + obj2' for a custom Vector class instead of requiring a named method like obj1.add(obj2).
16. What is multiple inheritance in C++ and what problem can it cause? (Medium)
Multiple inheritance allows a class to inherit from more than one base class simultaneously. It can cause the 'diamond problem,' where a class inherits from two classes that both derive from a common base class, resulting in ambiguity about which version of the inherited member to use; C++ resolves this using virtual inheritance.
17. What is a pointer in C++? (Easy)
A pointer is a variable that stores the memory address of another variable rather than a value directly. Pointers are declared using the * symbol, for example int* ptr, and are dereferenced with * to access or modify the value at the address they point to.
18. What is a null pointer and why is it useful? (Easy)
A null pointer is a pointer that does not point to any valid memory location, represented in modern C++ as nullptr. It is useful as a way to explicitly indicate that a pointer is currently 'empty' or uninitialized, and checking for it before dereferencing helps prevent undefined behavior from accessing invalid memory.
19. What is a dangling pointer? (Medium)
A dangling pointer is a pointer that still holds the address of memory that has already been freed or gone out of scope, such as a pointer to a local variable after the function returns, or to heap memory after 'delete' has been called. Dereferencing a dangling pointer results in undefined behavior and is a common source of bugs.
20. What is pointer arithmetic in C++? (Medium)
Pointer arithmetic refers to performing arithmetic operations, such as addition or subtraction, directly on pointers. Incrementing a pointer moves it forward by the size of the data type it points to, not necessarily by one byte, which is why array traversal using pointers works correctly regardless of the element's type size.
21. What is a void pointer in C++? (Medium)
A void pointer (void*) is a generic pointer type that can hold the address of any data type but cannot be dereferenced directly, since the compiler doesn't know what type of data it points to. It must be explicitly cast to a specific pointer type before the underlying data can be accessed.
22. What is a reference in C++ and how is it different from a pointer? (Medium)
A reference is an alias for an existing variable, created using the & symbol, that must be initialized at declaration and cannot be reseated to refer to a different variable afterward. A pointer, by contrast, can be reassigned to point to different variables, can be null, and requires explicit dereferencing to access the value it points to.
23. Why are references commonly used as function parameters in C++? (Medium)
Passing arguments by reference avoids the overhead of copying large objects, since the function operates directly on the original variable rather than a duplicate. It also allows the function to modify the caller's original variable when needed, unlike pass-by-value, which only operates on a local copy.
24. What is the difference between passing by value, by pointer, and by reference in C++? (Medium)
Passing by value copies the argument into the function's parameter, so changes inside the function don't affect the original. Passing by pointer passes the memory address, allowing the function to modify the original value through dereferencing, but requires explicit pointer syntax. Passing by reference achieves the same ability to modify the original but with cleaner syntax, since the reference behaves just like the original variable.
25. What is the difference between stack memory and heap memory allocation? (Medium)
Stack memory is allocated automatically for local variables when a function is called and freed automatically when the function returns, making it fast but limited in size and lifetime. Heap memory is allocated manually at runtime using 'new' (or malloc in C) and persists until explicitly freed with 'delete' (or free), giving more control and larger capacity at the cost of manual management responsibility.
26. What is a memory leak and how can it occur in C++? (Medium)
A memory leak occurs when dynamically allocated memory is no longer needed by the program but is never freed, causing the program's memory usage to grow over time and potentially exhaust available memory. In C++, this commonly happens when 'new' is called without a matching 'delete', especially if an exception or early return skips the cleanup code.
27. What are smart pointers in C++ and why are they preferred over raw pointers? (Medium)
Smart pointers, such as std::unique_ptr and std::shared_ptr, are template classes that automatically manage the lifetime of dynamically allocated objects, deleting them when they go out of scope or reference count reaches zero. They are preferred over raw pointers because they greatly reduce the risk of memory leaks and dangling pointers by tying deallocation to object lifetime (RAII).
28. What is RAII in C++? (Medium)
RAII (Resource Acquisition Is Initialization) is a C++ programming idiom where a resource, such as memory, a file handle, or a lock, is acquired in an object's constructor and automatically released in its destructor. This ties resource lifetime to object scope, ensuring cleanup happens automatically even if an exception is thrown, which underlies tools like smart pointers.
29. What is a virtual function in C++? (Medium)
A virtual function is a member function declared in a base class with the 'virtual' keyword that can be overridden in a derived class. It enables runtime polymorphism, ensuring that the correct derived-class version of the function is called when accessed through a base class pointer or reference, based on the object's actual type rather than the pointer's declared type.
30. What is a pure virtual function and what does it make a class? (Medium)
A pure virtual function is a virtual function declared with '= 0' and has no implementation in the base class, forcing any derived class to provide its own implementation. A class containing at least one pure virtual function becomes an abstract class, which cannot be instantiated directly and serves purely as an interface or base contract.
31. What is a virtual destructor and why is it important in C++? (Medium)
A virtual destructor ensures that when an object is deleted through a base class pointer, the correct derived class destructor is called first, followed by the base class destructor, properly cleaning up all resources. Without a virtual destructor, deleting a derived object through a base pointer results in undefined behavior, typically only calling the base class's destructor and causing resource or memory leaks.
32. What is the difference between function overriding and function hiding in C++? (Hard)
Function overriding occurs when a derived class provides a new implementation for a base class's virtual function with an identical signature, enabling runtime polymorphism. Function hiding occurs when a derived class declares a non-virtual function or a function with a different signature that shares a name with a base class function, which hides the base version entirely for that derived class rather than overriding it.
33. What is the difference between a process and a thread? (Medium)
A process is an independent, self-contained program in execution with its own separate memory space, resources, and address space. A thread is a smaller unit of execution within a process that shares the same memory space and resources as other threads in that process, making thread creation and context switching generally lighter-weight than process creation.
34. What is a deadlock in operating systems? (Medium)
A deadlock occurs when two or more processes or threads are each waiting for a resource that the other holds, resulting in all of them being permanently blocked with no progress possible. Deadlocks typically require four conditions to occur simultaneously: mutual exclusion, hold and wait, no preemption, and circular wait.
35. What is virtual memory and why do operating systems use it? (Medium)
Virtual memory is a memory management technique that gives each process the illusion of having its own large, contiguous address space, even though physical RAM may be smaller and fragmented. It allows the OS to run programs larger than physical memory by swapping data between RAM and disk, and it isolates processes from each other for security and stability.
36. What is the difference between multitasking and multithreading? (Medium)
Multitasking refers to an operating system's ability to run multiple processes (independent programs) concurrently by rapidly switching the CPU between them. Multithreading refers to running multiple threads within a single process concurrently, sharing that process's memory space, which allows for more efficient communication and resource sharing between the concurrent units of work.
37. What is a context switch in operating systems? (Medium)
A context switch is the process by which the CPU stores the state of a currently running process or thread and loads the saved state of another one, allowing multiple tasks to share a single CPU core. While necessary for multitasking, context switches introduce overhead since saving and restoring state takes time that isn't spent doing useful work.
38. What is the difference between paging and segmentation in memory management? (Hard)
Paging divides memory into fixed-size blocks called pages, which simplifies allocation and avoids external fragmentation but can cause internal fragmentation within a page. Segmentation divides memory into variable-sized segments based on logical program units, such as code, stack, or data, which aligns more naturally with program structure but can suffer from external fragmentation.
39. What is a race condition? (Medium)
A race condition occurs when two or more threads or processes access shared data concurrently, and the final outcome depends on the unpredictable timing or order of their execution. It often leads to inconsistent or incorrect results and is typically prevented using synchronization mechanisms like mutexes, locks, or semaphores.
40. What is the difference between a mutex and a semaphore? (Hard)
A mutex (mutual exclusion lock) allows only one thread to access a critical section at a time and is typically owned and released by the same thread that locked it. A semaphore maintains a counter that can allow a specified number of threads to access a resource simultaneously, and it can be signaled or waited on by threads other than the one that acquired it, making it more flexible for managing limited resource pools.
CodeMaster Academy