# THESE PROBLEMS ARE JUST FOR PRACTICE AND TO GET AN IDEA OF KINDS # OF QUESTIONS THAT MIGHT BE ON THE MIDTERM. # THE ACTUAL QUESTIONS MAY BE SIGNICANTLY DIFFERENT. ################# ## Reading Code # ################# # What does the following code print k = 0.3 if k >= 0.33: print(k) elif k < -5: print(k*2) elif k > -4.9: print(k+2) else: print(k - 1) # What does the following program print? for k in range(2,6): print(k+5) # What does the following program print? for k in [2,1,9]: print(k**2) # What is the value of k after running the # following program? k = 2 for ch in "Hi Kid": k = k + 2 # What does the following code print test_val = 10 while test_val > 2: print(test_val) test_val = test_val - 2 print("done") # What does the following code print test_val = 10 while test_val > 2: print(test_val) test_val = test_val - 2 print("done") # What does the following code print test_val = 10 while test_val < 2: print(test_val) test_val = test_val - 2 print("done") # What does the following code print def hello(): print("hello") return "bye" print("Good day") # What does the following code print def hello(): print("hello") return "bye" print("Good day") hello() # What does the following code print def hello(): print("hello") return "bye" print("Good day") print(hello()) # What does the following code print def F(y,L,x): L[2] = 9 L[3] = L[2] L[2] = 55 y = x + y return y x, z = 2, 6 myList = ['good', 8, 'aunt', 'be', 100] z = F(z, myList, x) print("x:",x) print("z:",z) print("myList:", myList) # What does the following code print def F(L): if L[2] > 7: L[2] = 7 else: L[2] = 0 return L[0] def G(x): x = x + 10 if x < 20: return x else: return 20 S = [4,5,9,5] x = 600 print("F return:", F(S)) print("G return:",G(x)) print("x =",x," and S =",S) print("Composition:", G(F(S))) print("x =",x," and S =",S) ################ # Writing Code # ################ # 1 # Write a program that swaps the values in # variables A and B. # 2 # Write a function that takes in 5 numbers # as input and outputs their average. # 3 # Write a program that prompts the user # for two numerical inputs (B and H) # and prints out the area of a triangle # with base B and height H. # 4 # Do the last program again, but do it so # that it complains if either the base # or height is not a valid number and quits. # 5 # Do the last program again, but do it so # that it complains if either the length # or width is not a valid number # AND keeps prompting the user for a valid input till they give one # 6 # One pound is 0.45kg. # 1) Write a function that takes a number # as input and outputs its conversion # to kg. # 2) Write a function that takes a number # as input and outputs its conversion # to pounds # 7 # Write a program that does the following: # It repeatedly prompts the user for characters # (assume all the characters are among A, B, C, D, E) # and prints out the letter that occurs most often. # If there is a tie, then the alphabetically first character. # Hint: Use a list. # 8 # Write a function that takes 3 numbers as # input and outputs the minimum of the 3 # numbers # 9 # Write a function Censor that takes in a string S # and a character C, and then replaces all occurences # of C by "X", then returns this censored string. # Ex: Censor("hello lil one", "l") returns # "heXXo XiX one" # 10 # Write a function that takes a positive # integer n as input, and outputs the number # of positive even integers less than n. # (There is a way to do this and there is a better way to do this ...) # 11 # Write a function that takes a positive # integer n as input, and outputs the number # of positive even integers less than n, AND # the number of positive odd integers # less than n. # 12 # Write a function that takes a positive # integer n as input, and ouputs the sum # of the positive even integers less than n. # 13 # Write a function that takes a positive # integer n as input, and ouputs the sum # of the positive odd integers less than n. # 14 # Write a function that takes a list of # numbers as input and returns the sum of # the numbers in the list. #15 # Write an Idiot Checker: # It asks the user for a whole number between 1 and 10 inclusive # It keeps on asking till the user enters such a number # Once the user finally gives a proper answer it tells the user how # how much of an idiot she/he is depending on how many tries it # took to enter a number between 1 and 10. #16 # Write The Dumbest Game In The World: # It is played between two people, taking turns entering anything they like # Whoever enters "Q" first loses.