Tuples and Dictionaries - tuple assignment, dictionaries operations and methods, iterators and generators, advanced list processing - One Line Questions
1.
What is the difference between `dict.update(other_dict)` and `dict = dict(other_dict)`? —
`update` modifies in place, `dict()` creates a new dictionary
2.
Which two methods define an iterator object? —
__next__() and __iter__()
3.
How can you create a tuple with a single element? —
(element,)
4.
Consider `list(zip([1, 2], ['a', 'b']))`. What is the output? —
[(1, 'a'), (2, 'b')]
5.
What is the result of `tuple(range(3))`? —
(0, 1, 2)
6.
What is the result of `(1, 2) + (3, 4)` in Python? —
(1, 2, 3, 4)
7.
Consider `list(map(lambda x: x * 2, [1, 2, 3]))`. What is the output? —
[2, 4, 6]
8.
Consider `list(filter(lambda x: x > 5, [3, 7, 2, 8, 5]))`. What is the output? —
[7, 8]
9.
Which of the following is a valid list comprehension to create a list of squares from 0 to 4? —
[x**2 for x in range(5)]
10.
Which syntax represents a generator expression? —
(x*x for x in range(5))
11.
Which syntax represents a dictionary comprehension to create a dictionary of squares? —
{x: x**2 for x in range(5)}
12.
What will `my_dict = {'a': 1, 'b': 2}; print(my_dict['a'])` output? —
1
13.
Consider the code: `def count_up_to(n): for i in range(1, n + 1): yield i`. What does `next(count_up_to(3))` return? —
1
14.
What does `reduce(lambda x, y: x + y, [1, 2, 3, 4])` compute? —
10
15.
What is a generator in Python? —
A special type of iterator that yields values one at a time
16.
What does `[x for x in range(10) if x % 2 == 0]` produce? —
A list of even numbers from 0 to 9
17.
What is a dictionary comprehension? —
A way to create dictionaries using a concise syntax
18.
What is a generator expression? —
A way to create generators using a syntax similar to list comprehensions, but with parentheses
19.
What is a list comprehension? —
A way to create lists using a concise syntax
20.
Consider the code: `a, b, c = (1, 2, 3)`. What are the values of `a`, `b`, and `c` after execution? —
a=1, b=2, c=3
21.
Which method is used to add a key-value pair to a dictionary or update an existing one? —
update()
22.
What is an iterator in Python? —
An object that can be iterated upon (looped over)
23.
What does 'tuple assignment' refer to in Python? —
Assigning multiple variables from a tuple in one statement
24.
How are elements accessed in a Python dictionary? —
By key
25.
What does `zip()` function do when used with iterables? —
Creates pairs (tuples) of corresponding elements from multiple iterables
26.
What is the primary advantage of using generator expressions over list comprehensions when dealing with large datasets? —
Generator expressions use less memory by yielding values lazily
27.
What is the main advantage of using generators over regular functions that return lists? —
Generators consume less memory as they produce items on demand
28.
What happens when `next()` is called on an iterator and there are no more items? —
It raises a `StopIteration` exception
29.
What is the result of `del my_dict['key']` if 'key' exists in `my_dict`? —
It removes the key-value pair from the dictionary
30.
Which dictionary method returns a view object that displays a list of all the values in the dictionary? —
values()
31.
Which dictionary method returns a view object displaying a list of a dictionary's key-value tuple pairs? —
items()
32.
Which of the following is NOT a method of Python dictionaries? —
append()
33.
Which of the following is an immutable sequence type in Python? —
Tuple
34.
Which built-in function can be used to create an iterator from an iterable object? —
iter()
35.
What is 'advanced list processing' in Python often associated with? —
List comprehensions and functional programming techniques
36.
What is the primary characteristic of a tuple in Python? —
Immutable and ordered sequence
37.
Which module contains the `reduce()` function in Python 3? —
functools
38.
What is the primary characteristic of a dictionary in Python? —
Mutable collection of key-value pairs
39.
What does the `popitem()` method do in a dictionary? —
Removes and returns an arbitrary (key, value) pair from the dictionary
40.
What is a common use case for tuple unpacking? —
Returning multiple values from a function
41.
What does the `setdefault(key, default)` dictionary method do? —
Returns the value for key if key is in the dictionary, else inserts key with a value of default and returns default
42.
What does the `__iter__()` method return? —
The iterator object itself
43.
Which of the following is true about generators? —
They are created using the `yield` keyword
44.
What is the purpose of the `filter()` function in Python? —
To filter elements from an iterable based on a condition
45.
What is the purpose of the `reduce()` function (from `functools` module) in Python? —
To cumulatively apply a function to the items of an iterable, reducing it to a single value
46.
What is the purpose of the `map()` function in Python? —
To apply a function to each item of an iterable
47.
What is the purpose of the `get(key, default)` method for dictionaries? —
To retrieve the value associated with a key, returning a default value if the key is not found
48.
Which of the following is the correct way to create an empty tuple? —
()
49.
Which of the following is the correct way to create an empty dictionary? —
{}
50.
How is a generator typically created in Python? —
Using the `yield` keyword in a function