COMP1005/1405 Final Exam Practice Problems PDF
Document Details
Uploaded by Deleted User
COMP1005/1405
Dr. Andrew Runka
Tags
Summary
This document is a practice exam paper for COMP1005/1405, focusing on various programming concepts such as data types, loops, functions, and lists in Python.
Full Transcript
COMP1005/1405: Final Exam Practice Problems (Credit for the majority of the practice problems goes to Dr. Andrew Runka.) Do not post the Final Exam Practice and/or solutions on any website. 1. Data Types What will each line of the following code print? print(1+1)...
COMP1005/1405: Final Exam Practice Problems (Credit for the majority of the practice problems goes to Dr. Andrew Runka.) Do not post the Final Exam Practice and/or solutions on any website. 1. Data Types What will each line of the following code print? print(1+1) print(str(1)+str(1)) print(str(1)+1) print("Hello"*2) print("Hello"+3) 2. Variables What does the following code print? foo = 20 bar = foo*2 print(bar) Is this code equivalent to the code above? foo = 20 print(foo*2) Is there anything wrong with this code? foo = 20 print(int(foo)*2) 3. User Input Is there anything wrong with the code below? Will it run? Will the answer be logically correct? # double the user's input myInput = input("Please input a number here: ") print(myInput*2) 4. Branching Control Structures What is the output of the following code? a = 25 b = False if (a < 25) or (not b): b = not b elif (a >= 25) and b: a /= 5 else: a += 7 b = False print(f"{a}: {b}") 5. Looping Control Structures - Fizz Buzz Write code that prints the first 100 numbers on individual lines. However: for multiples of 3, print 'fizz' instead; for multiples of 5, print 'buzz' instead; for multiples of 3 and 5, print 'fizzbuzz' instead. For example, the first part of the output should be similar to the output shown on the right. 6. Functions Are the following two functions equivalent? def foo(x): myVar = 2*x return myVar def bar(x): return 2*x What is the difference between the two functions below? def baz(x): return 2*x def qux(x): print(2*x) Using the same function definitions as above, identify the problem in the following code. Will it run? Will the results be what you expect? Why or why not? a = baz(3) b = qux(3) print(a) print(b) Identify the problem in the following piece of code. def myFunc(x): x = int(input("Input any number.")) if x > 10: return True return False Exponent Write a function that takes a numeric value x (int), as well as an integer n, and uses a n looping structure to return the exponent x. 7. Data Structures - Lists SumList: Write a function that takes a list as an argument to find the sum of all elements in the list using a looping structure. UniqueList: Write a function that takes a list as an argument and returns a new list that removes all duplicates in the list. ListMethods: Trace through the code manually to predict what will happen when the code is executed. Then, convert the code snippet into Python code to verify your answers. 2-D Lists Write a function that returns a N-by-N 2-D list of randomly generated numbers between 0 and 100. Take n as an argument to your function. Then, write a function to print this 2-D list, line by line, as a string. The function must be counter-controlled. The functions must take two arguments: grid (your 2-D list) and n (the size of your list). For example, with n=2 : 27 4 46 9 8. Data Structures - Dictionaries Consider the following code: myDict = {} for i in range(1,5): myDict[i] = i*2 for key in myDict: print(f"{key}: {myDict[key]}") Bob says: I believe this code will print out "1:2, 2:4, 3:6, 4:8, 5:10", in that order, all on new lines. However, Alice disagrees. Who is correct? If Alice is correct, what are the reasons Bob is wrong? You are given the following code: prices = { "banana": 4, "apple": 2, "orange": 1.5, "pear": 3, "bread": 3, "bacon": 4, "chips": 2 } Assume that the userCart variable is a list of strings representing the items in the user's cart. For example, ["banana", "apple", "bacon", "apple"] is a valid shopping cart. Write a function, receipt(userCart) , to calculate the total cost of a user's cart and print a nicely formatted receipt. 9. Recursion You may not use looping structures for any questions in this section. We will calculate the sum of a list of numbers recursively. However, we will trace the steps to solve this by hand. You may also implement the code if you wish. Complete the following trace: sum([5,6,2,8,9]) 5 + (sum([6,2,8,9])) 5 + (6 + (sum([2,8,9]))) Now, we will do the same for the product of a list of numbers. prod([10,9,6,4,2]) 10 * # Complete this line as well Factorial We define n!=n*(n-1)*(n-2)*...*2*1. 1. What is the base case? 2. What is the recursive case? 3. Write the code to define the recursive function fact(n). Comprehension What will happen when this function is run? def myFunc(x): return x + myFunc(x-1) What will happen when this function is run? def myFunc(x): if x == 0 return x print(myFunc(x-1)) 10. Objects & Classes Classes Implement an Animal class with the following specifications: Having the below attributes: o species o size o sound (e.g., "bark!") o isHungry (True or False) Having the below methods: o __init__(self, species, size, sound) : A constructor to initialize these attributes for the animal. You may set isHungry to True or False as you please. o getSize(self) : returns the size of the animal o makeNoise(self) : prints the animal's sound o feed(self) : if animal is hungry, change to false; if animal is not hungry, print something like: "Giraffe already ate!” Objects What outputs will the following blocks of code produce? a = Animal("Dog", "Medium", "Bark!") b = Animal("Dog", "Medium", "Bark!") c = Animal("Dog", "Small", "Bark!") print(a == b) print(b == c) 11. File I/O What do the following file modes do? file = open(“myFile.txt”, ‘r’) file = open(“myFile.txt”, ‘w’) file = open(“myFile.txt”, a) 12. Debugging: Try-Except Mechanism Common Error Types and techniques on how to find the error in your code. White box test: Minimal test cases for Statement/Branching/Condition Coverage 13. Searching What are the two types of search algorithms mentioned in the lecture? Which search algorithm would you use on a sorted list of elements? Would you use binary search on an unsorted list of elements? 14. Sorting: Which sorting algorithm is quicker on a large list of elements – selection, bubble or merge-sort? Why? Final Exam Multiple Choice Practice: 1. What will be the output of the following code? a) X b) Y c) Both X and Y d) Z e) Both Y and Z 2. Which of the following pieces of code produces the pattern below as output? 1 22 333 4444 55555 a) b) c) d) e) None of the above are correct. 3. Which of the following data structures in Python are mutable? a) List and String b) Tuple and List c) Dictionary and String d) List and Dictionary e) All of the above Below is a recursive function to calculate the peaches; a monkey picks a certain number of peaches on the first day, eats half of them, and one more. Fill in the missing blanks so that the program works properly: 4. Blank 1: a) return 0 b) return 1 c) return 2 d) print(1) e) print(0) 5. Blank 2: a) nDays-1 b) nDays c) original_peaches(nDays) d) original_peaches(nDays-1) e) nDays + 1