Pointers and References
What is the key difference between a pointer and a reference in C++?
- A pointer must be initialized, while a reference cannot be re-assigned.
- A reference can be null, while a pointer cannot.
- A pointer can be re-assigned to point to a different object, while a reference cannot be re-seated after initialization.
- There is no difference; pointers and references are identical.
- A referance can be re-assigned to point to a different object, while a pointer cannot.
Virtual Functions
What is the primary purpose of using virtual functions in C++?
- To improve compile-time performance.
- To enable polymorphism and dynamic dispatch.
- To reduce memory usage.
- To prevent inheritance.
- To enforce static typing.
Memory Management
Which keyword is used to allocate dynamic memory in C++?
- malloc
- allocate
- new
- create
- memory
Templates
What is the main advantage of using templates in C++?
- To avoid compile-time errors.
- To write generic code that works with different data types.
- To reduce the size of the executable.
- To improve runtime performance by eliminating function calls.
- To enforce object-oriented programming principles.
Constructors
What is the purpose of a constructor in a C++ class?
- To destroy objects.
- To initialize the object's data members.
- To allocate memory for the object.
- To define the object's methods.
- To prevent inheritance.
Inheritance
What is multiple inheritance in C++?
- A class inheriting from a single base class multiple times.
- A class inheriting from two or more base classes.
- A class inheriting from a derived class.
- A function overloading multiple times.
- A constructor overloading multiple times.
Exception Handling
What is the purpose of the 'try' and 'catch' blocks in C++ exception handling?
- To define global variables.
- To define a macro.
- To handle errors and prevent program termination.
- To optimize code execution.
- To declare namespaces.
The Standard Library
Which standard library provides input/output functionalities in C++?
- stdio.h
- iostream
- stdlib.h
- math.h
- string.h
Operator Overloading
What does operator overloading allow you to do in C++?
- Create new operators.
- Change the precedence of operators.
- Define custom behavior for operators when used with user-defined types.
- Remove operators from the language.
- Disable operators.
Namespaces
What is the primary purpose of using namespaces in C++?
- To improve code execution speed.
- To group related classes together.
- To avoid name collisions between identifiers from different libraries.
- To reduce memory usage.
- To simplify debugging.