Basic Questions
What is C++?
- C++ is a general-purpose programming language created by Bjarne Stroustrup as an extension of the C programming language. It includes object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
What are the main features of C++?
- Object-Oriented Programming
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
- Standard Template Library (STL)
- Exception Handling
What is the difference between C and C++?
- C is a procedural programming language, while C++ is both procedural and object-oriented. C++ supports classes and objects, while C does not.
What is a class?
- A class is a blueprint for creating objects. It encapsulates data for the object and methods to manipulate that data.
What is an object?
- An object is an instance of a class. It contains data and methods defined in the class.
Intermediate Questions
What is inheritance in C++?
- Inheritance is a feature of OOP that allows a class to inherit properties and behaviors (methods) from another class.
What is polymorphism in C++?
- Polymorphism allows functions to be used interchangeably based on the context. It can be achieved using function overloading, operator overloading, and virtual functions.
Explain the concept of constructors and destructors in C++.
- Constructors are special member functions that initialize objects. Destructors are used to clean up when an object is destroyed.
What is the difference between a pointer and a reference in C++?
- A pointer can be re-assigned to point to different objects, whereas a reference is a constant alias for an existing object.
What are virtual functions?
- Virtual functions allow derived classes to override methods in the base class, enabling runtime polymorphism.
Advanced Questions
What is the Standard Template Library (STL)?
- STL is a collection of template classes and functions in C++ for data structures and algorithms, including vectors, lists, queues, and stacks.
What is a template in C++?
- A template is a feature that allows functions and classes to operate with generic types. This enables code reusability.
Explain the concept of RAII (Resource Acquisition Is Initialization).
- RAII is a programming idiom where resource allocation is tied to object lifetime, ensuring resources are properly released.
What is a smart pointer in C++?
- Smart pointers are objects that manage the lifetime of dynamically allocated memory, ensuring automatic memory management.
What is the difference between deep copy and shallow copy?
- A shallow copy copies all the member values, whereas a deep copy duplicates everything, including dynamically allocated memory.
Memory Management
What is a memory leak?
- A memory leak occurs when dynamically allocated memory is not released back to the system, causing a gradual loss of available memory.
How do you prevent memory leaks in C++?
- By using smart pointers or ensuring every
new
has a correspondingdelete
.
- By using smart pointers or ensuring every
What is the difference between
malloc
andnew
?malloc
is a C function for memory allocation, whilenew
is a C++ operator that also invokes constructors.
What is a dangling pointer?
- A dangling pointer is a pointer that references a memory location that has already been deallocated.
Explain the use of the
delete
operator.- The
delete
operator deallocates memory allocated by thenew
operator and invokes the destructor.
- The
Object-Oriented Programming
What is encapsulation?
- Encapsulation is the bundling of data and methods that operate on that data within a single unit or class.
What is abstraction?
- Abstraction hides complex implementation details and exposes only the necessary and relevant features of an object.
What is operator overloading?
- Operator overloading allows custom implementation of operators for user-defined types.
What is multiple inheritance?
- Multiple inheritance is when a class inherits from more than one base class.
What are pure virtual functions?
- Pure virtual functions are declared in a base class and must be implemented by derived classes, making the base class abstract.
Miscellaneous
What is the use of the
friend
keyword?- The
friend
keyword allows a function or another class to access private and protected members of the class in which it is declared.
- The
What is a namespace in C++?
- A namespace is a declarative region that provides scope to the identifiers inside it, preventing name conflicts.
Explain the
static
keyword in C++.static
can be used for variables and functions. Static variables retain their value between function calls, and static functions are limited to the file scope.
What is the
this
pointer?- The
this
pointer is an implicit pointer passed to non-static member functions of a class, pointing to the object for which the function is called.
- The
What is a lambda function?
- A lambda function is an anonymous function defined using the
[]
syntax, which can capture variables from the surrounding scope.
- A lambda function is an anonymous function defined using the
Design Patterns
What is a singleton pattern?
- The singleton pattern ensures that a class has only one instance and provides a global point of access to it.
What is the factory pattern?
- The factory pattern defines an interface for creating objects but lets subclasses alter the type of objects that will be created.
What is the observer pattern?
- The observer pattern allows a subject to notify observers about changes without them tightly coupling.
What is the strategy pattern?
- The strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable.
What is the command pattern?
- The command pattern encapsulates a request as an object, allowing parameterization of clients with different requests.
Multithreading
What is multithreading?
- Multithreading allows concurrent execution of two or more threads for maximum utilization of CPU.
What are the different ways to create threads in C++?
- Using
std::thread
from the C++11 standard library, using POSIX threads (pthreads), or using platform-specific APIs.
- Using
What is a race condition?
- A race condition occurs when the behavior of software depends on the timing or sequence of uncontrollable events like thread execution.
How do you avoid race conditions?
- By using synchronization mechanisms like mutexes, locks, and atomic operations.
What is a deadlock?
- A deadlock is a situation where two or more threads are blocked forever, waiting for each other to release resources.
Templates and STL
What are the different types of STL containers?
- Sequence containers (vector, list, deque)
- Associative containers (set, map, multiset, multimap)
- Container adapters (stack, queue, priority_queue)
What is the difference between
std::vector
andstd::list
?std::vector
is a dynamic array providing random access, whilestd::list
is a doubly linked list providing efficient insertions/deletions.
What are iterators?
- Iterators are objects that point to an element inside a container and can iterate through the container.
What is the
std::unique_ptr
?std::unique_ptr
is a smart pointer that owns and manages another object through a pointer and disposes of that object when theunique_ptr
goes out of scope.
What is the
std::shared_ptr
?std::shared_ptr
is a smart pointer that maintains a reference count to manage the lifetime of the object it points to.
Best Practices
What is the Rule of Three?
- If a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.
What is the Rule of Five?
- Extends the Rule of Three to include move constructor and move assignment operator for better management of resources.
What is a move constructor?
- A move constructor transfers resources from a temporary object to a new object, leaving the temporary in a valid but unspecified state.
What is the purpose of the
explicit
keyword?- The
explicit
keyword is used to prevent implicit conversions and copy-initialization.
- The
What is the difference between
throw
andnoexcept
?throw
specifies that a function might throw an exception, whilenoexcept
specifies that a function does not throw exceptions.
Additional Questions
What is the difference between
const
andconstexpr
?const
declares a variable as constant, whileconstexpr
ensures that a variable or function can be evaluated at compile-time.
What are user-defined literals?
- User-defined literals allow you to extend the language with custom literals by defining special operator functions.
What is
decltype
used for?decltype
inspects the declared type of an expression, providing the type without evaluating the expression.
What are
type_traits
?type_traits
is a library in C++ that provides a set of templates to perform compile-time type checking and transformations.
What is a functor in C++?
- A functor is an object that can be called as if it were a function, typically by overloading the
operator()
.
- A functor is an object that can be called as if it were a function, typically by overloading the
What are lambda expressions useful for?
- Lambda expressions provide a concise way to define anonymous function objects directly within the scope where they are invoked.
What is the purpose of the
override
keyword?- The
override
keyword ensures that a member function is overriding a virtual function in a base class, providing better code clarity and compiler checking.
- The
What is the purpose of the
final
keyword?- The
final
keyword prevents a class from being inherited or a virtual function from being overridden.
- The
What is the difference between
std::sort
andstd::stable_sort
?std::sort
provides faster sorting but does not guarantee the relative order of equal elements, whilestd::stable_sort
maintains the relative order of equal elements.
What is the use of
std::tie
?std::tie
is used to unpack tuples into individual variables, often used withstd::tuple
to assign values.