Knowledge Check - Memory
1. True/False. Anything on the stack can be accessed at any time.
False. You generally only have access to things in the top frame on the stack (and things on the heap).
2. True/False. The stack keeps track of local variables.
True. This is the primary role of the stack.
3. True/False. The contents of a value type can be placed on the heap.
True, though a little tricky. Value types will be placed on the heap if they are part of a reference type. So an
int will contain value types (
int) inside of it, and the whole array object lives on the heap.
4. True/False. The contents of a value type are always placed on the heap.
False. When possible, value types are placed on the stack instead. The only time these end up on the heap is when they are put there as a part of a reference type like an array.
5. True/False. The contents of a reference type are always placed on the heap.
True. The variables containing just a reference may live on the stack, but the contents are always placed on the heap somewhere.
6. True/False. The garbage collector cleans up old, unused space on the heap and stack.
False. It does do this for the heap, but the stack does not need help cleaning up old memory from the garbage collector.
7. True/False. If
b are arrays that reference the same object, modifying a will affect b as well.
True. They refer to the same object in memory, and thus a change made through one variable affects that shared object, and can be seen through the other reference.
8. True/False. If
b are ints with the same value, modifying
a will affect
b as well.
False. Value types represent different memory locations. In this case,
b are separate locations; modifying one will change its own memory, but leave the other untouched.