Sample Midterm

These are sample questions that are very similar to the ones I will ask on the midterm. The midterm will be (slightly) longer.

  1. If the current working directory is /usr/ecs036a/tmp, give an absolute path name without any . or .. directories in it for:
    1. ../src/./../passwd+/misc/./root.c
    2. /usr/cs/files/../DMS/././.././DMS/schedule

  2. If I wanted to send the output of the command from to the file FROM.OUT, and the error messages to the terminal, what would I type to the C shell?

  3. How can you delete a file named -i?

  4. Multiple choice: which of the following expressions will cause an error to occur when it is evaluated?
    1. 3 + 5.0
    2. 10 % 4 + 7 // 2
    3. abs(5 - 20 // 3) ** 4
    4. "If %d + %d = %2.2f, then %s" % (2, 2, 4, "bye")
    5. 4 // "3"

  5. Multiple choice: if s = "abcdefg", what is the value of s[2:-4]?
    1. "bc"
    2. "c"
    3. "" (that is, the empty string)
    4. "fed"
    5. It causes an exception

  6. Which of the following Python identifiers are legal variable names? For those that are not, say why not.
    1. x
    2. Celsius
    3. spam3
    4. <hello>
    5. question?

  7. What is the data type (int, float, string, boolean) of the following? Please evaluate them.
    1. 3.9 + 6
    2. 5 + 1 > 7 or 2 == 1 + 1
    3. int("3")
    4. 7 / 2
    5. 8 // 3

  8. Write the following formulas in Python.
    1. a2
    2. x over y + z
    3. 12x - 3 over y

  9. What does the following program print?
    for i in range(1, 5):
        x = (-1) ** i * i
        print(i, x)

  10. Convert the following into a “for” loop.
    i = 1
    while i <= 10:
        print(i)
        i += 3

  11. When I ran the following program:
    x = int(input("Enter a number: "))
    y = 1 / x
    print("The reciprocal of", x, "is", y)
    I got the following error message:
    Traceback (most recent call last):
      File "x.py", line 2, in <module>
        y = 1 / x
    ZeroDivisionError: division by zero
    What was the specific error? Where did it cause the exception? And what did the user type?


UC Davis seal
Matt Bishop
Office: 2209 Watershed Science
Phone: +1 (530) 752-8060
Email: [email protected]
You can also obtain a PDF version of this.
Version of January 28, 2019 at 11:40AM