1 |
e02 |
CS8 M17 |
Name: | Seat: | |
---|---|---|
(as it would appear on official course roster) | ||
Umail address: | @umail.ucsb.edu |
EXAM: e02: Final Exam
ready? | date | points |
---|---|---|
true | Thu 09/14 09:30AM |
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.
-
Please refer to the handout that came with this exam, and find the Example
pytest
test cases from lab03 at the top of page 1 of the handout.The third test case uses
pytest.approx
, while the other two don’t.Provide brief answers to these questions:
- (4 pts) What is the specific problem that arises with the third test case, but not with the first two?
-
(4 pts) How does
pytest.approx
address that problem?
-
Draw the characters in the boxes below as indicated:
pts character draw it here (2 pts) forward slash (2 pts) backslash -
What are the Unix commands that do each of the following:
-
(2 pts) List contents of your current directory
-
(2 pts) List contents of your current directory, showing permissions such as
rwxr-xr-x
? -
(2 pts) Show the absolute path of the current working directory?
-
(2 pts) Change current working directory to my home directory?
-
(2 pts) Change current working directory to
cs8
under my current directory? -
(2 pts) Create a new directory called
lab09
under the current directory? -
(2 pts) Asssuming I have a file called lab09.py in my current directory,
change its permissions torwxr-x---
?
-
-
In test driven development, we often write a stub for a function before we write the correct definition of that function.
-
(5 pts) Suppose that you were asked to write a function called
smallestOdd
. The function should take a parameter calledalist
which should be a non-empty list of integers. The function should return the smallest odd integer.In the space below write a stub definition for this function, following the test-driven development practices taught in the CMPSC 8 labs this quarter. Write only a stub definition for this function, and nothing else.
-
(5 pts) Now write two suitable test cases for
smallestOdd
in thepytest
style. (As a reminder, the handout that came with this exam has some examplepytest
test cases).
-
- Perform the number conversions indicated.
- (2 pts) Convert 12 from octal to binary
- (2 pts) Convert 1000 0111 from binary to decimal
- (2 pts) Convert 110 000 110 from base 2 to base 8
-
You are interviewing for a part-time job working on a research project with a professor at UCSB. The job involves a bit of Python coding. The professor asks you the following questions. You are not sure whether they are “testing you” (and they know the correct answers), or whether they really don’t know and they are curious.
Either way, you want to give an answer that is helpful, but brief—too long, and the professor will think you are tedious, and won’t want to hire you. But too vague, or incorrect, and the professor will think you really don’t know your stuff.
How do you answer in a way that gets you the job?
-
(4 pts) “Sometimes I see this line at the top of a Python file. What’s it for?”
#!/usr/bin/env python3
-
(4 pts) “Sometimes I see code like this in a Python file. What’s going on here?
if __name__=="__main__": # more code goes here
-
- Perform the number conversions indicated.
- (2 pts) Convert 0101 0100 0011 1100 from binary to hexadecimal
- (2 pts) Convert 18d5 from base 16 to binary
- (2 pts) Convert 158 from decimal to binary
-
As discussed in lecture, the Python interpreter has to keep track of all of the local variables in a running Python program, and the progress of functions as they are called and as they return.
Here are some questions about that.
- (3 pts) There is a special part of the computer's memory that keeps track of local variables, and information about each function that is in progess. What is the name given to this area in memory?
- (2 pts) Does a function call cause this part of the computer's memory to grow in size or shrink?
- (2 pts) Does returning from a function call cause this part of the computer's memory to grow in size or shrink?
- (2 pts) There is a special word associated with putting something new into this area of memory. What is that word?
- (2 pts) There is a special word associated with removing something from this area of memory. What is that word?
-
On the handout , you’ll find several attempts at writing a function to find the index of the shortest string in a list with names such as
indexShortest_a
,indexShortest_b
, etc. Please locate those on the handout before reading further.These functions are intended to operate as follows:
- If the parameter alist is not a list, is empty, or contains anything that
is not of type
str
, then raise aValueError
with an appropriate message. - Otherwise, return the index of the shortest string.
- If there is a tie, i.e. two or more strings of equal length, return the
index that is the smallest index among the indexes of all strings
of that length. For example for
['bear','dog','cat']
, return1
for'dog'
because it has a smaller index than2
for'cat'
.
That is how the functions are supposed to work. However, any or all of them may or may not contain bugs.
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 column. The first is done for you as an illustration
Points Function Call 0
1
2
3
None
Python
error
messagesomething
else(0 pts) indexShortest_a(['ant','dog','bear','horse'])
✔ (3 pts) indexShortest_a(['tiger','cow','cat','monkey'])
(3 pts) indexShortest_a(['rat','pig','fish','ox'])
Points Function Call 0
1
2
3
None
Python
error
messagesomething
else(3 pts) indexShortest_b(['ant','dog','bear','horse'])
(3 pts) indexShortest_b(['tiger','cow','cat','monkey'])
(3 pts) indexShortest_b(['rat','pig','fish','ox'])
Points Function Call 0
1
2
3
None
Python
error
messagesomething
else(3 pts) indexShortest_c(['ant','dog','bear','horse'])
(3 pts) indexShortest_c(['tiger','cow','cat','monkey'])
(3 pts) indexShortest_c(['rat','pig','fish','ox'])
Points Function Call 0
1
2
3
None
Python
error
messagesomething
else(3 pts) indexShortest_d(['ant','dog','bear','horse'])
(3 pts) indexShortest_d(['tiger','cow','cat','monkey'])
(3 pts) indexShortest_d(['rat','pig','fish','ox'])
- If the parameter alist is not a list, is empty, or contains anything that
is not of type