15-112: Fundamentals of Programming and Computer Science
Quiz1



  1. Short Answer [20 pts; 10 pts each]
    1. Write one line of Python that generates a runtime error.

    2. The function xor(b1, b2) takes two boolean values and returns True if one of them is True and one of them is False, and False otherwise. The following functions are supposed to work just like xor(b1, b2), for boolean inputs, but some of them might contain bugs. Circle any that do not work, and for those, provide one example of inputs on which they fail.
      def f1(b1, b2): return ((b1 and not b2) or (b2 and not b1))
      
      def f2(b1, b2): return (b1 != b2)
      
      def f3(b1, b2): return (b1 + b2 > 0)
      
      def f4(b1, b2): return ((not b1) == b2)
      
      def f5(b1, b2): return (b1 - b2 != b2 - b1)
      

  2. Code Tracing [30 pts]: Indicate what this will print:
    # hint: prints 5 total lines
    def f(x):
        print "f", x
        x += 1
        return x**2/10
    def g(x):
        print "g", x
        x = (7*x)%5
        return f(x+3)
    x = 5
    print f(g(f(x))) + x
    

  3. Reasoning Over Code [15 pts]
    Find arguments for the following function that make it return True. You only need one set of arguments for the function, even if there are multiple correct answers.
    def f(x):
        assert(type(x) == int)
        return ((x%10 == x/10-1) and
                (x/9*9 == x))
    

  4. Free Response: nearestLatticePointDistance (NLPD) [35 pts]
    A lattice point is a point (x,y) where x and y are both integers. So (1, 1) and (83, 72) are lattice points, but (1.5, 2) is not. With this in mind, write the function nearestLatticePointDistance(x, y), which you may abbreviate NLPD(x,y), which takes two floats x and y, and returns the distance to the nearest lattice point, rounded to the nearest 1/10th.

  5. Bonus/Optional: [2.5 pts] What will this print?
    For credit, be sure to show your work!
    def f(x=7, y=4): return (x*y)**(2*(x>y))%(7*x/y)
    print f(f(8,5)+f(),f(2))