1 |
e02 |
CS8 S18 |
Name: | ||
---|---|---|
(as it would appear on official course roster) | ||
Umail address: | @umail.ucsb.edu |
EXAM: e02: Midterm Exam
ready? | date | points |
---|---|---|
true | Tue 05/15 03:30PM |
You may not collaborate on this exam with anyone. If you need to use the restroom, you must leave your cell phone with the exam proctor before leaving the room.
- Write your name at the top of this page AND EVERY ODD NUMBERED PAGE.
- Double check that you turned in ALL pages; look for "End of Exam" on the last page.
- This exam is closed book, closed notes, closed mouth, cell phone off.
- You are permitted one sheet of paper (max size 8.5x11") on which to write notes.
- This sheet will be collected with the exam, and might not be returned.
- Please write your name on your notes sheet.
NOTE: All references to Python on this exam mean Python 3, so you should answer accordingly.
-
Find Handout A and locate the definition of the dictionary
states
.Indicate with a check ✓ what Python will return as the type of each expression.
Note that when Python reports a type, it reports it with expressions such as
<class 'int'>
and<class 'str'>
.I’m omitting the
<class '___' >
part to save space.-
(3 pts)
type(states["AZ"])
□str □ int □ float □ list □ tuple □ dict □ bool -
(3 pts)
type(states["NV"]["name"])
□str □ int □ float □ list □ tuple □ dict □ bool -
(3 pts)
type(states["CA"]["baseball"])
□str □ int □ float □ list □ tuple □ dict □ bool -
(3 pts)
type(len(states["NV"]["baseball"]))
□str □ int □ float □ list □ tuple □ dict □ bool -
(2 pts)
type(states["WA"]["baseball"][0])
□str □ int □ float □ list □ tuple □ dict □ bool -
(2 pts)
type( states["AZ"]["baseball"]==[] )
□str □ int □ float □ list □ tuple □ dict □ bool
-
-
(5 pts) For the next few problems, we are still working with the code for
states
on Handout A. In each, you’ll be given a function definition, with one line of code missing, together with:- what they would compute given the value of
states
on p. 1 of Handout A and the value ofeastern_states
on p. 2 of Handout A. - four choices for what to fill in the blank to complete the function.
Your job is simply to select which of the four choices shown correctly completes the function. If none of the answers are correct, check “None of the above”.
☐ answer[key] = states[key][name]
☐ answer[key] = states[key]["name"]
☐ answer["key"] = states["key"][name]
☐ answer["key"] = states["key"]["name"]
☐ None of the above 1 2 3 4 5 6
def abbrevToName(states): " return a dictionary with abbrev as key, and state name as value" answer = {} for key in states: _______________________ return answer
Sample function calls:
>>> abbrevToName(states) {'CA': 'California', 'AZ': 'Arizona', 'OR': 'Oregon', 'NV': 'Nevada', 'WA': 'Washington'} >>> abbrevToName(eastern_states) {'NY': 'New York', 'NJ': 'New Jersey', 'PA': 'Pennsylvania', 'DE': 'Delaware', 'MD': 'Maryland'} >>>
- what they would compute given the value of
-
(5 pts) Which line correctly completes the function below? Put a check mark (✓) in the appropriate box beside that line. If none of the answers are correct, check “None of the above”.
☐ answer.append(states[key])
☐ answer.append(states["name"])
☐ answer.append(states[key]["name"])
☐ answer.append(states[key][name])
☐ None of the above 1 2 3 4 5 6 7
def listOfStatesWhereLargestIsCapital(states): answer=[] for key in states: if states[key]["capital"]==states[key]["largest"]: _________________________________ return answer
Sample Function Calls. (For definitions of
states
andeastern_states
, see Handout A.)>>> listOfStatesWhereLargestIsCapital(states) ['Arizona'] >>> listOfStatesWhereLargestIsCapital(eastern_states) [] >>>
-
(5 pts) Which expresssion correctly completes the function below? Put a check mark (✓) in the appropriate box beside that expression. If none of the answers are correct, check “None of the above”.
☐ len(states[key]["baseball"])!=[]
☐ len(states[key]["baseball"])!=0
☐ len(states[key]["baseball"])==0
☐ states[key]["baseball"]==[]
☐ None of the above 1 2 3 4 5 6 7
def listOfStateAbbrevsWithBaseballTeams(states): answer=[] for key in states: if ______________________: answer.append(key) return answer
Sample Function Calls. (For definitions of
states
andeastern_states
, see Handout A.)>>> listOfStateAbbrevsWithBaseballTeams(states) ['CA', 'AZ', 'WA'] >>> listOfStateAbbrevsWithBaseballTeams(eastern_states) ['NY', 'MD', 'PA'] >>>
-
(5 pts) Which line correctly completes the function below? Put a check mark (✓) in the appropriate box beside that line. If none of the answers are correct, check “None of the above”.
☐ answer = [ answer + states[key]["capital"] ]
☐ answer = answer + [ states[key]["capital"] ]
☐ answer = [ answer ] + [ states[key]["capital"] ]
☐ answer = answer + states[key]["capital"]
☐ None of the above 1 2 3 4 5 6
def listOfStateCapitals(states): answer=[] for key in states: _______________________________ return answer
Sample Function Calls. (For definitions of
states
andeastern_states
, see Handout A.)>>> listOfStateCapitals(states) ['Phoenix', 'Salem', 'Sacramento', 'Carson City', 'Olympia'] >>> listOfStateCapitals(eastern_states) ['Albany', 'Trenton', 'Harrisburg', 'Annapolis', 'Dover'] >>>
-
Find Handout B. On it, you’ll find some test cases for a function called
indexOfLargestEvenInt
, which takes a list of integers as a parameter, and then returns the index of the largest even integer in the list, or the valueNone
if there are no even integers in the list.Locate that handout and those test cases now, and make sure you understand what the function
indexOfLargestEvenInt
is supposed to compute by looking at the test cases.Folloing those test cases, there are four attempts to write this function, labelled
indexOfLargestEvenInt_a
,indexOfLargestEvenInt_b
,
indexOfLargestEvenInt_c
, andindexOfLargestEvenInt_d
.At least one of these functions has correct code, i.e. it would pass all four of the test cases shown for
indexOfLargestEvenInt
. The others may or may not contain bugs.</p>Your job is to do what Python would do with this code, i.e. indicate the output of the function call shown.
Assume that it has been loaded into
idle3
and that we’ve selectedRun Module
(or pressed F5.) Then we typed in the function call shown, and something is printed as a result.
Which of the answers shown matches what is printed? Put a check mark (✓) in the appropriate box.
HINT: Doing a comparison between a value of type
int
and the valueNone
results in an error. Any time that happens, you should check the box in the column labelled Python error message.Points Function Call Python
error
messagesomething
else(4 pts) indexLargestEvenInt_a([22,55,88,33,66])
☐ None
☐ 0
☐ 2
☐ 22
☐ 88
☐ ☐ (4 pts) indexLargestEvenInt_a([33,11,77])
☐ None
☐ 0
☐ 1
☐ 2
☐ 77
☐ ☐ (4 pts) indexLargestEvenInt_a([33,34,11,66,99])
☐ None
☐ 3
☐ 4
☐ 66
☐ 99
☐ ☐ (4 pts) indexLargestEvenInt_a([44,11,22])
☐ None
☐ 0
☐ 1
☐ 22
☐ 44
☐ ☐ Points Function Call Python
error
messagesomething
else(4 pts) indexLargestEvenInt_b([22,55,88,33,66])
☐ None
☐ 0
☐ 2
☐ 22
☐ 88
☐ ☐ (4 pts) indexLargestEvenInt_b([33,11,77])
☐ None
☐ 0
☐ 1
☐ 2
☐ 77
☐ ☐ (4 pts) indexLargestEvenInt_b([33,34,11,66,99])
☐ None
☐ 3
☐ 4
☐ 66
☐ 99
☐ ☐ (4 pts) indexLargestEvenInt_b([44,11,22])
☐ None
☐ 0
☐ 1
☐ 22
☐ 44
☐ ☐ Points Function Call Python
error
messagesomething
else(4 pts) indexLargestEvenInt_c([22,55,88,33,66])
☐ None
☐ 0
☐ 2
☐ 22
☐ 88
☐ ☐ (4 pts) indexLargestEvenInt_c([33,11,77])
☐ None
☐ 0
☐ 1
☐ 2
☐ 77
☐ ☐ (4 pts) indexLargestEvenInt_c([33,34,11,66,99])
☐ None
☐ 3
☐ 4
☐ 66
☐ 99
☐ ☐ (4 pts) indexLargestEvenInt_c([44,11,22])
☐ None
☐ 0
☐ 1
☐ 22
☐ 44
☐ ☐ Points Function Call Python
error
messagesomething
else(4 pts) indexLargestEvenInt_d([22,55,88,33,66])
☐ None
☐ 0
☐ 2
☐ 22
☐ 88
☐ ☐ (4 pts) indexLargestEvenInt_d([33,11,77])
☐ None
☐ 0
☐ 1
☐ 2
☐ 77
☐ ☐ (4 pts) indexLargestEvenInt_d([33,34,11,66,99])
☐ None
☐ 3
☐ 4
☐ 66
☐ 99
☐ ☐ (4 pts) indexLargestEvenInt_d([44,11,22])
☐ None
☐ 0
☐ 1
☐ 22
☐ 44
☐ ☐