Review Questions for Exam 2
Solutions

This review sheet is intended to give you some practice questions to use in preparing for our first midterm. It is not necessarily complete. The first exam covers the reading assignments, programming projects and class/discussion material through Friday 4/2/10.

1. Re-work all questions on the exam 1 review sheet and exam 1. I will assume that you can work all these questions quickly and accurately.

2. Work the discussion assignment questions again.

3. Write Python code that reads an integer from the user. If the integer is 1, it prints the word "one", if it's 2, it prints the word 2, ..., if it's 5 it prints the word "five". If the integer is less than 1, print "non-positive" and if it's greater than 5, print "more than 5". Use an if-elif structure.

n = input("enter an integer")
if n == 1:
print "one"
elif n == 2:
print "two"
elif n == 3:
print "three"
elif n == 4:
print "four"
elif n == 5:
print "five"
elif n < 1:
print "non-positive"
elif n > 5:
print "more than 5"

4. Print the value stored in x in a field of minimum width 15 and with 3 digits after the decimal point.
x = input("Enter a floating point number: ")

See string formatting notes

5. Fill in the blanks below:
x = input("Enter an integer: ")
y = input("Enter another integer: ")
print "%__d__ + %___d____ is a big %__s___!" % (x, y, "number")

6. Explain what it means to say that + is overloaded.

See class notes.

7. What is the value of the Python expression? If the expression is not valid, write "error". The way in which you write the value should indicate its type.
a. 25%2
b. "call " + 911
c. 3 - 8 + 6 * 4 % 2
d. round(3.55)
e. str(411)
f. (20 + 20 + 20) / 3
g. not (true or false)
h. (3 > 1) or ("help" > "fire")

This are easy to check with the interpreter.

8. Write a couple of lines of code that indicate whether or not the string referenced by variable s contains the substring "hi".

if string.find("hi") >= 0:
print "contains hi"
else:
print "does not contain hi"

9. Write a function reverseIt() that takes a string s and returns the reverse of s.

We did this one in class.

10. Write a function evenPos() that takes a string s and returns the string that contains the characters of s at even indices. That is, if s is "helloworld" then the string returned by evenPos() should be "hlool".

def evenPos(s):
str = ""
for i in range(0, len(s), 2):
str += str[i]
return str

11. What is a variable's scope?  See online class notes.

12. Write a function numTimes(n) that returns a tuple containing: n, 2n, 3n, and 4n.

def numTimes(n):
return n, 2*n, 3*n, 4*n

13. Write a program that computes the sum of a list of integers entered by the user. The user hits the enter key when they have no more numbers to enter.

We did this sentinel loop example in class.

14. Explain how eval() works.

See class notes.

15. what is the output?
s = "hello world"
print s[-2]
print s[-3]
print s[0]
print s[2:]

16. Describe how the functions count(), upper() and capitalize() in the string library work.

17. Write a Python program which contains two functions, main() and maximumValue().

• maximumValue(x, y, z) -- returns the largest of the 3 inputs, without using any math module functions.
• main() -- prompts the user for 3 integers, and then calls maximumValue() to determine which is largest. Then prompts the user for 3 strings and prints the largest.
This problem was worked in discussion.

18. Write a program that prints the following output. You may only print one character at a time.

1
12
123
1234
12345
123456
1234567
12345678
...
12345678910111213... 20

for i in range(1, 21):
for j in range(1, i+1):
sys.stdout.write(str(j))
print  # go to new line

19. Write a function printBox(m, n). If m and n aren't both positive, do nothing. If they are both positive, print a box like this, where m is the number of rows and n is the number of asterisks in the top (and bottom) rows.
****
*   *
*   *
*   *
****

def printBox(m, n):
if m > 0 and n > 0:
print "*" * n
s = " "* (n-2)
s = "*" + s + "*"
s = s + "\n"
print s*m
print "*" * n

20. Write a function getPowers(x) that returns a tuple containing x, x^2, x^3, and x^4. Then write a main() function that reads 10 integers from the user, and for each integer read, prints that value raised to the powers 3 and 4.

21. Write a function that takes the name of an input file as its argument, and returns the number of lines in the file that contain the word "hello". Then write a main() function that calls this function for a text file you've created.

I've left it to you to write the main function.

def countHello(myFile):
# myFile is a string
f = open(myFile, "r")
count = 0
for line in f:
if line.find("hello") >= 0:
count = count + 1
f.close()
return count

22. Write a Python program that creates a new file named outFile.txt and prints the following to it:
1 2 3
4 5
6

Done in discussion sections.

23: Write a python program that reads all the lines from a file inFile.txt and prints the last character in each line to the screen.
If inFile.txt contains:
hello world
Elvis lives!