CMU 15-112: Fundamentals of Programming and Computer Science
Quiz 2 (30 minutes)
Quiz 2 frontmatter:
- You may do the questions in any order, and you may leave them incomplete and return to them later if you wish. HOWEVER, if you begin a free response question (where you can run python code) any viewed but unsubmitted CTs and multiple choice questions will automatically be submitted and locked (so that you cannot run the CT code in the free response editor).
-
You have a fixed time to answer all the questions.
- Watch the timer carefully!
- If you are locked out due to a security error, exit the breakout room to speak with the TA or faculty member on duty
- Plug in your phone and computer
- Turn on do-not-disturb on your phone and computer and make sure your phone won't go to sleep or remove you from the zoom call or turn off your camera.
- Your phone must be properly positioned as described below
- Other than your main monitor and your phone, you must not have any other computer monitors on, and no other phones/tablets/calculators/notes/other resources should be accessible.
- Follow all proctoring instructions exactly. Failure to adhere to proctoring policy may result in a zero on the quiz, and possibly a deduction from your semester grade based on severity.
- Any attempt to subvert, exploit, or misuse the testing environment (including attempts to access the raw content or investigate the underlying source code) will be considered a severe academic integrity violation, possibly leading to course failure or other significant penalties.
-
You may not ask questions during the quiz.
- If you are unsure how to interpret a problem, take your best guess.
- You may not leave the quiz and return, and you may not interact with anyone else (remotely or in person) except for the TAs or faculty until the quiz is submitted.
- You must not leave the full-screen testing environment at any time. If anything except the testing environment is visible on your screen, it will trigger a security error, and you will be locked out of your quiz / you may receive a deduction or a zero. Additionally, we will investigate whether this could be a matter of academic integrity.
-
All of these must be visible to your phone's camera at all times:
- All of your screen, and any other screens nearby
- Most of your desk
- Your mouse and keyboard
- Note: You must not block your screen with your head/arms/etc while taking the quiz
- You must not have any other computer monitors on, and no other phones/tablets/calculators/notes/other resources should be accessible.
- If you are locked out due to a security error, exit the breakout room immediately to speak with the TA or faculty member on duty. At their discretion, they may unlock the quiz and allow you to continue.
- When you finish a question, press the submit button to lock in your answer. You will not be able to return and change your answers after pressing submit. Once the allotted time elapses, the quiz will auto-submit with your current progress. See above for more details
- You may receive a deduction if this is due to a lack of preparedness
- Join Zoom on your computer instead.
- You MUST still position your phone to see the items listed above, and you MUST record a video of the entire quiz until your proctor releases you.
- We will not grade any section of the quiz that we did not receive full video for, and you may receive a zero.This is true even if your phone runs out of storage space.
- After the quiz, immediately upload this to Google Drive or Box or Dropbox and then send us a link.You must use one of these three services. If you email the video directly or upload it to another source, you will receive a zero.
- If we cannot access or view your video, or if you do not record one, we will give you a zero, so make sure your upload is successful. We must be able to view the video soon after the quiz, so please begin uploading immediately.
- When your upload is complete, fill out the tech fail form and copy in the link to your video. Do not send it by email unless we request it.
- If you submit your quiz before time is up, wait until everyone else finishes and your proctor gives you further instructions.
- Wait until your proctor dismisses you, and then please exit Zoom. You are done! Rejoin the lecture Zoom session on your laptop.
- If you are still in Zoom, exit the breakout room and speak with the TA/Faculty in the main room. Do not speak while in the breakout room.
- If you cannot immediately speak with a TA or professor, stop taking the quiz and fill out the tech fail form linked from the course website's forms?page. (Note: A security error is not a tech fail, unless you are unable to return to the testing environment without triggering another one. We may be able to address accidental security errors in certain circumstances, but only if they are immediately brought to our attention.)
- We will email you soon to resolve the issue.
(This quiz was given in the CS Academy quiz proctoring app on 7/19)
Quick Reminders:
During the quiz
You must use your phone to join the proctored Zoom meeting. If you cannot join Zoom on your phone:
After the quiz
For any tech fails (laptop or internet stops working, etc.):
1. True/False [4 points]
Write only the whole word "True" or "False" (and not just T or F).
Strings are mutable, which means python can modify a string (instead of having to create a new one each time).
2. True/False [4 points]
Write only the whole word "True" or "False" (and not just T or F).
Inside redrawAll, the following code will draw a circle whose center is at x=100, y=200 with a radius of 50 pixels.
canvas.create_oval(100, 200, 50, 50)
3. Multiple Choice [4 points]
Write only the letter(s) for each correct answer. For full points, you must write the letter(s) for EVERY correct answer.
Which of the following lines are valid drawing calls in cmu_112_graphics? (Note: List all that apply. We have shown all the correct answers in lecture/recitation/notes.)
canvas.create_line(x0, y0, x1, y1)
canvas.create_triangle(x0, y0, x1, y1, x2, y2)
canvas.create_rectangle(x0, y0, x1, y1)
canvas.create_circle(cx, cy, r)
canvas.create_polygon(x0, y0, x1, y1, x2, y2)
4. Multiple Choice [4 points]
Write only the letter(s) for each correct answer. For full points, you must write the letter(s) for EVERY correct answer.
Which of the following would be considered an MVC violation? (List all that apply.)
Calling canvas.create_text in keyPressed
Modifying the model in appStarted
Calling canvas.create_oval in redrawAll
Modifying app.timerFired in redrawAll
Using a while loop in mousePressed
5. Code Tracing [10 points]
What does the following code print?
def ct1(s):
x = 'x'
y = 'y'
z = 'z'
for c in s:
if c.islower():
x = x + c
print(f'x = {x}')
elif c.isdigit():
y += c
print(f'y = {y}')
else:
z = c + z
print(f'z = {z}')
m = len(x)*len(y)*len(z)
if m > 8:
break
return x + y + z
print(ct1('a9C3b'))
6. Reasoning over Code [10 points]
Find an argument for s that makes rc1(s) return True. Write your answer below. (Note: There may be more than one correct answer, but you only need to find one.)
#Be sure to scroll until you see the return
def rc1(s):
s2 = s[len(s):0:-1]
s += s2
result = ''
if len(s2) == 3 and s2.isupper():
for c in s:
if s.count(c)==1:
result+=c
return result == '?'
7. Free Response / Fill-In-The-Blank: Animation [32 points]
Note: Because you haven't (edit: hadn't at the time of the quiz) gotten HW5 feedback yet and because you can't yet run animations directly in the testing framework, we will provide you with some starter code with blanks for you to fill in. As with all other FRs, partial credit is possible. (We do not advise modifying the starter code outside of the blanks, but you may if you wish.)
Write an animation with the following features:
- A red circle with radius 30 begins at the center of the canvas
- A score (which starts at 0) is drawn near the center-bottom of the screen
- Clicking inside the circle moves it 10 pixels up
- Clicking outside the circle increases the radius by 10
- If the circle extends beyond the top or bottom of the canvas, the score increases by 5 and the circle resets (i.e. it returns to the center of the canvas. Its radius also resets to 30.)
- Pressing 'r' resets the circle AND the score resets to 0.
#NOTE: Replace the _________ with your own code from cmu_112_graphics import * def appStarted(app): app._________ = 0 resetCircle(app) def resetCircle(app): app.cx = _________/2 app.cy = _________/2 app.r = 30 def keyPressed(app, event): if _________: app.score = 0 _________ def distance(x0, y0, x1, y1): return (_________)**0.5 def mousePressed(app, event): if distance(_________) <= _________: _________ else: _________ if (_________) or (_________): app.score += 5 resetCircle(app) def redrawAll(app, canvas): #Make the dot: canvas._______________ #Make the score: canvas._______________ runApp(width=500, height=400)
8. Free Response: nthTenlyPrime(n) [32 points]
Write the function encodeInterleave(s) that takes a string s and a positive integer r and returns a string which is interleave-encoded by r.
To interleave-encode a string by r, we include the beginning character s[0], but then we skip the next r characters before including each subsequent one. After we reach the end of the string, we pass over our original string again as many times as necessary, starting from s[1], s[2] and so-on until each character is included in our result.
For example:
encodeInterleave('abcde',1) returns 'acebd' because we skip 1 character and make 2 passes encodeInterleave('147258369',2) returns '123456789' because we skip 2 characters and make 3 passes encodeInterleave('123456789',6) returns '182934567' using 7 passes, though because skipping 6 characters usually takes us beyond the end of the string, most passes add only one character.
Look at the test cases for more examples. You may not use lists, tuples, sets, dictionaries, recursion. Do not hardcode, as we may use additional test cases.
Hint 1: Notice in the examples above that we make r+1 passes through the string
Hint 2: Solutions that use slicing will probably be shorter and less complicated
Note: We have included almostEqual and roundHalfUp as usual, but this certainly does not mean you need them.
######Helpers######### def almostEqual(d1, d2, epsilon=10**-7): #helper-fn return (abs(d2 - d1) < epsilon) #(This works the same as the one you've seen before) def roundHalfUp(d): #helper-fn sign = 1 if (d >= 0) else -1 d = abs(d) n = int(d) if (d - n >= 0.5): n += 1 return sign * n ###################### def encodeInterleave(s, r): return 42 def testEncodeInterleave(): print("Testing encodeInterleave...", end="") assert(encodeInterleave('abcde',2) == 'adbec') assert(encodeInterleave('12345678', 1) == '13572468') assert(encodeInterleave('abcdefghi', 2) == 'adgbehcfi') assert(encodeInterleave('147258369', 2) == '123456789') assert(encodeInterleave('abcdefgh', 2) == 'adgbehcf') assert(encodeInterleave('boring!', 0) == 'boring!') assert(encodeInterleave('12345678', 3) == '15263748') assert(encodeInterleave('12345678', 6) == '18234567') assert(encodeInterleave(':o)', 112) == ':o)') assert(encodeInterleave('', 4) == '') print("passed!") testEncodeInterleave()