CMSC 201 Fall 2024 Practice Final Exam PDF
Document Details
Uploaded by FastestGrowingBeryllium
UMBC
2024
UMBC
Tags
Summary
This is a practice final exam for CMSC 201 at UMBC. The exam covers multiple choice questions, programming problems, and other types of questions.
Full Transcript
CMSC 201 Fall 2024 Practice Final Exam 165 points *(5 Extra Points) Circle the discussion section you are enrolled in: 11 - Abel 12 - Liam 13 - Sammie 14 - Richard 15 - Ahmad 16 - Nicholas 41 - Anuar...
CMSC 201 Fall 2024 Practice Final Exam 165 points *(5 Extra Points) Circle the discussion section you are enrolled in: 11 - Abel 12 - Liam 13 - Sammie 14 - Richard 15 - Ahmad 16 - Nicholas 41 - Anuar 42 - Mahmood 43 - Ellie 44 - Claire 45 - Sean 46 - Jessica _________ Write your name (neatly!) on the line below: ______________________________________________ First Name (Given) Last Name (Family) ______________________________________________ GL login id (your umbc email without the @umbc.edu) You must use a pencil on this exam. Exams taken in pen will not be graded. WRITE YOUR NAME AT THE TOP OF EVERY PAGE Undergraduate Honors Statement In this course, each student assumes the responsibilities of an active participant in UMBC’s scholarly community, in which everyone’s academic work and behavior are held to the highest standards of honesty. Cheating, fabrication, plagiarism, and helping others to commit these acts are all forms of academic dishonesty, and they are wrong. By signing below, I agree to follow the university's and the course's Academic Integrity policies. __________________________ Sign Your Name Here Name: _________________________________________ Multiple Choice: 30 points (3 points each) Choose the correct answer by clearly filling in the bubble next to the correct option. MAKE SURE THAT YOU CLEARLY AND CLEANLY BUBBLE IN THE OPTION YOU SELECT 1. How many times will this loop run? for x in range(3, 17, 2): print(x) ○ 3 ○ 5 ○ 7 ○ 14 2. Which line has the recursive call? 1 def find_depth(my_list) : 2 current_max = 0 3 if my_list: 4 for sublist in my_list: 5 d = 1 + find_depth(sublist) 6 if d > current_max: 7 current_max = d 8 return current_max ○ Line 2 ○ Line 3 ○ Line 5 ○ Line 8 3. For the line of code here: not(my_int % 2) when would it be True? ○ When my_int is odd. ○ When my_int is even. ○ It would always be true. ○ It would never be true. 4. When a function returns: ○ Local variables are stored until the function is called again. ○ Local variables that are not returned leave scope and cannot be accessed. ○ Local variables become global variables. 5. Select the correct statement. ○ List is a mutable type and it is passed by value. ○ List is an immutable type and it is passed by value. ○ List is an immutable type and it is passed by reference. ○ List is a mutable type and it is passed by reference. Page 2 of 11 Name: _________________________________________ 6. Based on the error message given by Python below, what is the cause of the error? Traceback (most recent call last): File warp_drive.py", line 3, in my_favorite_things = 'Star Trek' TypeError: 'NoneType' object does not support item assignment ○ The list my_favorite_things is longer than three elements. ○ 3 is not a valid index for a list. ○ The variable my_favorite_things is actually None. ○ Lists are immutable so you cannot assign to an element. 7. Consider the following list [-5, 2, 0, 5, 5, 6, 17, 22, 37, 45, 51]. How many comparisons will be made by linear search when the target is 17? (Hint: The comparison to determine if you found the element counts as one of your comparisons.) ○ 2 ○ 12 ○ 7 ○ Linear search cannot be used on this list 8. Consider the following list [-5,-2, 0, 5, 5, 6, 17, 22, 37, 45, 51]. How many comparisons will be made by binary search when the target is 17? (Hint: The comparison to determine if you found the element counts as one of your comparisons. On even sized lists, round to the higher value to search. ) ○ 0 ○ 4 ○ 17 ○ Binary search cannot be used on this list 9. Can every for loop be rewritten as a while loop? Also, is the reverse true? Can every while loop be rewritten as a for loop? ○ All for loops can be written as while loops, but not all while loops can be recoded into for loops. ○ All for loops can be written as while loops, and all while loops can be rewritten as for loops. ○ Not all for loops can be written into while loops, but all while loops can be rewritten into for loops. ○ Neither is true. 10. If you open a file in write mode, but the file already exists: ○ The file will not be opened since it already exists. ○ The file will be opened and blanked/zeroed out. ○ The file will be opened at the cursor placed at the end of the file. Page 3 of 11 Name: _________________________________________ Creating Boolean Expressions: 15 points (5 points each) Create the requested Boolean expressions below. You do not need an “if” in your answer. Remember a boolean expression is a single statement which will evaluate to True or False. 11. Write an expression which returns True if and only if the list scotsmen is non-empty and ship_of_theseus has at least 12 elements in it. 12. Write an expression which returns True if and only if the string game is either equal to chess or checkers (in lower case) and the board_size (a 2d list) is 8x8. Assume that the number of columns is the same for every row. 13. Write an expression that returns True if and only if the string the_semester is "over" regardless of case, or the integer num_assignments is between 5 and 13 inclusively (can be 5 and 13). Dictionary Code Evaluation: 15 points (3 points each) Evaluate each dictionary manipulation, and write down what its output would be, assuming that each statement is independent, and that the starting dictionary is composers, as shown below. If the code will result in an error, state why. composers = {"Beethoven": 9, "Chopin": 24, "Vivaldi": 4} 14. print( composers["Scarlatti"] ) 15. print( composers.get("Scriabin", "Who?!") ) 16. print( composers.get("Vivaldi", "Who?!") ) Page 4 of 11 Name: _________________________________________ 17. print(composers["Chopin"], composers["Beethoven"], composers["Vivaldi"]) 18. composers['Scarlatti'] = 17 print( composers ) Binary, Hexadecimal, and Decimal Conversion: 20 points (4 points per problem) Convert the original number, and write your final answer on the line provided. Show your work for the possibility of partial credit. 19. Convert the decimal number 64 to binary: _________________ to hexadecimal: _________ 20.Convert the decimal number 183 to binary: _________________ to hexadecimal: _________ 21. Convert the binary number 1101 1011 to decimal: _________ to hexadecimal: _________ 22. Convert the binary number 1010 0000 to decimal: _________ to hexadecimal: _________ 23. Convert the hexadecimal number FD30A15E to binary: _____________________________________________________ (Make sure to leave space between each group of four!) Page 5 of 11 Name: _________________________________________ Code Evaluations (part 1): 20 points (5 points each) Evaluate each code snippet below and answer the question asked. You may assume that the code is syntactically correct. Make sure that you pay attention to the formatting of the output when you write down your answer. Put your final answer in the box. 24. When the code below is run, what does it print out? print("\\\\\nMerry\nChristmas!\\") 25. When the code below is run, what does it print out? def list_add(my_list): my_list.append('3') if __name__ == '__main__': the_list = ['1', '7'] list_add(the_list) list_add(the_list) print(the_list) 26. What is the output when the lines of code below are run? a_list = ['Python', 'is', 'a', 'fun', 'time'] print(a_list[2:4]) print(a_list[4:10]) print(a_list[:3]) 27. What is the output when the lines of code below are run? def hello(count): if count < 0: return print("hello") if count % 5 == 0: hello(count - 8) else: hello(count + 3) if __name__ == '__main__': hello(15) Page 6 of 11 Name: _________________________________________ Code Evaluations (part 2): 20 points (5 points each) 28. When the code below is run, what does it print out? the_matrix = [[4, 2, 7], [8, 5, 6], [9, 1, 3]] print( the_matrix ) print( the_matrix ) print( the_matrix ) 29. When the code below is run, what does it print out? def three_plus_one(x): return 3 * x + 1 if __name__ == '__main__': y = 1 for i in range(4): y = three_plus_one(y) print(y) 30.When the code below is run, what does it print out? def rec_fun(a): if a == 0: return '' if a % 2 == 0: return 'a' + rec_fun(a // 2) else: return 'b' + rec_fun(a // 2) if __name__ == '__main__': print(rec_fun(5)) 31. When the following code executes, what is the printed output? if __name__ == '__main__': a = [1,2] b = list(a) b += 3 print(a) Page 7 of 11 Name: _________________________________________ Debugging (part 1): 15 points (1 point per error, 2 points per correction) The code below has errors; you must find five. The errors may be syntax errors or logic errors, and there may be more than one per line; examine the code carefully to find them. Indicate each of the errors you find by writing the line number and correction in the space provided below. Each error can be solved with a single fix, and should not be counted twice. 32. You must find and correct five of the errors. (Do not fix more than five errors!) 1 def count_vowels(word_string): 2 """ count_vowels is a recursive function which counts the vowels 3 in a string 4 word_string: str 5 """ 6 if word_string: 7 return 0 8 9 if word_string.lower() == ['a', 'e', 'i', 'o', 'u']: 10 return 1 + count_vowels(word_string[1:]) 11 else: 12 return count_vowels(word_string[2:]) 13 14 if __name__ == '__main__': 15 my_words = input('Enter some words here') 16 while my_words != 'quit' 17 result = count_vowels() 18 print('The number of vowels is' + result) 19 Line Number Correction – you must explain how to fix it! Debugging (part 2): 15 points (1 point per error, 2 points per correction) Page 8 of 11 Name: _________________________________________ 33. You must find and correct five of the errors. (Do not fix more than five errors!) 1 def sum_positives(the_list) 2 """ 3 : the_list (a list of integers) 4 :return: the sum of all the positive numbers in the list 5 """ 6 7 for i in range(the_list): 8 if the_list[i]