Value and Reference Types Quiz
Below is the answers, and a brief discussion for the answers to the Value and Reference Types Quiz Try It Out! problem.
1: False. You can only access things in the top frame of the stack. Each frame represents a method call, and the top frame represents the current method you are in. You only have access to the variables that are local to the method, and not things from other methods. You only have access to the top frame on the stack.
2: True. The stack contains a method’s local variables, along with some extra information, like what method called this method.
3: True. Value types are often on the stack, if they’re a local variable, but when used inside of a reference type (including an array) the value typed variable can end up on the heap.
4: False. Value types can be on the stack or the heap. The contents of reference types are always placed on the heap (though references to a reference type can be placed on the stack).
5: True. Reference types are placed on the heap. (Though, to be clear, the contents of value types are always placed on the heap, while the bucket containing the reference may still be on the stack.
6: False. The garbage collector handles the heap. The stack maintains itself as methods are called or completed. It doesn’t need garbage collection.
7: False. Only reference types can use null
. null
implies a reference to nothing.
8: True. If two things reference the same object, then by accessing one to modify the data, you’ll also be affecting what the second one is referencing.
9: False. When you assign a value from one variable to another, if it is a value type, a copy of the contents is made. Modifying one doesn’t make a difference on the other, since it is a completely different copy.