Pointer vs Reference
Which of the following statements correctly describes a reference in C++?
- A reference points to nothing by default and must be assigned before use.
- A reference stores the memory address of a variable and can be reassigned.
- A reference can refer to multiple objects during its lifetime.
- A reference is an alias for a variable and cannot be null.
- A reference is used only for arrays in C++.
Understanding Smart Pointers
Why are smart pointers, such as std::unique_ptr and std::shared_ptr, important in modern C++ programming?
- They replace all uses of references in code.
- They are slower than raw pointers in all cases.
- They automate memory management and help prevent memory leaks.
- They are required for template classes.
- They make the compilation process faster.
Choosing new vs malloc
What is a key difference between using new and malloc in C++?
- new cannot fail, while malloc can.
- new requires manual memory deallocation, malloc does not.
- malloc supports exception handling by default, new does not.
- new calls object constructors while malloc does not.
- malloc returns a type-safe pointer, but new does not.
Virtual Functions Usage
In C++, what does declaring a function as virtual in a base class accomplish?
- It enables derived classes to override the function for runtime polymorphism.
- It prevents the function from being overridden in derived classes.
- It converts the function into a global function.
- It makes the function only accessible to friend classes.
- It stops the function from being inherited.
RAII Principle
What does RAII (Resource Acquisition Is Initialization) ensure in C++ resource management?
- Resource leaks are more likely with RAII.
- Objects must be initialized with null values.
- All memory allocation must use malloc and free.
- Resources are tied to object lifetimes and released automatically when objects go out of scope.
- RAII is only applicable to integer variables.
const vs constexpr
Which statement best differentiates const from constexpr in C++?
- const can be evaluated at runtime, but constexpr must be evaluated at compile time.
- const variables must not be used with classes.
- constexpr allows modifying values during program execution.
- constexpr variables can only store floating-point numbers.
- const and constexpr are interchangeable in all contexts.
Manual vs Automatic Memory Management
When should you prefer smart pointers over raw pointers for memory management in C++?
- When you do not need destructor calls.
- When you need to save compilation time.
- When you want automatic and safe cleanup of resources without manual delete.
- When the memory allocation size is unknown at compile time.
- When you intend to manage memory only in C-style projects.
Polymorphism with Example
Given a base class Animal with a virtual sound() function, what will happen if a Dog class overrides sound() and we call sound() using a base pointer to a Dog object?
- Neither function will be called.
- A compile-time error will occur.
- The function will execute twice, once for each class.
- The Dog class's sound() function will be called at runtime.
- The base class's sound() will always be called.
Object Constructors
When you use new to create an object in C++, what happens regarding constructors?
- You must manually call the constructor.
- The object's constructor is called automatically.
- Constructors are not needed with new.
- Only a memory block is allocated without any initialization.
- The object remains uninitialized and unusable.
Smart Pointer Ownership
Which smart pointer type allows multiple owners to manage the same resource in C++?
- std::shard_ptr
- std::shared_ptr
- std::unique_ptr
- std::pointer_ref
- std::unknow_ptr