Reference Type interview questions.
Posted by Vadim on June 30, 2008
Part of my job is to interview developers for open position at my company. I’m surprised how many times people give wrong answers to questions related to Reference type vs. Value type. Bellow list of the questions some interviewers get incorrectly:
Question 1: Describe behavior of the code bellow:
Incorrect Answer 1: It will output “Interview”.
Correct Answer 1: NullReferenceException will be thrown on line 6. Because builder is null and cannot access ToString() method.
Question 2: What will the output of the code bellow?
Incorrect Answer 2: True
Correct Answer 2: False
Often people are very eager to demonstrate their knowledge of the subject and they volunteer statements like this one:
Reference Types live on the heap and Value Types on the stack.
And this triggers a question like this one from me:
Does it mean that in the code above b of type B will be stored on the heap but b‘s member i of type int is going to be stored on the stack?
This questions really confuses some people.
Correct statement is:
Reference Types and instance Value Types live on the heap and local Value Types live on the stack.
The purpose of this post is not to make fun but to educate.