Review Questions for Exam 1
Some 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 2/27.

1. What is a CPU?

2. Give one example of an input device and one example of an output device.

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

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 the user.

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

import string
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") >= 0:
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):
revText += 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 max
for i in range(min, max+1):
sum += i
# return the sum
return 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
d. math.sqrt(16)
e. ("a" == "a") and ("b" != "B")  Value: True  Type: bool
f. round(12.56999)
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.

def main():
# sentinel loop set-up - read first integer

num = input('Enter first number (0 or negative to stop): ')

# 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
num = input('Next number (0 or negative to stop): ')

if count > 0:
print sum/float(count)

main()

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):
print i

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!"
else:
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.

def main():

# 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:
print num%10
num = num/10

main()

19. Write an if-elif-else statement that prints a message indicating whether the value stored in int variable n is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 or 10.

if n == 0:
print "zero"
elif n == 1:
print "one"
elif n == 2:
print "two"
elif n == 3:
print "three"
...
elif n == 10:
print "ten"
else:
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"
else:
for i in range(1, n+1):
print i*i

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 each string.

def main():
for i in range(15):
text = raw_input("Enter the next string: ")
print "Reversed string: " + reverseIt(text)

def reverseIt(theStr):
revStr = ""   # initially the reversed string is empty
# loop through theStr backwards, adding each character to revStr
for i in range(len(theStr)-1, -1, -1):
revStr += theStr[i]

# return the reversed string from the function
return revStr

main()   # execute the main function

22. Write a function that takes a positive integer and returns the ones digit of the integer.

def getOnes(num):
# return the ones digit of the integer num
onesDigit = num%10
return onesDigit

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)):
print myString[i]