15-112 Fall 2013 Quiz 1

* Note that there were 2 very similar versions of this quiz.  Each is included here.

* 20 minutes.  No calculators, no notes, no books, no computers.
* No loops or conditionals or recursion!
SHOW YOUR WORK, CIRCLE YOUR ANSWERS.

1.      Quick Answers.  [20 pts]

a.       State and prove any of DeMorgan’s laws using truth tables.

b.      Give a Python expression that is True (not “Truthy”, but actually True) if the variable x refers to a value that is “Truthy” and False otherwise.

c.       What is the common term we use to describe floating-point numbers x where (x % 1) equals 0?

d.      Give two expressions that use some Python operator where the semantics of the operator differ based upon the types of the operands.
 

2.      Code Tracing  [20 pts]Indicate what each will print:

def f(x): return (x**2)/(x%7)

def g(x): return x-1

def h(x, y): return (f(g(x)) % g(f(y)))

print h(9, 13)

 

############################################

 

def f(x, y):

    print "f",      # don't miss this print statement!

    return (x > y)

def g(x, y):

    print "g",      # ditto

    return (x > 2*y)

def h(x, y):

    print "h",      # ditto

    return x*y
 

print f(1, 0) and g(-1,-2) and f(3,4) and g(5,6)

print h(-1, 0) or h(0, 1) or h(1, 2) or h(2, 3)
 

3.  Reasoning Over Code  [20 pts]
Find arguments for the following functions f and h (not g) that make them return True.  You only need one set of arguments for each function, even if there are multiple correct answers.

def f(x,y):
    return ((type(x) == type(y) == int) and
            (100 > x > y > 0) and
            (x/10 + x%10 + y/10 + y%10 < 5) and
            (x % 10 == y % 10) and
            (x == y * 3))
 

############################################
# Reasoning Over Code  (continued). 

def g(x, y):
    return x - x/y*y  # hint: what is this doing in general?

def h(x, y):
    return ((type(x) == type(y) == int) and
            (40 > x > 30) and (13 > y > 10) and  # hint: notice unusual bounds here
            (g(x, y) == 5))

 

4.      Free Response:  findRoot  [40 pts]
Recall the quadratic formula:
     
Using this formula, write the function findRoot(a, b, c) that takes three numeric values (so you may safely assume they are numeric) and returns the larger of the two values given by the quadratic formula above.  Your function may not crash for any inputs, but instead should return False if it cannot compute the numeric result.
 

5.      Bonus/Optional  [1 pt each]

a.       If x and y are integers where ((x%y == y%x) and (x != y)) is True, what must be true about x and y?

b.  What will this print?
def r(x, y): return (x*y == 0)*1 or x+r(r(x/3,y/4),x/5)
print r(5,3)

 

15-113 Fall 2013 Quiz 1

* 20 minutes.  No calculators, no notes, no books, no computers.
* No loops or conditionals or recursion!
SHOW YOUR WORK, CIRCLE YOUR ANSWERS.

1.      Code Tracing  [20 pts]Indicate what each will print:

def f(x): return (x**2)/(x%7)

def g(x): return x-1

def h(x, y): return (f(g(x)) % g(f(y)))

print h(10, 12)
 

############################################
 

def f(x, y):

    print "f",      # don't miss this print statement!

    return (x > y)

def g(x, y):

    print "g",      # ditto

    return (x > 2*y)

def h(x, y):

    print "h",      # ditto

    return x*y
 

print f(1, 0) and g(-1,2) and f(3,4) and g(5,6)

print h(0, 1) or h(2, 3) or h(4, 5)
 

2.  Reasoning Over Code  [20 pts]
Find arguments for the following functions f and h (not g) that make them return True.  You only need one set of arguments for each function, even if there are multiple correct answers.

def f(x,y):
    return ((type(x) == type(y) == int) and
            (100 > x > y > 0) and
            (x/10 + x%10 + y/10 + y%10 < 5) and
            (x % 10 == y % 10) and
            (x + y == 22))

############################################


def g(x, y):
    return x - x/y*y  # hint: what is this doing in general?

def h(x, y):
    return ((type(x) == type(y) == int) and
            (30 > x > 20) and (13 > y > 10) and  # hint: notice unusual bounds here
            (g(x, y) == 6))
 

3.      Quick Answers.  [20 pts]

a.       What is the common term we use to describe floating-point numbers x where (x % 1) equals 0?

b.      Give a Python expression that is True (not “Truthy”, but actually True) if the variable x refers to a value that is “Falsey” and False otherwise.

c.       Quick Answers [continued]
State and prove any of DeMorgan’s laws using truth tables.

d.      Give two expressions that use some Python operator where the semantics of the operator differ based upon the types of the operands.
 

4.      Free Response:  findRoot  [40 pts]
Recall the quadratic formula:
     
Using this formula, write the function findRoot(a, b, c) that takes three numeric values (so you may safely assume they are numeric) and returns the larger of the two values given by the quadratic formula above.  Your function may not crash for any inputs, but instead should return False if it cannot compute the numeric result.

 

5.      Bonus/Optional  [1 pt each]

a.       If x and y are integers where ((x%y == y%x) and (x != y)) is True, what must be true about x and y?

b.  What will this print?
def r(x, y): return (x*y == 0)*1 or x+r(r(x/3,y/4),x/5)
print r(4,4)