Appearance
Programming from First Principles - Practice Test 1
This test covers Modules 1-7.
Questions
- What is the difference between a list and a tuple?
- What is a higher-order function?
- Write a list comprehension that produces the squares of 1, 2, and 3.
- What is lazy evaluation?
- What is a lambda function?
- What does it mean for a function to be first-class?
- What is a closure?
- Explain dynamic typing and type hints.
- Trace the result of
list(map(lambda x: x * 2, [1, 2, 3])). - State one advantage and one limitation of generators.
Answer Key
- A list is mutable; a tuple is generally immutable.
- A function that accepts another function or returns a function.
[x * x for x in [1, 2, 3]].- Delaying computation until a result is required.
- A small anonymous function written with
lambda. - It can be assigned, passed, returned, and stored like other values.
- An inner function together with retained access to variables from its enclosing scope.
- Dynamic typing checks types during execution; type hints document intended types and assist tools but do not normally enforce them.
[2, 4, 6].- Advantage: low memory use. Limitation: values are consumed progressively and may not be reusable without recreating the generator.