C++ interview questions.
What is the difference between '== operator' and 'equals()' in C++?
'==' compares values, while 'equals()' is a method used for comparing object equality.
What is the purpose of the 'alignas' keyword in C++?
'alignas' specifies the alignment requirement for a variable or type.
What is the role of the 'decltype' keyword in C++?
'decltype' is used to query the type of an expression at compile time.
What is the purpose of the 'extern' keyword in C++?
'extern' is used to declare a variable that is defined in another translation unit.
What is the use of the 'friend' keyword in C++?
'friend' allows a function or another class to access private and protected members of a class.
What is the role of the 'mutable' keyword in C++?
'mutable' allows a member of a class to be modified even if the object is const.
What is the difference between 'new' and 'malloc' in C++?
'new' initializes objects and calls constructors, while 'malloc' allocates raw memory without initialization.
What is the purpose of the 'noexcept' specifier in C++?
'noexcept' indicates that a function does not throw exceptions.
What is the purpose of the 'override' keyword in C++?
'override' indicates that a method is intended to override a virtual method in a base class.
What is the difference between 'public', 'protected', and 'private' access specifiers?
'public' allows access from anywhere, 'protected' allows access in derived classes, and 'private' restricts access to the class itself.
What is the purpose of the 'sizeof' operator in C++?
'sizeof' returns the size, in bytes, of a data type or object.
What is the purpose of the 'static' keyword in C++?
'static' indicates that a variable retains its value between function calls or limits the visibility of a variable to its translation unit.
What is the difference between 'static_cast' and 'dynamic_cast'?
'static_cast' is used for compile-time type checking, while 'dynamic_cast' is used for safe downcasting in inheritance hierarchies.
What is the difference between 'std::list' and 'std::vector'?
'std::list' is a doubly linked list, allowing efficient insertions and deletions, while 'std::vector' is a dynamic array optimized for random access.
What is the purpose of the 'std::map' in C++?
'std::map' is an associative container that stores elements in key-value pairs, allowing fast retrieval based on keys.
What is the purpose of the 'std::shared_ptr' in C++?
'std::shared_ptr' is a smart pointer that allows multiple pointers to share ownership of an object.
What is the role of the 'std::unique_ptr' in C++?
'std::unique_ptr' is a smart pointer that maintains exclusive ownership of an object, automatically deleting it when no longer needed.
What is the difference between 'std::vector' and 'std::array'?
'std::vector' is a dynamic array that can change size, while 'std::array' is a fixed-size array.
What is the purpose of the 'this' pointer in C++?
'this' pointer refers to the current object instance within a class's member function.
What is the difference between 'throw' and 'throws' in C++?
'throw' is used to signal an exception, while 'throws' is not a keyword in C++.
What is the purpose of the 'volatile' keyword in C++?
'volatile' indicates that a variable's value may change unexpectedly, preventing compiler optimizations.
What is the role of the 'volatile' keyword in C++?
'volatile' tells the compiler that a variable may be changed by something outside the control of the program.
What is the difference between deep copy and shallow copy?
A shallow copy creates a new object but copies the values of the original object's fields, which can lead to shared references. A deep copy creates a new object and recursively copies all objects referenced by the original object, ensuring that modifications to the copied object do not affect the original object.
What is a virtual destructor in C++?
A virtual destructor ensures that the destructor of derived classes is called when an object is deleted through a base class pointer.
What is a virtual function in C++?
A virtual function is a member function in a base class that you expect to override in derived classes. When you use a base class pointer to refer to a derived class object, the virtual function ensures that the correct function is called for that object, enabling polymorphism. It is declared using the 'virtual' keyword.
What is encapsulation in C++?
Encapsulation is the bundling of data and methods that operate on that data within a single unit or class.
What is exception handling in C++?
Exception handling is a mechanism to handle runtime errors using try, catch, and throw keywords.
What is the difference between a class and a struct in C++?
In C++, a class is a user-defined type that can encapsulate data and functions, with default access specifiers being private. A struct is similar but has public access by default. Classes are typically used for encapsulation and data hiding, while structs are used for simpler data structures.
What is the difference between a class and a struct in C++?
In C++, classes default to private access while structs default to public access.
What is inheritance in C++?
Inheritance is a feature that allows a class to inherit properties and behaviors from another class.
Explain the concept of inheritance in C++.
Inheritance is a fundamental concept in C++ that allows a class (derived class) to inherit properties and behaviors (methods) from another class (base class). This promotes code reusability and establishes a hierarchical relationship between classes. Types of inheritance include single, multiple, hierarchical, and multilevel inheritance.
What is a multithreading in C++?
Multithreading allows multiple threads to execute concurrently, improving performance and responsiveness.
What is a namespace in C++?
Namespaces are used to organize code and prevent name collisions in larger projects.
What is operator overloading in C++?
Operator overloading allows developers to redefine the way operators work for user-defined types.
What is polymorphism in C++?
Polymorphism allows methods to do different things based on the object it is acting upon, typically through method overriding.
Explain RAII (Resource Acquisition Is Initialization) in C++.
RAII is a programming idiom in C++ where resource allocation is tied to object lifetime. Resources are acquired during object creation (constructor) and released during object destruction (destructor). This ensures that resources are properly managed and released, preventing memory leaks and dangling pointers.
What is the Standard Template Library (STL) in C++?
STL is a collection of template classes and functions for data structures and algorithms.
What is the difference between deep copy and shallow copy?
Shallow copy copies the object's reference, while deep copy creates a new object and copies all values.
What are smart pointers in C++?
Smart pointers are objects that manage memory and ensure proper deallocation, preventing memory leaks.
What are smart pointers in C++?
Smart pointers are wrapper classes that manage the lifetime of dynamically allocated objects, ensuring proper resource management and preventing memory leaks. The two most common types are 'std::unique_ptr', which ensures exclusive ownership, and 'std::shared_ptr', which allows shared ownership among multiple pointers.
What is the difference between stack and heap memory?
Stack memory is managed automatically and has a limited size, while heap memory is managed manually and can grow dynamically.
What is a template in C++?
Templates allow functions and classes to operate with generic types, enabling code reuse.
What is the significance of the 'const' keyword in C++?
The 'const' keyword indicates that a variable's value cannot be changed after initialization.
What is the purpose of the 'const' keyword in C++?
The 'const' keyword is used to define constants, indicating that a variable's value cannot be modified after initialization. It can be applied to variables, pointers, and member functions to enforce immutability, which helps prevent accidental changes and enhances code safety.
What is the use of the 'friend' keyword in C++?
The 'friend' keyword allows a function or another class to access private and protected members of a class. This is useful for operator overloading or when two classes need to work closely together while still maintaining encapsulation.
What is the role of the preprocessor in C++?
The preprocessor handles directives before compilation, such as including files and defining macros.
What are virtual functions in C++?
Virtual functions allow derived classes to override methods in base classes, enabling dynamic polymorphism.
What is a pure virtual function in C++?
A pure virtual function is a function declared in a base class that has no definition and must be overridden in derived classes.
What is a reference in C++?
A reference is an alias for another variable, allowing indirect access to the variable's value.
What is a lambda expression in C++?
A lambda expression is an anonymous function that can capture variables from its surrounding scope.
What is a move constructor in C++?
A move constructor transfers resources from a temporary object to a new object, optimizing resource management.
What is a circular dependency in C++?
A circular dependency occurs when two or more modules depend on each other, leading to compilation issues.
What is a constructor in C++?
A constructor is a special member function that is automatically called when an object of a class is created.
What is a copy constructor in C++?
A copy constructor is a special constructor that initializes an object using another object of the same class.
What is a destructor in C++?
A destructor is a special member function that is called when an object is destroyed, used to free resources.
What is a function pointer in C++?
A function pointer is a variable that stores the address of a function, allowing dynamic function calls.
What are the basic features of C++?
C++ features include object-oriented programming, encapsulation, inheritance, polymorphism, and abstraction.
What is C++?
C++ is a high-level, object-oriented programming language that is an extension of the C programming language. It supports features like classes, inheritance, polymorphism, encapsulation, and abstraction, making it suitable for system/software development and game programming.
What is the difference between C++ and C?
C++ is an object-oriented programming language, while C is a procedural programming language.
What are constructors and destructors in C++?
Constructors are special member functions that are called when an object of a class is created, used to initialize object attributes. Destructors are called when an object is destroyed, used to free resources allocated to the object. Constructors can be overloaded, while destructors cannot.
