I chose to use the discussion of Python functions for my assessments exercise.
Given the following function definition, which of the code fragments below will print "the answer is 2″ (without the quotes) to the screen, without producing any errors?
def square(x=5):<br />
temp = x * x<br />
return temp<br />
A.
a = square()<br />
b = a / 12.5<br />
print "the answer is", b<br />
B.
b = square(a=2) / 2<br />
print "the answer is", b<br />
C.
a = 2<br />
b = square(a) / 3<br />
print "the answer is", b * 2<br />
D.
x = 1<br />
square(2)<br />
print "the answer is", x<br />
E.
a = '2'<br />
b = square(a) / 2<br />
print "the answer is", a<br />
There are five errors in the following code fragment. What are they?
`def add(a, b=2):
return a + b</p>
def subtract(a, b=2):
return a - b
print """If only one number is passed to the add or subtract functions,
they will add or subtract""", b, "from that number."
x = add[1,1]
print "x is", x
y = subtract(x)
print "y is", y
print "x / y is: ", x / y
w = subtract(add(1))
z = add(w 5)
print "If Pat has", z, "eggs, then she has", z / 12 * 100, "percent of one dozen."`