Stack vs Heap Allocation
In C++, where is a local int variable declared inside a function most commonly allocated?
- On the stack
- On the hip
- On the heap
- In the global memory area
- In the large object heap
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?
- On the heap
- On the stack
- In the static memory
- In the buffer
- On the filestack
Value Passing Semantics
What happens when you pass a struct by value as a function argument in C++?
- A copy of the struct is made
- A reference to the struct is passed
- Only the pointer to the struct is passed
- The content is always shared
- Nothing is passed
References in C++
If you use a reference parameter in C++ (e.g., void f(intu0026 x)), what is being passed to the function?
- A reference to the argument
- A copy of the value
- A pointer to a copy of the value
- A deep copy of the object
- A heap allocation
Memory Deallocation
In C++, what is the correct way to deallocate memory allocated with 'new' for a single object?
- Using delete
- Using remove
- Using free
- Using erase
- Using del
Fragmentation in Heap
What might happen to the heap memory in C++ programs after many allocations and deallocations?
- It can become fragmented
- It gets automatically cleared
- It is always contiguous
- It swaps to stack
- It gets compressed automatically
Stack Frame Lifetime
How long does a variable declared inside a function in C++ usually exist for?
- Until the function returns
- Until the program ends
- As long as the heap exists
- Until a new thread is started
- Indefinitely
Boxing in C++
Which C++ mechanism is most similar to 'boxing' in other languages?
- Storing a value type in a std::any or std::variant
- Using a class pointer
- Declaring a global variable
- Using new to create a primitive
- Assigning to a reference
Global Variables Allocation
Where are global variables usually stored in a C++ program?
- In the static memory segment
- On the heap
- On the stack
- In the register
- On the large object heap
Reference vs. Value Types
Which statement about C++ is correct regarding class and struct instances?
- Class instances created with 'new' are on the heap; struct instances declared without 'new' are on the stack
- Both classes and structs are always allocated on the heap
- Only primitive types can be on the stack
- All struct instances are allocated in global memory
- Assignment always copies by reference