CMU 15-112: Fundamentals of Programming and Computer Science
Quiz1b

Important notes:
  1. Do not start until you are instructed to do so!
  2. We suggest you do not use your browser's zoom feature. Instead...
    • Click near left edge to make font smaller.
    • Click near right edge to make font bigger.
  3. You will have 15 minutes once the proctor says to start.
  4. You will have brief additional time after we stop to scan and submit your solutions.

  5. Just before the quiz...
    1. Have a fully-charged smartphone and laptop, and still plug both in if possible
    2. Log into Gradescope on your phone
    3. Change the screen timeout setting on your phone to never, so your screen doesn't go black if you don't interact with your screen for a while.
      • iPhones: Settings / Display & Brightness / Auto-Lock / Never
      • Android: Settings / Display / Screen timeout / 10 minutes (or the maximum amount of time)
    4. Turn on Do Not Disturb (or otherwise turn off all notifications).
    5. Position your webcam so we can see:
      • Your desk
      • The paper you are working on
      • Your writing utensil(s)
      • Both of your hands
      • Your phone

  6. During the quiz:
    1. You may not ask questions during the exam.
      • If you are unsure how to interpret a problem, take your best guess.
    2. You may not touch your laptop or webcam.
      • This includes muting yourself at any point; the proctors may mute you though.
    3. All of these must be visible at all times:
      • Your desk
      • The paper you are working on
      • Your writing utensil(s)
      • Both of your hands
      • Your phone, with the quiz webpage
    4. For any tech fails (laptop or internet stops working, etc.):
      1. Stop taking the quiz
      2. Fill out this Google Form immediately
      3. We will email you soon to set up a 1-on-1 oral quiz with the course faculty

  7. After the quiz:
    1. Follow all proctor instructions on how to end the quiz.
    2. Keep everything in view (as noted above) until the proctor calls "time".
    3. When instructed, use your phone to scan your quiz and submit the PDF to Gradescope.
    4. After submitting to Gradescope, hold your phone up to the webcam to show the receipt.
    5. Even then, remain in quiz mode until the proctor calls "all clear"


More important notes:
  • Write your answers by hand with no calculators, no computers.
  • No strings, lists, string or list indexing, loops, or recursion.
  • You may call almostEqual(x, y) and roundHalfUp(d) without writing them. Write everything else!


1. Code Tracing [20 points; 5 points each]

What does the following code print?
Be certain to show your work, and also very clearly circle your answer!

CT 1 of 4:
def ct1(x, y, z):
  x *= z
  z = y ** z
  return (10 * y + z) % x

print(ct1(5, 3, 2))

CT 2 of 4:
def ct2():
  print(42, end='')
  # no return statement!

print(ct2())

CT 3 of 4:
# f(x,y) is used by ct3

def f(x, y):
  y += 1
  return x - x // y

def ct3(x, y):
  x = f(x, y - 1)
  return x / (y - 1)

print(ct3(9, 3))

CT 4 of 4:
# h(x,y) is used by ct4

def h(x, y):
  if (x > y):
    if (x > 2 * y):
      return 3
  elif (x < 2 * y):
    return 5
  else:
    return 7
  return 9

def ct4():
  w = h(8, 3)
  x = h(8, 4)
  y = h(4, 8)
  z = w + 10 * x + 100 * y
  print(z)

ct4()


2. True/False [10 points; 2 points each]

For each of the following, write the whole word "True" or "False" (and not just T or F).

(A) I understand that it is an Academic Integrity Violation to discuss anything at all about this quiz with any other 112 student, regardless of which lecture they attend, until the day after the quiz is taken (so, tomorrow). Hint: your answer had better be True!

(B) We use roundHalfUp() instead of round() because of problems related to the 'Approximate Values of Floating-Point Numbers'.

(C) Short-Circuit Evaluation occurs in multiplication when the first value is 0, because Python knows the result will be 0.

(D) If you try to evaluate 1 + 2 / 0, your program will crash due to a runtime error (as opposed to a syntax error or logical error).

(E) In turtle graphics, the point (0, 0) is at the left-top corner of the window.

3. Free Response [70 points]

Background: we will say an integer is "doubly" (a coined term) if it is exactly 4 digits long (ignoring leading 0's) and the left two digits match the right two digits in the same order. Thus, these values are doubly:
  1212
  3838
  9999
  -4242 # negatives are ok
And these values are not doubly:
  1221  # 12 != 21
  1122  # 11 != 22
  333   # not enough digits
  66666 # too many digits
  1.212 # not an int
  'wow' # also not an int
With this in mind, and without using strings, write the function isDoubly(n) that takes a value of any type and -- without crashing -- returns True if n is doubly, and False otherwise.

4. Bonus Code Tracing [Up to 4 points; 2 points each]

What does the following code print?
Be certain to show your work, and also very clearly circle your answer!

Bonus CT 1 of 2
# q(n) and r(m, n) are used
# by bonusCt1()

def q(n):
  return (1 if (n == 0)
          else
          round(n/abs(n)))

def r(m, n):
  return (q(m**11) +
          q(n**12) +
          q((m+n)**13))

def bonusCt1(x, y, z):
  return (100 * r(x, y) +
          10 * r(x, z) +
          r(y, z))

print(bonusCt1(235,
               -104,
               -417))

Bonus CT 2 of 2
# v(s) and vv(s) are used
# by bonusCt2()

def v(s):
    return (10 if s < 20
            else
            v(s-10) +
            v(s-20))

def vv(s):
    return (v(s) if
            (v(s) > 500)
            else vv(s+10))

def bonusCt2(r):
    return vv(10*r)

print(bonusCt2(1))