WorksheetsFinal Exam Sample Q & A – Extracted Questions
Total questions: 16
Worksheet time: 18mins
For each statement in Column 1, answer T (true) or F (false). Python’s built-in functional programming tools do not modify the input data.
True
False
For each statement in Column 1, answer T (true) or F (false). The Python re.search() function will find all non-overlapping matches of a pattern within a string.
True
False
For each statement in Column 1, answer T (true) or F (false). A Python list comprehension is faster than an equivalent Python for loop.
True
False
For each statement in Column 1, answer T (true) or F (false). In production applications, Python logging() is generally preferred over Python print().
True
False
Multiple-choice questions. There is only 1 correct choice. What keyword is used to create a generator function?
return
yield
next
lambda
None of the above
Multiple-choice questions. There is only 1 correct choice. Which regex function returns a match only if the pattern occurs at the beginning of a string?
re.search()
re.findall()
re.fullmatch()
re.match()
re.sub()
Multiple-choice questions. There is only 1 correct choice. Which of the following is a valid Python list comprehension?
lc = [x * 2 for x in range(5) if x = 3]
lc = (x * 2 for x in range(5))
lc = [x * 2 for x in range(5)]
lc = {x * 2 for x in range(5)}
lc = list(x * 2 for x in range(5))
What is one key advantage of using logging instead of print() for debugging?
Logging messages can be filtered by severity level
Logging automatically fixes syntax errors
Logging runs twice as fast as print statements
Logging only works in production environments
Logging requires no configuration
Part 3: Short answer questions. Please give brief answers (At most, 1-2 sentences per question). What is a Python iterator?
Part 3: Short answer questions. Please give brief answers (At most, 1-2 sentences per question). What does the Python filter() function do, and what does it return?
Part 3: Short answer questions. Please give brief answers (At most, 1-2 sentences per question). What does the Python assert statement do?
Part 3: Short answer questions. Please give brief answers (At most, 1-2 sentences per question). In Python object-oriented programming (OOP), what is a class?
Part 4: Code-tracing questions. For each question, trace the code and write the exact output. If the code does not produce any output, write "No output". If the code produces an error (exception), write "Exception". What is the output of this code?
data = [1,2,3,4]
result = list(map(lambda x: x * 10, data))
print(result[1])
(a)
Part 4: Code-tracing questions. For each question, trace the code and write the exact output. If the code does not produce any output, write "No output". If the code produces an error (exception), write "Exception". What is the output of this code?
import numpy as np
nums = np.arange(100,250,50)
it = iter(nums)
next(it)
next(it)
next(it)
print(next(it))
(a)
Part 4: Code-tracing questions. For each question, trace the code and write the exact output. If the code does not produce any output, write "No output". If the code produces an error (exception), write "Exception". What is the output of this code?
cities = ['New York', 'Chicago', 'Philadelphia', 'Boston']
sorted(cities, key=len, reverse=True)
print(cities)
(a)
Part 4: Code-tracing questions. For each question, trace the code and write the exact output. If the code does not produce any output, write "No output". If the code produces an error (exception), write "Exception". What is the result of the following code?
fruits_1 = ['apple', 'banana', 'pear', 'kiwi']
fruits_2 = {w: len(w) for w in fruits_1 if len(w) <= 4}
print(fruits_2)
(a)
