CMU 15-112: Fundamentals of Programming and Computer Science
Quiz 1


Note: this quiz was canceled due to technical difficulties. We are posting the quiz here to help you prepare for the midterm and final exams.
See the quiz1 frontmatter.
1. Code Tracing

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

def ct1(n):
    print(1+2*n-3*4, n//4, n/4, (n%4)**3)
    x = (n+2)%n
    y = (n-2)%n
    return 10*x + y
print(ct1(10)) # prints 5 total values (on 2 lines)


def ct2(x, y):
    x *= 2
    print(float(y), pow(min(x, int(x)), abs(y-8)), bool(x/y), bool(1//y))
print(ct2(2.8, 5)) # prints 5 total values (on 2 lines)


# h(x,y) is used by ct3()
def h(x, y):
    if (y > x):
        if (x < 5):
            return 2
        elif (x >= 3) and (y < 10):
            return 4
    else:
        return 6
    return x

def ct3(x):
    y = 10*h(x,10) + h(3,x)
    z = 10*h(x,5) + h(x, x+1)
    return y + z/100
print(ct3(5))

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) If you try to evaluate 1/0, your program will crash due to a syntax error.

(C) Because of the 'Approximate Value of Floating-Point Numbers', you should use almostEqual(x,y) instead of (x == y) if either x or y are floating-point values.

(D) When Python evaluates (True or f()), the function f will not be called due to 'Short-Circuit Evaluation'.

(E) In the expression 2*3+4**5-6/7, the first part to be evaluated is 2*3.
3. Free Response: integerSquareRoot

Background: we will define the integer-square-root of a non-negative integer N to be the largest integer M such that M**2 <= N. So the integer-square-root of 99 is 9, becuase 9**2 is 81 which is <= 99, but 10**2 is 100 which is not <= 99.

With this in mind, write the function integerSquareRoot(v) that takes an arbitrary value v (possibly not an integer), and if v is a non-negative integer, returns the integer-square-root of v. Otherwise, the function returns None.

Here are some test cases:
    assert(integerSquareRoot(0) == 0)
    assert(integerSquareRoot(1) == 1)
    assert(integerSquareRoot(2) == 1)
    assert(integerSquareRoot(3) == 1)
    assert(integerSquareRoot(4) == 2)
    assert(integerSquareRoot(5) == 2)
    assert(integerSquareRoot(99) == 9)
    assert(integerSquareRoot(123**2) == 123)
    assert(integerSquareRoot(123**2 - 1) == 122)
    assert(integerSquareRoot(2.4) == None)
    assert(integerSquareRoot(-5) == None)
    assert(integerSquareRoot('Do not crash here!') == None)

4. Free Response: alternatesEvenOdd

Write the function alternatesEvenOdd(n) that takes a positive int n that has exactly 3 digits (ignoring leading 0's), so 100 <= n <= 999, and returns True if n alternates between even and odd digits, and False otherwise. Another way to say this is that there are no two consecutive even digits and no two consecutive odd digits.

Here are some test cases:
    assert(alternatesEvenOdd(147) == True)
    assert(alternatesEvenOdd(478) == True)
    assert(alternatesEvenOdd(124) == False)
    assert(alternatesEvenOdd(235) == False)
    assert(alternatesEvenOdd(777) == False)
    assert(alternatesEvenOdd(222) == False)
    assert(alternatesEvenOdd(787) == True)
    assert(alternatesEvenOdd(878) == True)
    assert(alternatesEvenOdd(943) == True)
    assert(alternatesEvenOdd(652) == True)
    assert(alternatesEvenOdd(692) == True)

4. Bonus Code Tracing

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

# f, g, and h are helper functions for bonusCt1
def f(x): return float(x) == int(x)
def g(x,d): return f(x**d)
def h(x): return x if g(x,1/2) else x//2
def bonusCt1(x):
    return (h(h(h(h(h(h(h(h(h(h(x)))))))))) ,
            h(h(h(h(h(h(h(h(h(h(x+1)))))))))))
print(bonusCt1(15))


# z is a helper function for bonusCt2
def z(x, y, d):
    a = x//10**d%10
    b = y//10**d%10
    c = a**b%10
    return c*10**d
def bonusCt2(x, y):
    return z(x,y,0)+z(x,y,1)+z(x,y,2)
print(bonusCt2(123, 45))