Interview Questions

1) Explain constructor and destructor.


  • Constructor: Initializes objects; same name as class, no return type.
  • Destructor: Cleans up resources; same name as class with a ~, no return type.

2) What are classes and objects?


  • Class: Blueprint for creating objects.
  • Object: Instance of a class that holds data and methods.

3) What is a virtual function?


A function declared in a base class that can be overridden in a derived class. Enables runtime polymorphism.

4) What is the difference between a struct and a class?


  • Struct: Default access is public.
  • Class: Default access is private.

5) What is the difference between compile-time and runtime polymorphism?


  • Compile-time: Achieved using function overloading and operator overloading.
  • Runtime: Achieved using virtual functions.

6) What is a pure virtual function?


A virtual function with no implementation (= 0). Used in abstract classes.

7) What is the difference between delete and delete[]?


  • delete: Deallocates memory for a single object.
  • delete[]: Deallocates memory for an array.

8) What is an abstract class?


A class containing at least one pure virtual function. It cannot be instantiated.

9) What is the "this" pointer?


A pointer that refers to the current object in a class.

10) What is the difference between malloc() and new?


  • malloc(): Allocates memory, requires free() to deallocate.
  • new: Allocates memory and calls the constructor, requires delete to deallocate.

11) What is the difference between public, protected, and private?


  • Public: Accessible everywhere.
  • Protected: Accessible in derived classes.
  • Private: Accessible only within the class.

12) What is a friend function?


A function that can access private and protected members of a class. Declared using the friend keyword.

13) What is C++?


C++ is a general-purpose programming language developed by Bjarne Stroustrup. It is an extension of the C language with object-oriented features like classes and objects.

14) What are the main features of C++?


  • Object-Oriented Programming (OOP)
  • Encapsulation
  • Polymorphism
  • Inheritance
  • Abstraction
  • Low-level and high-level programming features
  • Standard Template Library (STL)

15) What is the difference between C and C++?


  • C: Procedural programming language.
  • C++: Supports both procedural and object-oriented programming.

16) What is the difference between shallow copy and deep copy?


  • Shallow copy: Copies only pointers, not the data they point to.
  • Deep copy: Copies actual data and allocates separate memory.

17) What are the types of inheritance in C++?


  • Single
  • Multiple
  • Multilevel
  • Hierarchical
  • Hybrid

18) What is the difference between overloading and overriding?


  • Overloading: Same function name, different parameters.
  • Overriding: Same function name, same parameters, but different implementation in a derived class

19) What is the Standard Template Library (STL)?


A collection of template classes and functions for data structures and algorithms (e.g., vector, map, set, etc.).

20) What is a namespace?


A declarative region for grouping identifiers to avoid naming conflicts. Example: std.

21) What is the difference between pointers and references?


  • Pointer: Stores memory address and can be null.
  • Reference: Alias for an existing variable, cannot be null.

22) What is exception handling in C++?


Mechanism to handle runtime errors using try, catch, and throw.

23) What are smart pointers?


Classes that manage memory automatically. Types:

  • unique_ptr
  • shared_ptr
  • weak_ptr

24) What is the difference between const and constexpr?


  • const: Compile-time constant, but evaluated at runtime.
  • constexpr: Compile-time constant and evaluated at compile time.

25) What are function templates?


Functions that work with any data type, defined using template keyword.

26) What is the difference between map and unordered_map?


  • map: Ordered key-value pairs, implemented using trees.
  • unordered_map: Unordered key-value pairs, implemented using hash tables.

27) What is a virtual destructor?


A destructor in a base class declared as virtual to ensure proper cleanup of derived class objects.

28) What is RAII?


Resource Acquisition Is Initialization, a design pattern to manage resource allocation and deallocation.

29) How is dynamic polymorphism implemented?


Using virtual functions and a vtable (virtual table).

30) What is the difference between ++i and i++?


  • ++i: Pre-increment, increments and returns the value.
  • i++: Post-increment, returns the value before incrementing.