Exam1 Soln
Exam1 Soln
6.189 Exam
Session 5
Administrivia
Name:
MIT ID:
Instructions:
1. Err..complete the questions :). Warning: they do increase in difficulty.
2. No calculators, no laptops, etc.
3. When we ask for output, you DON'T have to write the spaces/newlines in.
Program Text:
print “X”,
print “X”,
Output:
XX
meaning: don't just name all your variables a, b, c (exception: if you're writing a quiz to test your
Program Text:
a = 5
b = a + 7
a = 10
print b
Page 2
Output:
12
What is the type of each of the following expressions (within the type function)?
print type(5)
int
print type("abc")
string
print type(True)
boolean
print type(5.5)
float
print type(12/27)
int
print type(2.0/1)
float
print type(12 ** 3)
int
Program Text:
Output:
string
Page 3
print 5 == 5.0
True1
print float(1/2)
0.0
print float(1)/2
0.5
print 5 == "5"
false
Program Text:
a = 20
print 15-(a-15), “,”,
a = 10
print 15-(a-15),
Output:
10,20
Day 3: Conditionals
Tips/Notes:
The if statement executes a sequence of statements only if some condition is true. This condition
can be anything.
1
Scheme (the initial language taught at MIT before Python) took type to an extreme. 5.0 and 5 were not
considered equal because one was an integer and the other was a float. You weren’t even allowed to do (4 + 6.0.)
Python is more rational – it treats the two values as equal.
Page 4
elif / else is optional. Remember that at most one block of statements is executed. else occurs if
none of the above conditions are satisfied.
Problem 4: Basics
Consider the following code
Program Text:
a = ?
if a > 10 and a % 6 = 3:
print "a",
elif a > 10 and a < 20:
print "b",
else:
print "c",
Give a value for a that would produce the following outputs. If no value of a would produce that output,
write none.
Value of a: Output:
none a b
Value of a: Output:
15,21,27,… a
Value of a: Output:
Value of a: Output:
Any other c
Value of a: Output:
Program Text:
a = 5
while a < 8:
print "X",
Output:
Loops forever
Program Text:
a = -1
while a < 3:
print "X",
a = a + 1
Output:
XXXX
Program Text:
a = 1
while a % 7 != 0:
if a % 2 == 0:
print "O"
if a == 2:
print "X"
a = a + 1
Output:
OXOO
Page 6
Program Text:
keep_going = True
a = 0
b = 0
while keep_going:
print "O"
a = a + 5
b = b + 7
if a + b >= 24:
keep_going = False
Output:
OO
Program Text:
keep_going = True
a = 0
b = 0
while keep_going:
print "O"
if a + b >= 24:
keep_going = False
a = a + 5
b = b + 7
Output:
OOO
The remaining two variants are duplicates of the first two with >= replaced by >.
Program Text:
keep_going = True
a = 0
b = 0
while keep_going:
print "O"
a = a + 5
b = b + 7
if a + b > 24:
keep_going = False
Page 7
Output:
OOO
Program Text:
keep_going = True
a = 0
b = 0
while keep_going:
print "O"
if a + b > 24:
keep_going = False
a = a + 5
b = b + 7
Output:
OOOO
Problem 8: Freebie!
What is the output of the following code? If the code does not terminate, write error.
Program Text:
a = 0
while a < 3:
while True:
print "X",
break
print "O",
a = a + 1
Output:
XOXOXO
Page 8
Problem 9: (insert evil laugh) ..is what I’d like to say. Still not that bad, though
What is the output of the following code? If the code does not terminate, write error.
Program Text:
a = 1
while a < 3:
while a < 3:
print "O",
a = a + 1
Output:
Loops forever
Program Text:
a = 1
while a < 3:
if a % 2 == 0:
b = 1
while b < 3:
print "X",
b = b + 1
print "O",
a = a + 1
Output:
OXXO
Extra Credit (mainly due to time constraints.) Solve this if you finish early!:
Program Text:
a = 1
while a < 3:
b = 1
while b < 3:
if a == 2:
print "X",
print "O",
b = b + 1
print "O",
Output:
oooxoxoo
Page 9
Day 2: Functions
Tips/Notes:
A function is just a named sequence of statements. We usually define functions at the beginning of
code – definitions just associate the name with the sequence of statements.
Functions can take parameters (within the parenthesis suffix) and can return information via return
return is NOT a function. Like if, while, .. its a keyword: a basic command of the language.
You can find out more information about functions using the help(x) function, e.g. help(sqrt).
Remember to write from math import * first.
Program Text:
def f(a):
a = a + 5
return a
b = 0
f(b)
print b, ",",
b = f(b)
print b
Output:
0,5
Program Text:
def f(x):
print "X",
if x <= 1:
return 1
else:
return x+f(x-1)
Page 10
Fill out the following table for the return value and output of each function call.
f(1) 1 X
f(2) 3 XX
f(3) 6 XXX
f(4) 10 XXXX
Page 11
Extra stuff
If you were reasonably comfortable with this test, here is some extra stuff that you might find useful
(you can rip this page out if you like.)
Don’t forget about using # to write notes/comments in your code!
Instead of always writing a = a + 5 or a = a / 7, you can use the shorthand a += 5 and a /= 7. Be
careful not to get confused by this notation:
Program Text:
a = 5
b = a
a += 3
print b #still 5
String stuff
• You can use either a single quotation mark (‘) or a double quotation mark for strings. The only
difference is either one can contain the other (‘This is a “test”’ is valid, “This is a ‘test’” is valid,
“This is a “test”” is not valid.)
• You can use “\n” to insert a newline (Enter key) in a string.
• You can define multi-line strings using the triple-quotation """ operator.
Program Text:
print “““Thisis a sentence.\nThis sentence is on the second line.
This sentenceis on the third line.
This is on the fourth.”””
You can write a description of a function by putting a string as the first line of the function.
Program Text:
def hypo(a,b):
"""This function returns the length of the hypotenuse defined by
the right triangle with side lengths a,b"""
return sqrt(a*a + b*b)