CMU 15-112: Fundamentals of Programming and Computer Science
Quiz1a
Important notes:
- Do not start until you are instructed to do so!
- 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.
- You will have 15 minutes once the proctor says to start.
- You will have brief additional time after we stop to scan and submit your solutions.
- Just before the quiz...
- Have a fully-charged smartphone and laptop, and still plug both in if possible
- Log into Gradescope on your phone
- 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)
- Turn on Do Not Disturb (or otherwise turn off all notifications).
- 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
- During the quiz:
- You may not ask questions during the exam.
- If you are unsure how to interpret a problem, take your best guess.
- You may not touch your laptop or webcam.
- This includes muting yourself at any point; the proctors may mute you though.
- 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
- For any tech fails (laptop or internet stops working, etc.):
- Stop taking the quiz
- Fill out this Google Form immediately
- We will email you soon to set up a 1-on-1 oral quiz with the course faculty
- You may not ask questions during the exam.
- After the quiz:
- Follow all proctor instructions on how to end the quiz.
- Keep everything in view (as noted above) until the proctor calls "time".
- When instructed, use your phone to scan your quiz and submit the PDF to Gradescope.
- After submitting to Gradescope, hold your phone up to the webcam to show the receipt.
- 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(4, 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(7, 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, 4) x = h(4, 8) y = h(8, 3) 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) In turtle graphics, the point (0, 0) is at the left-top corner of the window.
(C) We use roundHalfUp() instead of round() because of problems related to the 'Approximate Values of Floating-Point Numbers'.
(D) Short-Circuit Evaluation occurs in multiplication when the first value is 0, because Python knows the result will be 0.
(E) 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).
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 okAnd 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 intWith 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))