Quiz 2 Environment Diagram Practice


Hello everyone! This is the second part of practice questions for the upcoming Quiz 02. The first set covered Dicts, and this one will be focused on memory diagrams. The solutions for these problems can be found here.

NOTE (10/20/2020): On the video, none of the counter variables for the for in loops were defined on the frames, even though they should be. Please make sure that on the quiz, that whenever we have something like:

That we also define x on the frame and update that on your diagram as you go through each item on the list. Just like how you would declare a variable i whenever we have a while loop, you do the same for the variable declared in the for .. in statement.

Another note is that when we use the actual item versus the index in our for in loop, i.e.:

versus

On the heap, the first set of code (where x represents the item on the list) nothing should change on the heap! This is because we define x itself to be each item on that list, and therefore doing x = x + 1 would only affect the x variable. The variable x will not always be a direct reference to the item on the list itself. Remember, strings, ints, floats, and bools are not reference types, so a List of these variable types would mean that a for loop directly accessing each of these elements would not be a reference. Again, this is why only x in the first example changes, not the actual element on the xs list.

On the other hand, if we used index accessing like we do in the second set of code, that will change the element on the heap List, since we are directly referring to the list xs when using the bracket notation.

Instructions

For these problems, please create a memory diagram for each documenting the entire execution of the program like you have practiced in class.

Review Problems

1. Pass Fail

2. The Return of Weather Stats (updated 10/20/2020)

3. Let’s Make Friends (updated 10/20/2020)

4. Biscuits!