Built-In Data Structures, Functions, and Files

50 questions available

Summary unavailable.

Questions

Question 1

What is the primary characteristic of a Python tuple as a data structure?

View answer and explanation
Question 2

What is the result of the following Python code snippet? tup = tuple(['foo', [1, 2], True]); tup[1].append(3)

View answer and explanation
Question 3

In Python, what does the special syntax `*rest` do when unpacking a tuple or list?

View answer and explanation
Question 4

What is a key difference between the `extend` method and using the `+` operator for combining lists?

View answer and explanation
Question 5

When using slice notation on a sequence like `seq[start:stop]`, which element is not included in the result?

View answer and explanation
Question 6

What is the output of the slice notation `seq[::-1]` on a list `seq`?

View answer and explanation
Question 7

In the context of Python dictionaries, what is the concept of 'hashability' related to?

View answer and explanation
Question 8

What is the purpose of the `setdefault` dictionary method?

View answer and explanation
Question 9

How can you create a set from the list `[2, 2, 2, 1, 3, 3]` in Python?

View answer and explanation
Question 10

Given two sets, `a = {1, 2, 3, 4, 5}` and `b = {3, 4, 5, 6, 7, 8}`, what is the result of `a.intersection(b)`?

View answer and explanation
Question 11

What does the built-in Python function `enumerate` return when used on a collection?

View answer and explanation
Question 12

When using the `zip` function on multiple sequences of different lengths, what determines the number of tuples produced?

View answer and explanation
Question 13

What is the purpose of the `reversed` function in Python?

View answer and explanation
Question 14

What is the result of the list comprehension `[x.upper() for x in strings if len(x) > 2]` where `strings` is `["a", "as", "bat", "car", "dove", "python"]`?

View answer and explanation
Question 15

What is the primary syntactical difference between a list comprehension and a set comprehension?

View answer and explanation
Question 16

Given the nested list `some_tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]`, what is the result of the nested list comprehension `[x for tup in some_tuples for x in tup]`?

View answer and explanation
Question 17

What value is returned by a Python function that reaches its end without an explicit `return` statement?

View answer and explanation
Question 18

When a function returns multiple values, such as `return a, b, c`, what data structure is actually being returned?

View answer and explanation
Question 19

What is the key characteristic of an anonymous or lambda function in Python?

View answer and explanation
Question 20

How does a generator function differ from a normal function in Python?

View answer and explanation
Question 21

What is the syntax for creating a generator expression in Python?

View answer and explanation
Question 22

What is the primary function of the `itertools.groupby` generator?

View answer and explanation
Question 23

In a `try...except...finally` block, when is the code inside the `finally` clause executed?

View answer and explanation
Question 24

What is the primary advantage of using the `with` statement when working with files (e.g., `with open(...) as f:`)?

View answer and explanation
Question 25

What does the file open mode 'x' do in Python?

View answer and explanation
Question 26

What is the difference between opening a file in text mode (e.g., 'r') versus binary mode (e.g., 'rb')?

View answer and explanation
Question 27

If `f1 = open(path)` is opened in text mode and `f2 = open(path, mode='rb')` is opened in binary mode, and both call `read(10)`, why might `f1.tell()` and `f2.tell()` return different values?

View answer and explanation
Question 28

What is a potential issue when using the `seek()` method on a file opened in text mode with a multi-byte encoding like UTF-8?

View answer and explanation
Question 29

Consider the code `b = ["saw", "small", "He", "foxes", "six"]; b.sort(key=len)`. What is the final state of the list `b`?

View answer and explanation
Question 30

What is the primary difference between a list's `sort()` method and Python's built-in `sorted()` function?

View answer and explanation
Question 31

Given `tuples = zip(range(5), reversed(range(5)))`, what will be the result of `dict(tuples)`?

View answer and explanation
Question 32

What is the function of the `collections.defaultdict` class?

View answer and explanation
Question 33

If `c = a.copy()` and `a` and `b` are sets, what is the effect of the in-place operation `c |= b`?

View answer and explanation
Question 34

Why might a programmer choose to use a generator expression `(x ** 2 for x in range(100))` instead of a list comprehension `[x ** 2 for x in range(100)]`?

View answer and explanation
Question 35

In a Python function definition like `def my_func(x, y, z=1.5):`, what is `z=1.5` an example of?

View answer and explanation
Question 36

What does the `global` keyword do inside a Python function?

View answer and explanation
Question 37

Why is it possible to pass functions as arguments to other functions in Python?

View answer and explanation
Question 38

In a `try...except...else` block, when does the `else` block execute?

View answer and explanation
Question 39

To catch multiple specific exception types in a single `except` block, what syntax should be used?

View answer and explanation
Question 40

What does the `list` built-in function do when applied to a generator expression?

View answer and explanation
Question 41

If `seq = [7, 2, 5, 1, 3]`, what is the result of `a = sorted(seq)`?

View answer and explanation
Question 42

What happens when you use the `pop` method on a dictionary, like `d.pop("key")`?

View answer and explanation
Question 43

Which method is used to merge one dictionary into another, modifying the first dictionary in-place?

View answer and explanation
Question 44

What is the result of using the `*` operator on a tuple, as in `('foo', 'bar') * 4`?

View answer and explanation
Question 45

When unpacking a tuple using `a, b, *_ = values`, what is the purpose of the underscore `_` variable?

View answer and explanation
Question 46

What is the computational cost of the list `insert` method compared to `append`?

View answer and explanation
Question 47

When checking if a value is in a list versus a set, which is typically faster for large collections and why?

View answer and explanation
Question 48

Given the list of lists `all_data = [["John", "Emily"], ["Maria", "Natalia"]]`, what is the result of `[name for names in all_data for name in names if name.count("a") >= 2]`?

View answer and explanation
Question 49

How can you unpack the tuple `values = (1, 2, 3, 4, 5)` so that variable `a` is 1, `b` is 2, and the rest of the values are discarded?

View answer and explanation
Question 50

What would the function `isiterable` defined in the text as `def isiterable(obj): try: iter(obj); return True; except TypeError: return False` return for the input `5`?

View answer and explanation