Questions for Exam 1
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 2/27.
1. What is a CPU?
2. Give one example of an input device and one example of an output
3. What is a compiler?
4. Write an algorithm that displays the sum of all numbers which are
multiples of 3 between 1 and 1000. Now translate your algorithm into
5. Write a line of code that reads a line of text from the user.
myString = raw_input("Enter your text: ")
6. Write a line of Python code that reads a floating-point number from
myNum = input("Enter a floating point number: ")
7. Assume that val is an initialized variable of type float. Write a
Python statement that prints val to the console window. Display 3
after the decimal point.
print "%.3f" % val
8. Write a complete Python program that reads 100 floating-point
numbers from the user, and
prints the max, min and average value.
This program only computes the
min value and the average. The maximum is similar (you do it).
num = input("Enter the first number: ")
min = num # we've read one value, so it's the smallest
number seen so far
sum = num
for i in range(99): # need to read 99 more numbers and
update the min if we read a number smaller than current value of min
num = input("Enter next number: ")
sum += num
if num < min:
min = num
print "The smallest number was %f." % min
print 'The average is %f.' % (sum/100.0)
9. Write a Python program that reads 20 lines of text from the user and
prints the number of lines that contain the phrase "happy day".
count = 0 # count the lines that contain "happy day"
for i in range(20):
line = raw_input("Enter the next line:
if string.find(line, "happy day") >=
count += 1
print "%d lines contained \"happy day\"" % count
10. Write a Python program that reads a line of text from the keyboard
and prints its reverse to the screen.
text = raw_input("Enter your string: ")
revText = "" # set the reverse to the empty string initially
for i in range(len(text)-1, -1, -1):
text[i] # append character at index i in text
to the new string
print "Reversed string: " + revText
11. Write a function that takes (as a parameter) an integer variable
num and returns the sum of the integers between 0 and num.
sum = 0
min = 0
max = num
if num < 0:
min = num
max = 0
# compute sum of integers from min to
for i in range(min, max+1):
sum += i
# return the sum
12. What is the value and type of the result?
a. 17/8 Value: 2 Type: int
b. 24%5 Value: 4 Type: int
c. 14.0/4 Value: 3.5 Type: float
e. ("a" == "a") and ("b" != "B") Value:
True Type: bool
g. str(15 + 2) + " hellos" Value: "17
hellos" Type: str
h. 15 * 2 / 4
i. (5 < 4) or (not 3 == 3) Value:
False Type: bool
13. Write a Python program that reads positive integers from the user,
stopping when the user enters a 0 or negative integer, and prints the
average of the positive integers.
# sentinel loop set-up - read first integer
num = input('Enter first number (0 or negative to
# Initialize sum and count to 0. While num is
positive, add num to the sum, and and 1 to count.
sum = 0
count = 0
while num > 0:
count += 1
sum += num
input('Next number (0 or negative to stop): ')
if count > 0:
14. Explain how the functions str(), float(), int() work. Give an
example of how you would use one of them.
See class notes.
15. Write a for loop that prints the multiples of 3 between 3 and 90.
for i in range(3, 91, 3):
16. Write a few lines of code that reads 2 strings from the user, and
prints a message indicating whether or not they are the same string.
string1 = raw_input("Enter first string: ")
string2 = raw_input("Enter second string: ")
if string1 == string2:
print "they are the same!"
print "they are different"
17. Give an example that illustrates how the find() function works.
import string # find() is in the string library
if string.find("Hello, good morning", "morn") >= 0:
print "The string \"morn\" occurs
in the string \"Hello, good morning\"!"
18. Write a few lines of code that read an integer from the user, and
then prints the digits in the number, one per line.
# get integer from user - I'm actually assuming the
integer is positive.
num = input('Integer? ')
# print digits of num, one per line
while num > 0:
num = num/10
19. Write an if-elif-else statement that prints a message indicating
the value stored in int variable n is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 or
if n == 0:
elif n == 1:
elif n == 2:
elif n == 3:
elif n == 10:
print "%d is less than zero or greater than 10" % n
20. Consider writing code that prints the squares of the numbers
between 1 and n, where n is a value entered by the user. Do this with a
a. for loop
n = input("Enter an integer: ")
if n < 1:
print "invalid value"
for i in range(1, n+1):
b. while loop
If the user enters an integer less than 1, print an error message.
21. Write a function that takes a string as its argument, and returns
the reverse of the string. That is, if the argument is "happy day",
then the function returns the string "yad yppah". Then write a main
function that reads 15 strings from the user and prints the reverse of
for i in range(15):
raw_input("Enter the next string: ")
"Reversed string: " + reverseIt(text)
revStr = "" # initially
the reversed string is empty
# loop through theStr backwards,
adding each character to revStr
for i in range(len(theStr)-1, -1,
revStr += theStr[i]
# return the reversed string from
main() # execute the main function
22. Write a function that takes a positive integer and returns the ones
digit of the integer.
# return the ones digit of the
onesDigit = num%10
23. Write a program that reads a string from the user, and prints the
string, one character per line.
myString = raw_input("Enter a string: ")
for i in range(len(myString)):