Essential C++ Concepts for Interview Success Quiz

  1. Pointer vs Reference

    Which of the following statements correctly describes a reference in C++?

    1. A reference points to nothing by default and must be assigned before use.
    2. A reference stores the memory address of a variable and can be reassigned.
    3. A reference can refer to multiple objects during its lifetime.
    4. A reference is an alias for a variable and cannot be null.
    5. A reference is used only for arrays in C++.
  2. Understanding Smart Pointers

    Why are smart pointers, such as std::unique_ptr and std::shared_ptr, important in modern C++ programming?

    1. They replace all uses of references in code.
    2. They are slower than raw pointers in all cases.
    3. They automate memory management and help prevent memory leaks.
    4. They are required for template classes.
    5. They make the compilation process faster.
  3. Choosing new vs malloc

    What is a key difference between using new and malloc in C++?

    1. new cannot fail, while malloc can.
    2. new requires manual memory deallocation, malloc does not.
    3. malloc supports exception handling by default, new does not.
    4. new calls object constructors while malloc does not.
    5. malloc returns a type-safe pointer, but new does not.
  4. Virtual Functions Usage

    In C++, what does declaring a function as virtual in a base class accomplish?

    1. It enables derived classes to override the function for runtime polymorphism.
    2. It prevents the function from being overridden in derived classes.
    3. It converts the function into a global function.
    4. It makes the function only accessible to friend classes.
    5. It stops the function from being inherited.
  5. RAII Principle

    What does RAII (Resource Acquisition Is Initialization) ensure in C++ resource management?

    1. Resource leaks are more likely with RAII.
    2. Objects must be initialized with null values.
    3. All memory allocation must use malloc and free.
    4. Resources are tied to object lifetimes and released automatically when objects go out of scope.
    5. RAII is only applicable to integer variables.
  6. const vs constexpr

    Which statement best differentiates const from constexpr in C++?

    1. const can be evaluated at runtime, but constexpr must be evaluated at compile time.
    2. const variables must not be used with classes.
    3. constexpr allows modifying values during program execution.
    4. constexpr variables can only store floating-point numbers.
    5. const and constexpr are interchangeable in all contexts.
  7. Manual vs Automatic Memory Management

    When should you prefer smart pointers over raw pointers for memory management in C++?

    1. When you do not need destructor calls.
    2. When you need to save compilation time.
    3. When you want automatic and safe cleanup of resources without manual delete.
    4. When the memory allocation size is unknown at compile time.
    5. When you intend to manage memory only in C-style projects.
  8. 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?

    1. Neither function will be called.
    2. A compile-time error will occur.
    3. The function will execute twice, once for each class.
    4. The Dog class's sound() function will be called at runtime.
    5. The base class's sound() will always be called.
  9. Object Constructors

    When you use new to create an object in C++, what happens regarding constructors?

    1. You must manually call the constructor.
    2. The object's constructor is called automatically.
    3. Constructors are not needed with new.
    4. Only a memory block is allocated without any initialization.
    5. The object remains uninitialized and unusable.
  10. Smart Pointer Ownership

    Which smart pointer type allows multiple owners to manage the same resource in C++?

    1. std::shard_ptr
    2. std::shared_ptr
    3. std::unique_ptr
    4. std::pointer_ref
    5. std::unknow_ptr