At Stern, we do Torah UMadda. For this year’s final last Friday, I didn’t include many Shavuot-related questions, but I did have about three.
1. Chapter 5 and 10. Dynamic scoping question
Consider the valid Python code above. Python, recall, has static scoping.
A) What is painful about this code? (5 points)
B) Draw the function call stack when we are in Moses() (5 points)
c) Now pretend that Python has dynamic scoping. Modify the code above to make it far simpler, using the fact of dynamic scoping. (10 points)
d) Using shallow access, what would the function call stack look like in the version of the code above? What would it look like now that you’ve modified the code? (5 points)
2] Chapter 6 Supplement: Object and Memory Management
Consider the following C++ code, involving destructors and smart pointers.
a) At point 1, after we’ve run constructList()
, draw a picture of the variables in memory. Label that picture so that I can understand what is happening, (5 points).
b) At point 2, after we’ve run killHolofernes()
, draw a picture of the variables in memory. Label that picture so that I can understand what is happening, (5 points).
c) Explain, in a paragraph of at least four sentences, why the picture in (a) has changed to the picture in (b). Refer to reference counting as part of your explanation. (10 points)
d) What one line could you add to prevent (c) from happening, that is, to cause a memory leak? Explain and refer to reference counting as part of your answer (5 pts)
3) Some additional questions about the C++ code in question 2. Answer in paragraph format. (5 points each)
a) Consider this constructor from above:
LinkedListNode(const string& value) : value(value) {}
Explain what the : value(value)
portion means, and why this is a neat feature.
b) Consider the (const string& value)
portion of the code. We are passing value in as a const, and passing it in as a &. Explain the benefit of each, in terms of what we covered in chapter 9 about parameter passing methods. What will the const prevent from happening? What does the & give us? (Hint: there is a give and take here, and these two features work together to provide something but counteract a negative aspect of the other.)
c) What is the name of the destructor? What is the equivalent of the destructor, in either Python of Java.
d) In what way is the -> syntactic sugar?
e) Name a feature used in the code for each of the three #includes.
3] Slightly modified from the original. And related to Sefira. Also to Lech Lecha.
What would the following Scheme code evaluate to?
(cdar '('(L A) (OM ER)))
I love it!
בברכה!
Nachman Levine