CSCI 110, Spring 2011
Home | | Course Schedule | | Assignments | | Lecture Notes
Practice Problems
The following problems are intended to help you study for the exam.
1)Given these boolean variables: x = true, y = false, z = true What is the value (true/false) of each of the following? i) x and (y or z) Answer: True ii) (not x) or (not(y and z)) Answer: True iii) y or (x and not z) Answer: False 2)Consider a program that does the following: Prompt the user to input a number between 0 and 100. Input the number from the keyboard If the number is greater than 50 and less than 100 write: XX is in the top 50 percent. where XX refers to the number inputted by the user. Otherwise, if the number is less than or equal to 50 write: XX is in the bottom 50 percent. Otherwise, write Illegal number entered. a) Draw a decision tree for the program. (solution given in class) b) Write a Python program to execute the program #Program lowOrHigh.py number = input("Enter a number between 0 and 100: ") if number > 50 and number < 100: print number, "is in the top 50 percent." elif number <= 50: print number, "is in the bottom 50 percent." else: print "Illegal number entered." 3) Compute the value of the following expressions: a) 6 / 3 * 2 Answer: 4 b) (17 - 11) / 3 + 3 Answer: 5 c) Add parentheses to part b so that the addition is performed before the division. Answer: (17 - 11) / (3 + 3) d) What is the value of c) ? Answer: 1
4) What is the value of sum after the execution of the following code?
sum = 0 for counter in range(1, 5): sum = sum + 2 * counterAnswer: 20
5) What is the output of the following code fragment (mySentence and partial are both strings)?
mySentence = "It was a dark and stormy night" partial = mySentence[10:13] print partial Answer: ark6) What is wrong with the following fragments of Python?
#Program Error1 number = raw_input("Please enter a number: ") number = number/2.0 print number Answer: raw_input( ) is used to input a number. It should be input( )
#Program Error2 response = raw_input("Do you like chocolate? ") if response == "yes": print "Me too!" else: print "That's too bad." Answer: else should be indented by the same amount as if
#Program Error3 secret = "csci110" response = raw_input("Enter the password: ") if response = secret: print "Access granted." else: print "Access denied. Answer: The = in the conditional should be ==
#Program Error4 count = 0 while count < 10 print count count = count + 1 Answer: The colon after the header of the while loop is missing.
7) Consider the following while loop: sum = 0 count = 5 while count <= 10: sum = sum + count count = count + 1
a) Fill in the following table for the values of count and sum each time through the loop (there may be more rows than you need in the table):
b)Write a for loop that does the equivalent of the above loop.
sum = 0 for count in range(5, 11): sum = sum + count
8. Consider the following Python code:
sum = 0 for i in range(1, 5): sum = sum + i print sumWhat is value of sum printed on the screen?
Answer: 10
9. Fill in the necessary code to fill in the depositList array with deposit amounts entered by the user. You should keep track of the sum of all the deposits made, so that the print statement at the end of the program makes sense. You can assume there are exactly 1000 deposits made.
Program deposit.py sum = 0 depositList = [ ] for i in range(1000): deposit = input("Please enter amount of deposit: ") depositList.append(deposit) sum = sum + deposit print "The sum of the deposits: $%6.2f"% sum