Review Questions for Exam 2

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 3/29/13.

This file was edited on 3/25/13 to include some file I/O questions.

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.

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: ")

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

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

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")

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

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

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".

11. What is a variable's scope?

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

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.

14. Explain how eval() works.

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().

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

12345678910111213... 20

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.
*   *
*   *
*   *

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.

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

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!
Then your program should print:
to the screen.

24. Given list L = [4, 2, 8]:
a. Write Python code that adds the number 15 at the beginning of the list (so now L is [15, 4, 2, 8]).
b. Write code that adds 10 to the end of the list (so now L is [15, 4, 2, 8, 10]).
c. Write code that adds all the elements in list L2 = [5, 1, 2, 4] to L. Use the concatenation operation.

25. What is the output?
myList = [1, 2, 5, 8]
print myList[1:-1]
print myList[-2]
print myList[:]

26. Describe the repetition operator (for lists), and give an example.

27. Write a program that reads strings from the user, and prints them to a file.

28. Write a function that takes a file name as its argument, opens the file for reading, and writes all the even integers from 2 to 100 to the file (one number per line).