Q1. What is the difference between C and C++? C is a procedural language. C++ supports both procedural and object-oriented paradigms. C++ introduces classes, references, function overloading, templates, and exception handling.
Q2. What is the difference between a pointer and a reference?
- A pointer has its own memory address and can be reassigned or point to
nullptr. - A reference is an alias to an existing variable. It must be initialized when declared and cannot be reassigned or be null.
Q3. What is the difference between new/delete and malloc/free?
newcalls the object's constructor.mallocdoes not.deletecalls the object's destructor.freedoes not.newthrows an exception on failure,mallocreturnsNULL.
Q4. Explain Polymorphism in C++. Polymorphism allows objects of different types to be treated as objects of a common base class. Run-time polymorphism is achieved using pointers/references to base classes and virtual functions.
Q5. What is a Virtual Function and a V-Table? A virtual function allows derived classes to override base class behavior. The compiler implements this using a Virtual Table (V-Table)—an array of function pointers created for any class containing virtual functions, used to resolve function calls at runtime (Dynamic Binding).
Q6. What is an Abstract Class / Pure Virtual Function?
A pure virtual function is declared as virtual void func() = 0;. A class with at least one pure virtual function is an Abstract Class and cannot be instantiated.
Q7. What is RAII? Resource Acquisition Is Initialization. It's a C++ paradigm where resources (memory, file handles, locks) are acquired in a constructor and released in a destructor, ensuring resources are never leaked if an exception is thrown.
Q8. What is the Rule of Three / Five? If a class requires a user-defined destructor (usually because it manages raw pointers), it almost certainly requires a user-defined copy constructor and copy assignment operator (Rule of Three). C++11 adds move constructor and move assignment (Rule of Five).
Q9. What are Smart Pointers?
Smart pointers (unique_ptr, shared_ptr, weak_ptr) act like raw pointers but automatically manage memory (via RAII) to prevent memory leaks and dangling pointers.
Q10. Explain the auto keyword (C++11).
auto tells the compiler to automatically deduce the type of a variable from its initializer. It makes code cleaner, especially with complex STL iterators.
Q11. What is an Rvalue Reference and Move Semantics?
An Rvalue reference (&&) refers to a temporary object. Move semantics (std::move) allows you to transfer ownership of resources (like dynamically allocated memory) from a temporary object to a new object, completely avoiding the overhead of deep copying.
Q12. What is a Lambda Expression?
A Lambda is an inline, anonymous function. It consists of a capture block [], parameters (), and a body {}. They are heavily used as predicates for STL algorithms like std::sort.
Q13. How do you prevent a Race Condition in C++ Multithreading?
A race condition occurs when multiple threads access and modify shared data simultaneously. To prevent it, you use a std::mutex. For safe, RAII-style locking, you wrap the mutex in a std::lock_guard, which automatically locks upon creation and unlocks when going out of scope.
Q14. What does the friend keyword do?
A friend function or class is granted access to the private and protected members of the class in which it is declared. It is commonly used when overloading the << operator for output streams.
Q15. What is the "Diamond Problem" in C++?
It occurs in Multiple Inheritance when a class inherits from two classes that both inherit from the same Base class (forming a diamond shape). This creates ambiguity because the final derived class inherits two copies of the base class variables. It is solved using Virtual Inheritance (e.g., class Derived : virtual public Base).
Q16. What is an inline function?
An inline function is a request to the compiler to insert the function's body directly into the calling code instead of performing a traditional function call jump. It is used to eliminate function call overhead for very small, frequently used methods.
Q17. What are the three main types of Constructors?
- Default: Takes no arguments.
- Parameterized: Takes arguments to initialize member variables.
- Copy Constructor: Takes a reference to an object of the same class to create a new object as a copy (
MyClass(const MyClass& other)).