CMU 15-112: Fundamentals of Programming and Computer Science
Quiz 1 (20 minutes)

Quiz 1 frontmatter:

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

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


I understand that it is an Academic Integrity Violation to discuss/message/read/share 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!



2. True/False [4 points]

Write only the whole word "True" or "False" (and not just T or F).


The following line of code will crash:

print((9 // 7 == 2) or (2 / 0 == 1))



3. Multiple Choice [4 points]

Write only the letter of your answer.


Which of the following is NOT a built-in Python data type?

  1. def
  2. int
  3. str
  4. boolean
  5. float


4. Multiple Choice [4 points]

Write only the letter of your answer.


Which of the following is NOT a part of the 15-112 style guide?

  1. Use clear, concise, and informative comments
  2. Make sure each function includes an explicit return statement
  3. Do not include unused code
  4. Line length should not exceed 80 characters
  5. Use helper functions to break down large programs



5. Code Tracing [10 points]

What does the following code print?


def f(x): 
    return 3 * x - 2

def g(x): 
    return f(x + 3)

def ct1(x):
    print(f(x - 2))
    x -= 2
    print(g(x))
    x %= 4
    return f(g(x) % 6) // 2

print(3 + ct1(4))


6. Code Tracing [10 points]

What does the following code print?


def ct2(m):
    x = 1
    while x < 5:
        x += 2
        print('x = ', x)
    for y in range(m, m+2):
        print('y = ', y)
        x += y
    return x

print(ct2(2))



7. Free Response: largestPerfectSquare(n) [32 points]

Write the function largestPerfectSquare(n) that takes a non-negative int n and returns the largest perfect square that is no larger than n. (A perfect square is any number whose square root is an integer, like 0, 1, 4, 9, 16, etc.)

For full credit, do not use any loops for this problem! You do not need them here. (Solutions that use loops will lose 3 points)

Hint: This can be written using just one or two lines of Python.

Note: We have included almostEqual and roundHalfUp, but this 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 largestPerfectSquare(n):
    return 42

def testLargestPerfectSquare():
    print("Testing largestPerfectSquare...", end="")
    assert(largestPerfectSquare(5) == 4)    
    assert(largestPerfectSquare(12) == 9)    
    assert(largestPerfectSquare(24) == 16)    
    assert(largestPerfectSquare(25) == 25)    
    assert(largestPerfectSquare(2) == 1)    
    assert(largestPerfectSquare(0) == 0) 
    assert(largestPerfectSquare(2600) == 2500)       
    print("passed!")

testLargestPerfectSquare()


8. Free Response: nthTenlyPrime(n) [32 points]

Free Response: nthTenlyPrime(n)

We will say that a number is “tenly” (a made-up term) if the digits of the number add up to 10. So 1153 is tenly, but 153 is not. With this in mind, write the function nthTenlyPrime(n) that takes a non-negative int n and returns the nth number that is both tenly and prime. You should also write all the required helper functions.

You may use loops and anything else from this week's notes, but you may not use strings, lists, sets, dictionaries, recursion.

The first several tenly primes are: 19, 37, 73, 109, 127…

Note: We have included almostEqual and roundHalfUp, but this 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 nthTenlyPrime(n):
    return 42

def testNthTenlyPrime():
    print("Testing nthTenlyPrime...", end="")
    assert(nthTenlyPrime(0) == 19)
    assert(nthTenlyPrime(1) == 37)
    assert(nthTenlyPrime(2) == 73)
    assert(nthTenlyPrime(3) == 109)
    assert(nthTenlyPrime(4) == 127)
    assert(nthTenlyPrime(8) == 307)
    print("passed!")

testNthTenlyPrime()