Skip to main content

cs2370 Demo Exam

··4 mins

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

  1. What will the following Python code print?

    x = 5
    y = 2
    print(x // y + x % y)
    
  2. What will the following Python code print?

    word = "banana"
    count = 0
    for letter in word:
        if letter == 'a':
            count += 1
    print(count)
    
  3. What will the following Python code print?

    nums = [1, 2, 3]
    nums.append(4)
    nums.insert(1, 5)
    print(nums[2])
    
  4. What is the return value of the following function call?

    def calculate(a, b=3):
        return a * b + 1
    
    print(calculate(4))
    
  5. What will the following Python code print?

    set1 = {1, 2, 3, 4}
    set2 = {3, 4, 5, 6}
    print(len(set1.intersection(set2)))
    
  6. What will the following Python code print?

    data = {'a': 1, 'b': 2, 'c': 3}
    print(data.get('d', 0) + data.get('a', 0))
    
  7. 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())
    
  8. 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))
    
  9. What will the following Python code print?

    list1 = [1, 2]
    list2 = list1
    list1.append(3)
    print(list2)
    
  10. 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

  1. Briefly explain the difference between mutable and immutable data types in Python. Give an example of each.

  2. What are the five main steps of the Design Recipe for creating functions, as discussed in class?

  3. When would you choose to use a dictionary over a list in Python? What is the primary advantage of a dictionary for lookups?

  4. What is the purpose of the self parameter in Python class methods?

  5. What is the base case in a recursive function? Why is it essential?

  6. What is the difference between O(n) and O(n^2) complexity? Which one is generally preferred for large inputs?

Unlikely Stuff the AI Suggests

  1. What is the purpose of the requests library in Python, in the context of web programming?

  2. What does it mean to “scrape” a web page? Which Python library is commonly used for parsing HTML during web scraping?

  3. What are the three fundamental boolean operations used in boolean algebra and logic circuits?

Likely Stuff the AI Didn’t Suggest

  1. 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 #

  1. 3 (Integer division 5 // 2 is 2. Modulus 5 % 2 is 1. 2 + 1 = 3)
  2. 3 (The loop counts the occurrences of ‘a’ in “banana”)
  3. 2 (After append(4), nums is [1, 2, 3, 4]. After insert(1, 5), nums is [1, 5, 2, 3, 4]. nums[2] is 2)
  4. 13 (The function uses the default value b=3. 4 * 3 + 1 = 13)
  5. 2 (The intersection of {1, 2, 3, 4} and {3, 4, 5, 6} is {3, 4}. The length is 2)
  6. 1 (data.get('d', 0) returns 0. data.get('a', 0) returns 1. 0 + 1 = 1)
  7. 2 (The increment method is called twice, increasing self.count from 0 to 2)
  8. 6 (factorial(3) calls 3 * factorial(2), which calls 2 * factorial(1), which calls 1 * factorial(0). factorial(0) returns 1. So, 1 * 1 = 1, then 2 * 1 = 2, then 3 * 2 = 6)
  9. [1, 2, 3] (list2 refers to the same list object as list1. Modifying list1 also modifies list2)
  10. ['changed', 20, 30] (Lists are mutable, so the function modifies the original list passed to it)
  11. 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).
    1. Stub with Names and Purpose statement, 2. Types, 3. Tests, 4. Standard pattern, 5. Function body.
  12. 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).
  13. 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.
  14. The base case is the condition under which the recursion stops. It’s essential to prevent infinite recursion.
  15. O(n) (linear time) means the execution time grows proportionally to the input size n. 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.
  16. The requests library is used to send HTTP requests (like GET, POST) to web servers and receive their responses.
  17. Web scraping is the process of automatically extracting data from HTML web pages. BeautifulSoup is commonly used for parsing the HTML structure.
  18. AND, OR, NOT.
  19. Left as an exercise for the reader.