Challenge your knowledge of advanced Python concepts focusing on the intricacies of lists, tuples, sets, and dictionaries, including their methods, mutability, and performance.
List Mutation Operations
Which of the following operations will raise a TypeError when attempted on a tuple containing integers: t = (1, 2, 3)?
- t.index(2)
- t.count(1)
- len(t)
- t + (4,)
- t[1] = 5
Set Membership Complexity
What is the average-case time complexity for checking membership using the 'in' operator in a Python set with n elements?
- O(log n)
- O(1)
- O(n log n)
- O(n)
- O(n^2)
Mutable Keys in Dictionaries
What will happen if you attempt to use a list as a key in a Python dictionary, such as d = {[1, 2]: 'value'}?
- The list is automatically converted to a tuple.
- A SyntaxError is raised at compile time.
- The code executes and sets the list as the key.
- A KeyError is raised for invalid operation.
- A TypeError is raised because lists are unhashable.
Copying Nested Structures
Given a nested list a = [[1, 2], [3, 4]], which method creates a truly independent copy of all nested lists within a?
- copy.deepcopy(a)
- list(a)
- a.clone()
- a.copy()
- a[:]
Tuple Packing and Unpacking
What is the result of executing a, *b, c = (1, 2, 3, 4, 5)?
- a = 1, b = (2, 3, 4, 5), c = None
- a = (1,), b = (2, 3, 4), c = (5,)
- a = 1, b = [2, 3, 4], c = 5
- a = [1, 2, 3], b = 4, c = 5
- a = 1, b = (2, 3), c = 4
Set Methods and Return Types
What is returned by the set difference_update() method when called as s1.difference_update(s2)?
- A tuple of unique values
- The set of differences as a new set
- None
- A list of removed elements
- A boolean indicating success
Storing Lists in Sets
Why can’t you insert a list into a Python set using s = set(); s.add([1,2])?
- Set size cannot be extended.
- Lists are too large for sets.
- Lists are mutable and unhashable.
- Lists are automatically sorted first.
- Lists convert to tuples automatically.
Dictionary View Objects
Given d = {'x': 1, 'y': 2}, what is the type returned by d.items()?
- tuple
- dict_items
- dict_pairs
- set
- list of tuples
List Comprehension Scope
If a variable x exists before a list comprehension '[x for x in range(5)]', what value does x have after executing the comprehension (in Python 3)?
- x is shadowed and lost.
- x is set to None.
- The original value of x is preserved.
- x is deleted from scope.
- x is overwritten to 4.
Maximal Performance of Containers
Which data structure provides the fastest average-case lookup performance for large numbers of unique items?
- Array
- List
- Set
- Deque
- Tuple
In-place Modification of Dictionaries
Which method on dict objects can update the dictionary with keys and values from another mapping in place?
- concat()
- merge()
- update()
- extend()
- append()
Immutability and Hashing
Out of the following, which is both immutable and hashable by default in Python?
- List of integers
- Set
- Dictionary
- List of floats
- Tuple of strings
Removing Items from Sets
What is the difference between set.remove(x) and set.discard(x) when x is missing from the set?
- Both methods remove x silently if missing.
- discard(x) raises an error, remove(x) does not.
- Both methods add x if missing.
- remove(x) raises an error, discard(x) does not.
- remove(x) converts x to None.
Slicing and Mutability
Given l = [10, 20, 30, 40, 50], which operation modifies the elements 20 and 30 in-place?
- l(1,2) = 200, 300
- l[2:4] = 100, 200
- l[1:3] = [200, 300]
- l[2,3] = [100, 200]
- l[1,2] = [200, 300]
Dictionary Key Existence
Which expression most efficiently checks if the key 'name' exists in a dictionary d?
- 'name' == d.key()
- 'name' in d
- d.exists('name')
- d.hasKey('name')
- 'name' in d.values()
Advanced Set Operations
Given sets a = {1, 2, 3} and b = {3, 4, 5}, which operation produces {1, 2, 4, 5}?
- a.intersection(b)
- a.difference(b)
- a.union(b)
- a + b
- a.symmetric_difference(b)