This article summarizes key C and C++ interview questions frequently encountered during graduate school admissions (both guaranteed admission and entrance exams) and technical interviews. It is also helpful for job seekers.
1. Pointer Array vs. Array Pointer
A pointer array is an array where each element is a pointer. For example, int *ptrArray[5] declares an array of five integer pointers.
An array pointer is a pointer to an array. For instance, int (*ptr)[5] declares a pointer to an array of five integers.
2. Structure vs. Union
In a structure, each member occupies its own distinct memory space, allowing multiple values to exist simultaneously.
In a union, all members share the same memory region. Modifying one member overwrites the others.
3. How Pointers Work at the Low Level
Pointers store memory addresses. Each variable has a unique address in memory; a pointer holds that address and can be used to access the variable's value indirectly.
4. Pointer vs. Reference
- Syntax: A pointer is a variable storing another variable's address, declared with
*. A reference is an alias for another variable, declared with&. - Memory Management: Pointers can be reassigned to point to different variables or to
nullptr, and support dynamic allocation withnew/delete. References cannot be reassigned after initialization; they always refer to their original target. - Nullability: Pointers can be null. References must be initialized to a valid object and cannot be null.
5. malloc vs. new
| Feature | malloc (C) |
new (C++) |
|---|---|---|
| Type | Library function | Operator / keyword |
| Memory only | Allocates raw memory | Allocates memory and calls constructor |
| Return type | void* (requires cast) |
Typed pointer |
| Failure | Returns NULL |
Throws std::bad_alloc exception |
| Size calculation | Manual (sizeof) |
Automatic |
6. C++ Templates and Their Benefits
Templates enable generic programming by parameterizing types or values.
- Function template:
template <typename T> T max(T a, T b) { ... } - Class template:
template <typename T> class Container { ... };
Benefits:
- Write reusable code for any type.
- Implement generic algorithms and containers (e.g.,
std::vector,std::sort). - Avoid code duplication through template instantiation.
7. std::vector Under the Hood and Growth Strategy
std::vector manages a contiguous dynamic array. It tracks size (number of elements) and capacity (allocated memory). When size == capacity during insertion, the vector:
- Allocates a new block of memory (typically 1.5× or 2× the old capacity).
- Copies (or moves) existing elements to the new memory.
- Deallocates the old memory.
Similarly, std::unordered_map uses a hash table with chaining. When the load factor exceeds a threshold (default 1.0), it rehashes into a larger bucket array (≈2× size).
8. Shallow Copy vs. Deep Copy
- Shallow copy: Only copies the pointer value, so both objects share the same dynamically allocated memory. Destructors cause double free issues.
- Deep copy: Allocates new memory and copies the pointed-to data, giving each object independent ownership.
9. Constructors and Destructors
Constructor:
- Same name as class, no return type.
- Called automatically upon object creation.
- Can be overloaded.
- Initializes member variables and acquires resources.
Destructor:
- Same name as class prefixed with
~, no return type. - Called automatically when object goes out of scope or is deleted.
- Performs cleanup (e.g., release memory, close files).
- Cannot be overloaded and takes no arguments.
10. Polymorphism
Polymorphism allows the same interface to exhibit different behaviors for different types.
Benefits:
- Code reuse via base class interfaces.
- Extensibility: new derived classes can be added without modifying existing code.
- Readability: uniform interface reduces complexity.
- Flexibility: runtime selection of implementation.
Forms in C++:
- Overload polymorphism (function/operator overloading)
- Coercion polymorphism (implicit type conversion)
- Parametric polymorphism (templates)
- Inclusion polymorphism (inheritance + virtual functions)
11. Virtual Functions
A virtual function is declared in a base class with the virtual keyword, and can be overridden in derived classes. When called via a base class pointer or reference, the most-derived override is executed (dynamic dispatch).
Benefits:
- Enables runtime polymorphism.
- Supports dynamic binding.
- Override to customize behavior without affecting other derived classes.
Follow-up: Have you used virtual functions in a project? (Answer based on your experience.)
12. Pure Virtual Functions
A pure virtual function has no implementation in the base class, declared with = 0:
virtual void draw() = 0;
Characteristics:
- The class becomes abstract (cannot be instantiated).
- Derived classes must provide an implementation.
- Serves as an interface contract.
13. Returning Multiple Values from a Function
- Use global variables.
- Return a struct/class containing all values.
- Pass parameters by reference (
&). - Pass parameters by pointer:
void compute(int* a, float* b) {
*a = 42;
*b = 3.14f;
}
14. Stack Overflow Causes
- Excessive local variables or large local arrays.
- Unbounded recursion (no termination condition).
- Deep call chain (many nested function calls).
15. Memory Layout of a C/C++ Program
- Stack: Automatic storage for function parameters, local variables, return addresses. Managed by compiler. Fast but limited size.
- Heap: Dynamic memory allocated via
malloc/freeornew/delete. Programmer must manage. Fragmented. - Static/Global: Stores global and static variables. Lifetime = entire program run.
- Constant/Literal: Read-only data (e.g., string literals).
- Code: Compiled machine instructions.
16. C vs. C++
| Feature | C | C++ |
|---|---|---|
| Paradigm | Procedural | Multi-paradigm (procedural + OOP) |
| Classes & OOP | No | Yes (inheritance, polymorphism, etc.) |
| Standard Library | Minimal | Rich (STL containers, algorithms) |
| Namespaces | No | Yes |
| Exception Handling | No | try/catch/throw |
| Operator Overloading | No | Yes |
17. C++ vs. Python (Low-Level Differences)
| Aspect | C++ | Python |
|---|---|---|
| Execution | Compiled to native machine code | Interpreted (bytecode + VM) |
| Typing | Static | Dynamic |
| Memory | Manual (new/delete) | Automatic (garbage collector) |
| Performance | Generally faster | Slower, but can use C extensions |
18. C++ vs. Java
| Feature | C++ | Java |
|---|---|---|
| Paradigm | Object-oriented + procedural | Pure object-oriented |
| Platform | Compiled per platform | Bytecode runs on JVM (cross-platform) |
| Memory | Manual manageemnt | Automatic garbage collection |
| Exception | User-defined exceptions allowed | Strict exception hierarchy |
| Speed | Usually faster | JIT optimization can approach C++ |
| Use Cases | Systems, games, embedded | Enterprise web, Android apps |
19. Polymorphism in C++ vs. Java
- C++ requires the
virtualkeyword for dynamic dispatch; Java treats all non-static methods as virtual. - C++ polymorphism works via pointers/references; Java uses references universally.
- C++ uses abstract classes (pure virtual functions); Java uses
interfacekeywords.
20. Python vs. Java
| Aspect | Python | Java |
|---|---|---|
| Syntax | Indentation-basedd, concise | Braces, verbose |
| Type System | Dynamic, duck typing | Static, strong |
| Execution | Interpreted | Compiled to bytecode |
| Memory | Auto (GC) | Auto (GC) |
| Common Uses | Data science, scripting, web | Enterprise, Android, large systems |
| Ecosystem | NumPy, Pandas, Django, etc. | Spring, Hibernate, Maven, etc. |
21. Procedural vs. Object-Oriented Programming
Procedural: Focus on functions/procedures that operate on data. Linear flow.
Object-Oriented: Orgenizes code into objects that bundle data and behavior.
- Inheritance: Reuse base class functionality.
- Encapsulation: Hide internal state, expose safe interface.
- Polymorphism: One interface, many implementations.
- Abstraction: Simplify complexity by modeling real-world entities.