mock-final-exam

pdf

School

University Of Arizona *

*We aren’t endorsed by this school

Course

110

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

12

Uploaded by ProfessorBeaver3611

Report
CSc 110 Introduction to Computer Programming I Final Exam – Practice Instructor: Adriana Picoral Spring 2023 This is an individual exam, and there will be a 120 minute window for you to complete it. Do not cheat off of those nearby you. Make sure to write your responses clearly and legible. There’s no need to add comments or docstrings to your code. Each question has a designated box where your response should go. Use a DARK pen or pencil, and write INSIDE the answer boxes provided. Anything outside the box will not be considered as part of your response. You may do extra work to arrive at the response, but the response MUST go in its designated box. Anything outside the box will not be considered as part of your response. If you have any questions, please raise your hand. 1
Question 1 A) True or False: The values in a dictionary must be unique. RESPONSE 1A: B) True or False: The values in a set are unique. RESPONSE 1B: C) True or False: The values in a list must be unique. RESPONSE 1C: D) True or False: Sets maintain the ordering of elements. RESPONSE 1D: D) What are the two methods to remove a value from a list? RESPONSE 1D: F) What data structure in Python is not mutable? RESPONSE 1F: 2
Question 2 Evaluate the code below. Enter in each box what the last line of code in each chunk prints. When the code throws an error, write ERROR in the response box. # 2A numbers = [ 1 , 2 ] numbers.append( 2 ) print(numbers) RESPONSE 2A: # 2B numbers = [ 1 , 2 ] numbers.insert( 1 , 3 ) print(numbers) RESPONSE 2B: # 2C numbers = [ 1 , 2 ] print(numbers[ 2 ]) RESPONSE 2C: # 2D numbers = { 1 , 2 , 3 , 1 , 2 } print(numbers) RESPONSE 2D: # 2E print( 2 ** 3 % 3 ) RESPONSE 2E: 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Question 3 – Bubble Sort def bubble_sort(items): swapped = False end = len(items) - 1 while not swapped: swapped = True for i in range(end): if items[i] > items[i + 1 ]: items[i], items[i + 1 ] = items[i + 1 ], items[i] swapped = False end -= 1 Using bubble sort (code for reference above), how many sweeps and swaps would it take until the list gets sorted? Show your work. Indicate the number of sweeps and swaps in their designated boxes. [10, 4, 2, 10, 5, 7] SHOW YOUR WORK FOR QUESTION 3: SWEEPS: SWAPS: 4
Question 4 Write a Python function that does the following: 1. Its name is trim_ends 2. It has one parameter: a 2D list 3. It iterates through the rows of the 2D list 4. It removes the first and last element of each row (if the sublist is not empty) 5. It mutates the parameter list (it does not create a new 2D list) – it does not return anything Test case: numbers = [ [ 10 , 20 , 200 , 40 ], [ ], [ 10 ], [ 1000 , 1000 , 10 ], [ 20 , 30 , 4 , 100 ] ] trim_ends(numbers) print(numbers) [[20, 200], [ ], [ ], [1000], [30, 4]] RESPONSE FOR QUESTION 4: 5
Question 5 Write a python function that does the following: 1. Its name is create_list 2. It takes two arguments, a set of strings and an integer n 3. It returns a list that contains each string from the set repeated n times items = { "banana" , "apple" , "pear" } print( create_list(items, 2 ) ) [ ' banana ' , ' banana ' , ' apple ' , ' apple ' , ' pear ' , ' pear ' ] RESPONSE FOR QUESTION 5: 6
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Question 6 See the python code and the contents of the file name data.txt . The python code writes content to a file named result.txt . You must determine what the contents of result.txt will be after the code runs. Put your answer in the response box. data.txt one silver edging trees leaves are green this simple request is finally final countdown def is_acceptable(x): for i in range( 0 , len(x) - 1 ): if x[i] == x[i + 1 ]: return True return False def main(): data = open( ' data.txt ' , ' r ' ) result = open( ' result.txt ' , ' w ' ) for line in data: words = line.strip( ' \n ' ).split( ' ' ) for word in words: z = is_acceptable(word) if z: result.write(word + ' \n ' ) data.close() result.close() main() RESPONSE FOR QUESTION 5: 7
Question 7 Write a function that does the following: 1. Its name is star_vowels 2. It has one string as parameter 3. It returns a new string of the same length as the parameter string, with every vowel replaced by a star ( "*" ) print( star_vowels( "banana" ) ) # "b*n*n*" print( star_vowels( "a" ) ) # "*" print( star_vowels( "apple" ) ) # "*ppl*" print( star_vowels( "" ) ) # "" RESPONSE FOR QUESTION 7: 8
Question 8 Write a function that does the following: 1. Its name is total 2. It has one parameter named file_name , being the name of a file to read 3. It expects that the file to read has an integer number on it per line 4. It computes the total of all the numbers from the file 5. It should return the total Example of data.txt file 5 1 5 20 print( total( "data.txt" ) ) # 31 RESPONSE FOR QUESTION 8: 9
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Question 9 Write a function that does the following: 1. Its name is average_rows 2. It has one parameter named lists , being a 2D list of float numbers 3. For each list (row) within the 2D list, it should calculate the average of the numbers within, round it at two decimals, and place the resulting average in a new list at the same index 4. It returns the list of the averages print( sum_rows([[ 1.2 , 5.4 , 4.3 , 2.0 ], [ 0.0 , 1.0 ]]) ) # [3.23, 0.5] print( sum_rows([[], [ 10.5 ]]) ) # [None, 10.5] print( sum_rows([[ 1.0 ], [ 2.5 , 3.5 , 4.5 ], [ 0.0 , 0.0 ], [ 0.0 , 2.0 ]]) ) # [1, 3.5, 0.0, 1.0] RESPONSE FOR QUESTION 9: 10
Question 10 Write a function that does the following: 1. Its name is mutate_dict 2. It takes two arguments: a dictionary with string keys and integer values, and a set of strings 3. It mutates the dictionary argument adding the strings in the set as keys in the dictionary: 4. if the key already exists in the dictionary, do not change anything 5. if the key does not exist in the dictionary, create with with the value zero associated with it test_dictionary = { "z" : 1 , "x" : 2 , "r" : 20 } mutate_dict(test_dictionary, { "a" , "z" , "r" , "b" } ) print(test_dictionary) # {"z": 1, "x": 2, "r": 20, "a": 0, "b": 0} RESPONSE FOR QUESTION 10: 11
Question 11 Write a Python function that does the following: 1. Function name is remove_vowel_ending 2. It takes a list of strings as argument (you can assume strings are never empty) 3. It removes list items that end in a vowel (upper or lower case) test_list = [ "Peter" , "Bob" , "Ana" , "MARIO" , "CEDRIC" ] remove_vowel_ending(test_list) print(test_list) # ["Peter", "Bob", "CEDRIC"] test_list = [] remove_vowel_ending(test_list) print(test_list) # [] RESPONSE FOR QUESTION 11: 12
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help