Python Data Structures: Lists, Tuples, Sets u0026 Dictionaries Deep Dive Quiz

Challenge your knowledge of advanced Python concepts focusing on the intricacies of lists, tuples, sets, and dictionaries, including their methods, mutability, and performance.

  1. List Mutation Operations

    Which of the following operations will raise a TypeError when attempted on a tuple containing integers: t = (1, 2, 3)?

    1. t.index(2)
    2. t.count(1)
    3. len(t)
    4. t + (4,)
    5. t[1] = 5
  2. Set Membership Complexity

    What is the average-case time complexity for checking membership using the 'in' operator in a Python set with n elements?

    1. O(log n)
    2. O(1)
    3. O(n log n)
    4. O(n)
    5. O(n^2)
  3. 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'}?

    1. The list is automatically converted to a tuple.
    2. A SyntaxError is raised at compile time.
    3. The code executes and sets the list as the key.
    4. A KeyError is raised for invalid operation.
    5. A TypeError is raised because lists are unhashable.
  4. 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?

    1. copy.deepcopy(a)
    2. list(a)
    3. a.clone()
    4. a.copy()
    5. a[:]
  5. Tuple Packing and Unpacking

    What is the result of executing a, *b, c = (1, 2, 3, 4, 5)?

    1. a = 1, b = (2, 3, 4, 5), c = None
    2. a = (1,), b = (2, 3, 4), c = (5,)
    3. a = 1, b = [2, 3, 4], c = 5
    4. a = [1, 2, 3], b = 4, c = 5
    5. a = 1, b = (2, 3), c = 4
  6. Set Methods and Return Types

    What is returned by the set difference_update() method when called as s1.difference_update(s2)?

    1. A tuple of unique values
    2. The set of differences as a new set
    3. None
    4. A list of removed elements
    5. A boolean indicating success
  7. Storing Lists in Sets

    Why can’t you insert a list into a Python set using s = set(); s.add([1,2])?

    1. Set size cannot be extended.
    2. Lists are too large for sets.
    3. Lists are mutable and unhashable.
    4. Lists are automatically sorted first.
    5. Lists convert to tuples automatically.
  8. Dictionary View Objects

    Given d = {'x': 1, 'y': 2}, what is the type returned by d.items()?

    1. tuple
    2. dict_items
    3. dict_pairs
    4. set
    5. list of tuples
  9. 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)?

    1. x is shadowed and lost.
    2. x is set to None.
    3. The original value of x is preserved.
    4. x is deleted from scope.
    5. x is overwritten to 4.
  10. Maximal Performance of Containers

    Which data structure provides the fastest average-case lookup performance for large numbers of unique items?

    1. Array
    2. List
    3. Set
    4. Deque
    5. Tuple
  11. In-place Modification of Dictionaries

    Which method on dict objects can update the dictionary with keys and values from another mapping in place?

    1. concat()
    2. merge()
    3. update()
    4. extend()
    5. append()
  12. Immutability and Hashing

    Out of the following, which is both immutable and hashable by default in Python?

    1. List of integers
    2. Set
    3. Dictionary
    4. List of floats
    5. Tuple of strings
  13. Removing Items from Sets

    What is the difference between set.remove(x) and set.discard(x) when x is missing from the set?

    1. Both methods remove x silently if missing.
    2. discard(x) raises an error, remove(x) does not.
    3. Both methods add x if missing.
    4. remove(x) raises an error, discard(x) does not.
    5. remove(x) converts x to None.
  14. Slicing and Mutability

    Given l = [10, 20, 30, 40, 50], which operation modifies the elements 20 and 30 in-place?

    1. l(1,2) = 200, 300
    2. l[2:4] = 100, 200
    3. l[1:3] = [200, 300]
    4. l[2,3] = [100, 200]
    5. l[1,2] = [200, 300]
  15. Dictionary Key Existence

    Which expression most efficiently checks if the key 'name' exists in a dictionary d?

    1. 'name' == d.key()
    2. 'name' in d
    3. d.exists('name')
    4. d.hasKey('name')
    5. 'name' in d.values()
  16. Advanced Set Operations

    Given sets a = {1, 2, 3} and b = {3, 4, 5}, which operation produces {1, 2, 4, 5}?

    1. a.intersection(b)
    2. a.difference(b)
    3. a.union(b)
    4. a + b
    5. a.symmetric_difference(b)