15-110 Spring 2011 Quiz 2
20 minutes

SHOW YOUR WORK.  Correct answers without supporting work will not receive credit.
For questions saying “in 10 words or less”, we will only grade the first 10 words you write!

1.       [20 pts; 2 pts each] Indicate what each statement will print:

Statement:

Prints:

print 5*2

 

print 5**2

 

print 5/2

 

print 5%2

 

print 1.0*5/2

 

print 5/2*1.0

 

print min(max(3, abs(-5), min(8, 1)), 2**232)

 

print len("Go Team!")

 

print "Go Steelers"[5:8] + str("Win!".find("n"))

 

print ((2 <= 3) and ((6 > 8) or ("a" <= "q")))

 

2.       [10 pts] In 10 words or less, what is short-circuit evaluation?



3.       [10 pts] In 10 words or less, why should you never use “==” when comparing floating-point numbers?



4.       [10 pts] In 10 words or less, what is the point of “dove tailing” (what problem is it solving)?



5.       [10 pts] In “Decoding the Value of Computer Science”, Kevin Carey explains how he modeled a real law in SAS (a programming language), then cleaned up his program, which led to passage of a new, streamlined version of the original law.  What was the general subject matter of that law?


6.       [10 pts] In “A journalist’s introduction to computational thinking”, Kim Pearson lists how various traditional practices have been informed by computational thinking.  For example, “getting news tips from sources” has evolved into “crowd-sourcing”.  List one more example she gives of a traditional practice and any single way she provides that that practice has been informed by computational thinking.



 

7.       [15 pts] What will the following code print?

def f(s, x):
    return s[x:x+2] + s[x-2:x]
y = 3
t = "abcdefg"
print f(t, y)



 

8.       [15 pts] Write the function tensDigit that takes one parameter, a non-negative integer, and returns the ten’s digit of that number.  So tensDigit(1234) would return 3.








 

9.  Bonus/Optional [5 pts]:  What will the following code print?  Show your work!

def f(a,b,c):

    b += a/c

    c -= max(a,b)/min(a,b)

    return a+b*c

print f(1, f(2,f(3,4,3)/4, 2), 1)