CMU 15-112: Fundamentals of Programming and Computer Science
Quiz 2 (30 minutes)

Quiz 2 frontmatter:

    (This quiz was given in the CS Academy quiz proctoring app on 7/19)

    Quick Reminders:

    During the quiz

    1. You may not ask questions during the quiz.
      • If you are unsure how to interpret a problem, take your best guess.
    2. 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.
    3. 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.
    4. 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.
    5. 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.
    6. 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 must use your phone to join the proctored Zoom meeting. If you cannot join Zoom on your phone:

    After the quiz

    1. If you submit your quiz before time is up, wait until everyone else finishes and your proctor gives you further instructions.
    2. Wait until your proctor dismisses you, and then please exit Zoom. You are done! Rejoin the lecture Zoom session on your laptop.

    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.)

  1. canvas.create_line(x0, y0, x1, y1)
  2. canvas.create_triangle(x0, y0, x1, y1, x2, y2)
  3. canvas.create_rectangle(x0, y0, x1, y1)
  4. canvas.create_circle(cx, cy, r)
  5. 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.)

  1. Calling canvas.create_text in keyPressed
  2. Modifying the model in appStarted
  3. Calling canvas.create_oval in redrawAll
  4. Modifying app.timerFired in redrawAll
  5. 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}')
            z = c + z
            print(f'z = {z}')

        m = len(x)*len(y)*len(z)
        if m > 8:
    return x + y + z


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:
  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:

  1. A red circle with radius 30 begins at the center of the canvas
  2. A score (which starts at 0) is drawn near the center-bottom of the screen
  3. Clicking inside the circle moves it 10 pixels up
  4. Clicking outside the circle increases the radius by 10
  5. 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.)
  6. 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

def resetCircle(app): = _________/2 = _________/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(_________) <= _________:

    if (_________) or (_________):
      app.score += 5

def redrawAll(app, canvas):
    #Make the dot:

    #Make the score:

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.

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) == '')