1
e01
CS8 M17
DO NOT WRITE IN THIS AREA! Name: Seat:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu

EXAM: e01: Midterm Exam

ready? date points
true Thu 08/24 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. Assume the following assignment statements have been entered at the Python prompt:

    team = "Gauchos"
    sport = "Soccer"
    season = "2017"
    

    Indicate the value of each of these expressions:

      Expression Result     Expression Result
    (3 pts) team[0:2]     (3 pts) team > sport  
    (3 pts) sport[1:3]     (3 pts) len(season)  
    (3 pts) season[-1]     (3 pts) type(season)  
  2. (5 pts) A complete Python program that prints out the following message is a very short program indeed:

    I'm a coder!
    

    Please write that entire program in the space below. You certainly have more space than you will need.

  3. (10 pts) As you may recall from your math courses, 2π radians = 360°, therefore to convert degrees to radians, we can divide by 360 and multiply by 2π.

    The value π in Python is writen as math.pi, which of course, requires an appropriate import statement.

    Given this information, write the definition of a Python function called deg2rad that takes a parameter called degrees and returns the corresponding value in radians.

  4. (5 pts) Now write a function call to this function, as you might type it at the Python prompt, for example, that computes the number of radians equal to 45°

  5. Testing a Python function can be done with the pytest module, as we did in lab02 and lab03 in this course. The handout you got with this exam has example code illustrating pytest test cases.

    1. (10 pts) Why do we need the pytest.approx thing? That is, why can’t we just write: assert cToF(100.0)==212.0?

    2. (12 pts) Now consider the deg2rad function you write earlier. Please write three test cases for that function appropriate for use with pytest.

      degrees radians
      0 0
      π/2 90
      π 180

      As values for your tests, you can use the values shown in the table for degrees and radians. (Reminder: you cannot write π in Python code for the value of π instead, use math.pi in the context of an appropriate import statement.)

    3. (10 pts) When using test cases, we often write a “stub” before writing the correct function definition. Write a function definition for deg2rad that illustrates this practice—that is, a function definition for deg2rad with a “stub”.

  6. (10 pts) Continuing with the topic of “stubs”—suppose you are at a job interview for an internship that involves coding.

    The interviewer says: “Testing is very important here at (name of company). Have you ever done anything with testing before, e.g. writing test cases, and using stubs?”

    You say “why, sure! I learned about stubs in my very first programming class at UCSB!”

    The interviewer says: “Great! Then, you can tell me: what is a stub, and what good is it? I mean, why do you write a stub first, instead of just going straight to writing the real function?”

    What do you say?

    Include enough detail in your answer so that the interviewer knows that you are technically sharp, and they should hire you. Do not include so much extra detail that the interviewer finds you tedious and annoying, and decides you would be painful to work with, and chooses to not hire you.

    Remember that it is a TWO PART QUESTION, and be sure you answer both parts, i.e. what is a stub, and why write a stub?

    Some points will be awarded for correctness, while others will be awarded based on a good, fluid answer that will convince someone to hire you. You may use an example in your answer, but your explanation of “what” a stub is must be more than just an example to get full credit. It should explain.

  7. When working on the CSIL systems, there is something called your “home directory”.

    1. (3 pts) There is a Unix/Linux shell command, that if you type it, always changes your current working directory to your home directory. Write that command in the space below.

    2. (3 pts) There is a special symbol that stands for the home directory of the user that is logged in (i.e. your home directory when you are logged in, my home directory if I am logged in). Please draw that symbol in the space below.

  8. (3 pts) There is a special directory called the “root” directory. What is the symbol used for the “root” directory?

  9. (3 pts) To start work on a Python program you need to type a command that brings up the Integrated Development Environment for Python (version 3). What is this command?

  10. (3 pts) A relative path is always interpreted in the context of the “current working directory”. What is the Unix command that shows you your current working directory?

  11. (5 pts) How can you tell if a path to a file or directory is a relative path or an absolute path?

End of Exam