1
e02
CS8 M17
DO NOT WRITE IN THIS AREA! 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.

  1. 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:

    1. (4 pts) What is the specific problem that arises with the third test case, but not with the first two?
    2. (4 pts) How does pytest.approx address that problem?

  2. Draw the characters in the boxes below as indicated:

    pts character draw it here
    (2 pts) forward slash  
    (2 pts) backslash  
  3. What are the Unix commands that do each of the following:

    1. (2 pts) List contents of your current directory

    2. (2 pts) List contents of your current directory, showing permissions such as rwxr-xr-x?

    3. (2 pts) Show the absolute path of the current working directory?

    4. (2 pts) Change current working directory to my home directory?

    5. (2 pts) Change current working directory to cs8 under my current directory?

    6. (2 pts) Create a new directory called lab09 under the current directory?

    7. (2 pts) Asssuming I have a file called lab09.py in my current directory,
      change its permissions to rwxr-x---?

  4. In test driven development, we often write a stub for a function before we write the correct definition of that function.

    1. (5 pts) Suppose that you were asked to write a function called smallestOdd. The function should take a parameter called alist 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.

    2. (5 pts) Now write two suitable test cases for smallestOdd in the pytest style. (As a reminder, the handout that came with this exam has some example pytest test cases).

  5. Perform the number conversions indicated.
    1. (2 pts) Convert 12 from octal to binary
    2. (2 pts) Convert 1000 0111 from binary to decimal
    3. (2 pts) Convert 110 000 110 from base 2 to base 8
  6. 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?

    1. (4 pts) “Sometimes I see this line at the top of a Python file. What’s it for?”

      #!/usr/bin/env python3
      
    2. (4 pts) “Sometimes I see code like this in a Python file. What’s going on here?

      if __name__=="__main__":
         # more code goes here
      
      
  7. Perform the number conversions indicated.
    1. (2 pts) Convert 0101 0100 0011 1100 from binary to hexadecimal
    2. (2 pts) Convert 18d5 from base 16 to binary
    3. (2 pts) Convert 158 from decimal to binary
  8. 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.

    1. (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. (2 pts) Does a function call cause this part of the computer's memory to grow in size or shrink?
    3. (2 pts) Does returning from a function call cause this part of the computer's memory to grow in size or shrink?
    4. (2 pts) There is a special word associated with putting something new into this area of memory. What is that word?
    5. (2 pts) There is a special word associated with removing something from this area of memory. What is that word?
  9. 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 a ValueError 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'], return 1 for 'dog' because it has a smaller index than 2 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 selected Run 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
    message
    something
    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
    message
    something
    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
    message
    something
    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
    message
    something
    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'])              
End of Exam