cs2370 Practice Exam 2
CS2370 Practice Exam 2 #
This practice exam covers material from the CS2370 course.
Questions #
Code Output/Return Value Questions
-
What will the following Python code print?
a = 10 b = 3 print(f"Result: {a * b - (a % b)}")
-
What will the following Python code print?
message = "Hello World" print(message[6:].lower())
-
What will the following Python code print?
data = [10, 20, 30, 40, 50] print(data[1:4])
-
What is the return value of the following function call?
def process(x, y): if x > y: return x - y else: return y // x print(process(4, 22))
-
What will the following Python code print?
set_a = {10, 20, 30} set_b = {20, 40, 50} print(sorted(list(set_a.union(set_b))))
-
What will the following Python code print?
inventory = {"apples": 5, "bananas": 0, "cherries": 12} print(inventory.get("bananas", -1) + inventory.get("grapes", -1))
-
Consider the following class. What will the code print?
class Vehicle: def __init__(self, kind): self.kind = kind self.speed = 0 def accelerate(self, amount): self.speed += amount def __str__(self): return f"{self.kind} at {self.speed} mph" car = Vehicle("Car") car.accelerate(50) print(car)
-
What will the following recursive function return when called with
sum_list([1, 2, 3, 4])
?def sum_list(nums): if not nums: # Base case: empty list return 0 else: return nums[0] + sum_list(nums[1:]) print(sum_list([1, 2, 3, 4]))
-
What will the following Python code print?
tuple1 = (10, 20) tuple2 = tuple1 + (30,) print(tuple2[1])
-
What will the following Python code print?
def update_dict(data, key, value): data[key] = value my_dict = {'x': 1, 'y': 2} update_dict(my_dict, 'z', 3) print(my_dict)
Conceptual/Design Questions
-
What is the difference between a
for
loop and awhile
loop in Python? Give a scenario where each is more appropriate. -
In the Design Recipe, what is the purpose of writing “Types” (or type hints) for a function?
-
Explain the difference between appending to a list (
.append()
) and extending a list (.extend()
). -
What is operator overloading in Python? Give an example of an operator that can be overloaded.
-
Why might a recursive solution be elegant for certain problems, but potentially inefficient for others? (Hint: think about function calls).
-
What is the difference between a class attribute and an instance attribute in Python?
-
Describe the process of reading data line-by-line from a text file in Python using a
with
statement. Why is usingwith
recommended? -
What is the role of HTML in a web page? What is the role of CSS?
-
What does the
bs4.BeautifulSoup
object represent when used in web scraping? What method is commonly used to find specific HTML elements (e.g., by tag name or CSS class)? -
Explain De Morgan’s Laws in the context of boolean logic. Provide one of the laws as an example.