C++ Memory Management and Types Quiz Quiz

  1. Stack vs Heap Allocation

    In C++, where is a local int variable declared inside a function most commonly allocated?

    1. On the stack
    2. On the hip
    3. On the heap
    4. In the global memory area
    5. In the large object heap
  2. Reference Types and Allocation

    When you use 'new' to create an object from a class in C++, where is the memory for that object generally allocated?

    1. On the heap
    2. On the stack
    3. In the static memory
    4. In the buffer
    5. On the filestack
  3. Value Passing Semantics

    What happens when you pass a struct by value as a function argument in C++?

    1. A copy of the struct is made
    2. A reference to the struct is passed
    3. Only the pointer to the struct is passed
    4. The content is always shared
    5. Nothing is passed
  4. References in C++

    If you use a reference parameter in C++ (e.g., void f(intu0026 x)), what is being passed to the function?

    1. A reference to the argument
    2. A copy of the value
    3. A pointer to a copy of the value
    4. A deep copy of the object
    5. A heap allocation
  5. Memory Deallocation

    In C++, what is the correct way to deallocate memory allocated with 'new' for a single object?

    1. Using delete
    2. Using remove
    3. Using free
    4. Using erase
    5. Using del
  6. Fragmentation in Heap

    What might happen to the heap memory in C++ programs after many allocations and deallocations?

    1. It can become fragmented
    2. It gets automatically cleared
    3. It is always contiguous
    4. It swaps to stack
    5. It gets compressed automatically
  7. Stack Frame Lifetime

    How long does a variable declared inside a function in C++ usually exist for?

    1. Until the function returns
    2. Until the program ends
    3. As long as the heap exists
    4. Until a new thread is started
    5. Indefinitely
  8. Boxing in C++

    Which C++ mechanism is most similar to 'boxing' in other languages?

    1. Storing a value type in a std::any or std::variant
    2. Using a class pointer
    3. Declaring a global variable
    4. Using new to create a primitive
    5. Assigning to a reference
  9. Global Variables Allocation

    Where are global variables usually stored in a C++ program?

    1. In the static memory segment
    2. On the heap
    3. On the stack
    4. In the register
    5. On the large object heap
  10. Reference vs. Value Types

    Which statement about C++ is correct regarding class and struct instances?

    1. Class instances created with 'new' are on the heap; struct instances declared without 'new' are on the stack
    2. Both classes and structs are always allocated on the heap
    3. Only primitive types can be on the stack
    4. All struct instances are allocated in global memory
    5. Assignment always copies by reference