--- title: Drill 14 Solution tags: Drills-F20, 2020 --- # Drill 14 Solution ## Question 1 Several of the questions below will refer to this dataclass: ```python @dataclass class Course: code : str students : list # of strings ``` Consider the code below. Write a line of code code to add 'Leo' to the end of the list of students in the first course in c. ::: spoiler Answer ``` c[0].students.append('Leo') ``` ::: ## Question 2 Consider the code below (same as above). How many memory locations would be needed store the information in 'c'? ``` c = [Course('csci0111', ['Eli', 'Rohan']), Course('csci0170', [])] ``` ::: spoiler Answer 5 We want to consider all the lists and Courses that are created: 1 for the list of courses, 1 for each of the courses, and 1 for each of the courses' lists. ::: ## Question 3 Consider the code below: ```python course1 = Course('csci0111', ['Monica', 'Julia']) course2 = Course('csci0112', ['Giselle', 'Colton']) course3 = course1 ``` Fill in the blanks in the program directory and memory: Program directory: | name | value | |---------|----------| | course1 | loc 1002 | | course2 | BLANK1 | | course3 | BLANK2 | Memory: | name | data | |----------|------------------------------| | loc 1001 | BLANK3 | | loc 1002 | Course('csci0111', loc 1001) | | loc 1003 | ['Giselle', 'Colton'] | | loc 1004 | Course('csci0112', BLANK4) | ::: spoiler Answer BLANK1: loc 1004 BLANK2: loc 1002 BLANK3: ['Monica', 'Julia'] BLANK4: loc 1003 ::: ## Question 4 Is there any program that will produce this program directory and memory? Program directory: | name | value | |---------|----------| | course1 | loc 1002 | | course2 | loc 1002 | Memory: | name | data | |----------|------------------------------| | loc 1001 | ['Monica', 'Julia'] | | loc 1002 | Course('csci0111', loc 1003) | | loc 1003 | ['Giselle', 'Colton'] | | loc 1004 | course1 | ( ) Yes ( ) No, two values cannot point at the same location in memory ( ) No, data in memory cannot refer to names ( ) No, loc 1002 cannot refer to loc 1003 :::spoiler Answer No, data in memory cannot refer to names Two values can point at the same location in memory--for instance, if we run `course1 = course2`. Data can refer to later memory locations--for instance, if we run `course1.students = ['Giselle', 'Colton']`. ::: ## Question 5 Imagine running the following program: ```python course1 = Course('csci0111', ['Julia', 'Colton']) course2 = Course(course1.code, course1.students) ``` Which of these would be the resulting program directory and memory? ### OPTION 1 Program directory: | name | value | |---------|----------| | course1 | loc 1002 | | course2 | loc 1002 | Memory: | name | data | |----------|------------------------------| | loc 1001 | ['Julia', 'Colton'] | | loc 1002 | Course('csci0111', loc 1001) | ### OPTION 2 Program directory: | name | value | |---------|----------| | course1 | loc 1002 | | course2 | loc 1003 | Memory: | name | data | |----------|------------------------------| | loc 1001 | ['Julia', 'Colton'] | | loc 1002 | Course('csci0111', loc 1001) | | loc 1003 | Course('csci0111', loc 1001) | ### OPTION 3 Program directory: | name | value | |---------|----------| | course1 | loc 1002 | | course2 | loc 1003 | Memory: | name | data | |----------|------------------------------| | loc 1001 | ['Julia', 'Colton'] | | loc 1002 | Course('csci0111', loc 1001) | | loc 1003 | loc 1002 | ### OPTION 4 Program directory: | name | value | |---------|----------| | course1 | loc 1002 | | course2 | loc 1004 | Memory: | name | data | |----------|------------------------------| | loc 1001 | ['Julia', 'Colton'] | | loc 1002 | Course('csci0111', loc 1001) | | loc 1003 | ['Julia', 'Colton'] | | loc 1004 | Course('csci0111', loc 1001) | ( ) OPTION 1 ( ) OPTION 2 ( ) OPTION 3 ( ) OPTION 4 :::spoiler Answer OPTION 2 :::