cs2370 Demo Exam
CS2370 Demo Exam #
This demo exam covers material from the CS2370 course.
This is what the exam should be according to Google’s Gemini.
Questions #
Code Output/Return Value Questions
-
What will the following Python code print?
x = 5 y = 2 print(x // y + x % y)
-
What will the following Python code print?
word = "banana" count = 0 for letter in word: if letter == 'a': count += 1 print(count)
-
What will the following Python code print?
nums = [1, 2, 3] nums.append(4) nums.insert(1, 5) print(nums[2])
-
What is the return value of the following function call?
def calculate(a, b=3): return a * b + 1 print(calculate(4))
-
What will the following Python code print?
set1 = {1, 2, 3, 4} set2 = {3, 4, 5, 6} print(len(set1.intersection(set2)))
-
What will the following Python code print?
data = {'a': 1, 'b': 2, 'c': 3} print(data.get('d', 0) + data.get('a', 0))
-
Consider the following class. What will the code print?
class Counter: def __init__(self): self.count = 0 def increment(self): self.count += 1 def get_value(self): return self.count c = Counter() c.increment() c.increment() print(c.get_value())
-
What will the following recursive function return when called with
factorial(3)
?def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) print(factorial(3))
-
What will the following Python code print?
list1 = [1, 2] list2 = list1 list1.append(3) print(list2)
-
What will the following Python code print?
def modify_list(items): items[0] = 'changed' my_list = [10, 20, 30] modify_list(my_list) print(my_list)
Conceptual/Design Questions
-
Briefly explain the difference between mutable and immutable data types in Python. Give an example of each.
-
What are the five main steps of the Design Recipe for creating functions, as discussed in class?
-
When would you choose to use a dictionary over a list in Python? What is the primary advantage of a dictionary for lookups?
-
What is the purpose of the
self
parameter in Python class methods? -
What is the base case in a recursive function? Why is it essential?
-
What is the difference between
O(n)
andO(n^2)
complexity? Which one is generally preferred for large inputs?
Unlikely Stuff the AI Suggests
-
What is the purpose of the
requests
library in Python, in the context of web programming? -
What does it mean to “scrape” a web page? Which Python library is commonly used for parsing HTML during web scraping?
-
What are the three fundamental boolean operations used in boolean algebra and logic circuits?
Likely Stuff the AI Didn’t Suggest
- Write a function in Python that takes a list of integers and returns that sequence of integers with all odd values doubled. Show all 5 steps of the design recipe.
Answer Key #
3
(Integer division5 // 2
is 2. Modulus5 % 2
is 1.2 + 1 = 3
)3
(The loop counts the occurrences of ‘a’ in “banana”)2
(Afterappend(4)
,nums
is[1, 2, 3, 4]
. Afterinsert(1, 5)
,nums
is[1, 5, 2, 3, 4]
.nums[2]
is2
)13
(The function uses the default valueb=3
.4 * 3 + 1 = 13
)2
(The intersection of{1, 2, 3, 4}
and{3, 4, 5, 6}
is{3, 4}
. The length is 2)1
(data.get('d', 0)
returns0
.data.get('a', 0)
returns1
.0 + 1 = 1
)2
(Theincrement
method is called twice, increasingself.count
from 0 to 2)6
(factorial(3)
calls3 * factorial(2)
, which calls2 * factorial(1)
, which calls1 * factorial(0)
.factorial(0)
returns1
. So,1 * 1 = 1
, then2 * 1 = 2
, then3 * 2 = 6
)[1, 2, 3]
(list2
refers to the same list object aslist1
. Modifyinglist1
also modifieslist2
)['changed', 20, 30]
(Lists are mutable, so the function modifies the original list passed to it)- Mutable objects can be changed after creation (e.g., lists, dictionaries, sets). Immutable objects cannot be changed after creation (e.g., integers, floats, strings, tuples).
-
- Stub with Names and Purpose statement, 2. Types, 3. Tests, 4. Standard pattern, 5. Function body.
- Use a dictionary when you need efficient lookups based on a unique key. The advantage is typically O(1) (constant time) lookup, whereas list lookup is O(n) (linear time).
self
refers to the instance of the class on which the method is being called. It allows the method to access and modify the instance’s attributes.- The base case is the condition under which the recursion stops. It’s essential to prevent infinite recursion.
O(n)
(linear time) means the execution time grows proportionally to the input sizen
.O(n^2)
(quadratic time) means the execution time grows proportionally to the square of the input size.O(n)
is generally preferred for large inputs as it scales much better.- The
requests
library is used to send HTTP requests (like GET, POST) to web servers and receive their responses. - Web scraping is the process of automatically extracting data from HTML web pages. BeautifulSoup is commonly used for parsing the HTML structure.
- AND, OR, NOT.
- Left as an exercise for the reader.