C++ is a wildly popular, general-purpose programming language that supports procedural, object-oriented, and generic programming paradigms. It is heavily used in software infrastructure and resource-constrained applications. In the context of a tech interview, questions about C++ measure a candidate’s understanding of syntax, memory management, data structures, object-oriented programming, STL library, and the efficient use of various programming paradigms provided by the language.
C++ Basics and Language Features
- 1.
What are the main features of C++?
Answer:C++ is renowned for its diverse set of features, many of which it inherited from C but also enhanced and expanded upon. These features enable a wide range of programming paradigms, from procedural to object-oriented and generic programming.
Key Features
Efficiency
- Low-Level Access: Pointers, direct memory manipulation.
- Resource Management: Smart pointers, deterministic destructors with RAII.
- Inline Functions: Eliminates function call overhead.
- Move Semantics: Transfers resources rather than copying.
Flexibility in Types
- Strongly-Typed: Encourages type safety.
- Type Inference:
autokeyword deduces types. - Concepts (C++20): Constraints and requirements for templates.
Multiple Paradigms
- Procedural: Functions and control structures.
- OOP Support: Classes, objects, inheritance, polymorphism.
- Functional Features: Lambdas,
constexpr, immutability. - Generic Programming: Templates and concepts.
Standard Library
- Rich in Utilities: Data structures, algorithms, I/O facilities.
- Modularity: Components like
<algorithm>encourage algorithmic abstraction. - Ranges (C++20): Composable algorithm sequences.
Code Reusability
- Inheritance: Base classes and derived classes.
- Composition: Building classes from other objects.
- Association: Member objects and relationships.
Polymorphism
- Compile-Time: Templates and concepts.
- Run-Time: Virtual functions, dynamic_cast.
Memory Management
- Stack vs. Heap: Automatic storage (
stack), dynamic memory via pointers. - Smart Pointers: Ownership-aware pointers (
unique_ptr,shared_ptr,weak_ptr). - RAII: Resource Acquisition Is Initialization.
Modern Features
- Coroutines (C++20): Simplified asynchronous programming.
- Modules (C++20): Improved code organization and compilation.
- Constexpr: Compile-time computation.
Code Example: Features in Action
#include <iostream> #include <vector> #include <memory> #include <concepts> template <typename T> concept Numeric = std::is_arithmetic_v<T>; template <Numeric T> T addOne(const T& val) { return val + 1; } struct Animal { virtual void speak() const = 0; virtual ~Animal() = default; }; struct Dog : public Animal { void speak() const override { std::cout << "Woof!" << std::endl; } }; int main() { std::vector<int> vec { 1, 2, 3 }; for (const auto& num : vec) { std::cout << addOne(num) << std::endl; } auto dog = std::make_unique<Dog>(); dog->speak(); // C++20 feature: constexpr vector constexpr std::vector<int> constexpr_vec{1, 2, 3, 4, 5}; static_assert(constexpr_vec.size() == 5); return 0; } - 2.
Explain the difference between C and C++.
Answer: - 3.
What is object-oriented programming in C++?
Answer: - 4.
What are the access specifiers in C++ and what do they do?
Answer: - 5.
Explain the concept of namespaces in C++.
Answer: - 6.
What is the difference between ‘struct’ and ‘class’ in C++?
Answer: - 7.
What is a constructor and what are its types in C++?
Answer: - 8.
Explain the concept of destructors in C++.
Answer: - 9.
What is function overloading in C++?
Answer: - 10.
What is operator overloading in C++?
Answer:
Memory Management and Pointers
- 11.
What is dynamic memory allocation in C++?
Answer: - 12.
Explain the difference between ‘new’ and ‘malloc()’.
Answer: - 13.
What is a memory leak and how can it be prevented?
Answer: - 14.
What is a dangling pointer?
Answer: - 15.
Explain the concept of smart pointers in C++.
Answer: