StudyFox
कीमतेंब्लॉगएक्सटेंशनमुफ़्त टूल
  • कीमतें
  • ब्लॉग
  • एक्सटेंशन
  • मुफ़्त टूल
  • शुरू करेंसाइन इन
StudyFox

अपने नोट्स, PDF और स्लाइड्स को फ़्लैशकार्ड, क्विज़, नोट्स और माइंड मैप में बदलें।

StudyFox खोलें

स्टडी टूल्स

  • होमवर्क हेल्पर
  • नोट मेकर
  • फ़्लैशकार्ड मेकर
  • क्विज़ मेकर
  • माइंड मैप मेकर

प्रोडक्ट

  • कीमतें
  • Chrome एक्सटेंशन
  • मुफ़्त एक्सटेंशन
  • स्टडी सेट देखें
  • ब्लॉग

सहायता

  • सहायता केंद्र
  • संपर्क करें
  • गोपनीयता नीति
  • सेवा की शर्तें

तुलना करें

  • Chegg का विकल्प
  • Course Hero का विकल्प
  • Coursology का विकल्प
  • Photomath का विकल्प
  • Gauthmath का विकल्प
  • PhotoSolve का विकल्प
  • CollegeTools का विकल्प
  • Transcript का विकल्प
  • SmartSolve का विकल्प
  • Homeworkify का विकल्प
  • AnswersAi का विकल्प
  • StudyX का विकल्प
  • Quizlet का विकल्प
  • Anki का विकल्प
  • Knowt का विकल्प
  • Brainscape का विकल्प
  • सभी तुलनाएँ

© 2026 StudyFox AI. सर्वाधिकार सुरक्षित।

  • X
  • Discord
  • Instagram
ExploreEngineering & TechData Science & AIPython Data Structures and Comprehensions: Predict the Output

Quiz · Medium

Python Data Structures and Comprehensions: Predict the Output

Slicing, append vs extend, sort vs sorted, Counter, sets, dict merging, unpacking, generators, shallow vs deep copy, hashing and iteration pitfalls.

by Kill h.16 questionsCreated Nov 20, 2026, 08:27 PM
Data Science & AI· PythonTags#python#data-structures#comprehensions

16 questions

  1. 1

    What does this code print?

    a = [1, 2, 3]
    a.append([4, 5])
    b = [1, 2, 3]
    b.extend([4, 5])
    print(a, b)
    • A

      [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]

    • [1, 2, 3, [4, 5]] [1, 2, 3, 4, 5]

    • C

      [1, 2, 3, [4, 5]] [1, 2, 3, [4, 5]]

    • D

      [1, 2, 3, 4, 5] [1, 2, 3, [4, 5]]

    append adds the list as a single nested element; extend adds each item of the iterable.

  2. 2

    What does this code print?

    x = [3, 1, 2]
    y = x.sort()
    print(y, x)
    • A

      [1, 2, 3] [1, 2, 3]

    • B

      [1, 2, 3] [3, 1, 2]

    • C

      None [3, 1, 2]

    • None [1, 2, 3]

    list.sort() sorts in place and returns None, so y is None while x is now sorted. Use sorted(x) to get a new list.

  3. 3

    What does this code print?

    s = ["a", "b", "c", "d", "e", "f"]
    print(s[-2:], s[:3], s[1:4])
    • ['e', 'f'] ['a', 'b', 'c'] ['b', 'c', 'd']

    • B

      ['e', 'f'] ['a', 'b', 'c'] ['b', 'c', 'd', 'e']

    • C

      ['d', 'e', 'f'] ['a', 'b', 'c'] ['b', 'c', 'd']

    • D

      ['e', 'f'] ['a', 'b', 'c', 'd'] ['b', 'c', 'd']

    Start is included and stop is excluded. s[-2:] is the last two items, s[:3] the first three, s[1:4] items at indices 1, 2, 3.

  4. 4

    What does this code print?

    from collections import Counter
    print(Counter("banana").most_common(2))
    • A

      [('b', 1), ('n', 2)]

    • B

      [('a', 3), ('b', 1)]

    • [('a', 3), ('n', 2)]

    • D

      [('n', 2), ('a', 3)]

    Counter counts a=3, n=2, b=1; most_common(2) returns the two highest counts in descending order.

  5. 5

    What does this code print?

    print({1, 2, 3} | {3, 4}, {1, 2, 3} & {3, 4}, {1, 2, 3} - {3, 4}, {1, 2, 3} ^ {3, 4})
    • A

      {1, 2, 3, 4} {3} {1, 2} {3, 4}

    • {1, 2, 3, 4} {3} {1, 2} {1, 2, 4}

    • C

      {1, 2, 3, 3, 4} {3} {1, 2} {1, 2, 4}

    • D

      {1, 2, 3, 4} {3} {4} {1, 2, 4}

    Union, intersection, difference (in the first set only) and symmetric difference (in exactly one set).

  6. 6

    What does this code print?

    d1 = {"a": 1}
    d2 = {"b": 2, "a": 3}
    print(d1 | d2)
    • {'a': 3, 'b': 2}

    • B

      {'a': 1, 'b': 2}

    • C

      {'a': 4, 'b': 2}

    • D

      {'b': 2, 'a': 1}

    In a dict merge, values from the right operand win for duplicate keys. The result keeps the first dict's key order: 'a' stays first.

  7. 7

    What does this code print?

    a, *rest = [1, 2, 3, 4]
    print(a, rest)
    • A

      1 (2, 3, 4)

    • B

      [1] [2, 3, 4]

    • C

      1 2 3 4

    • 1 [2, 3, 4]

    Star-unpacking collects the remaining items into a list.

  8. 8

    What does this code print?

    g = (x * x for x in range(3))
    print(list(g), list(g))
    • A

      [0, 1, 4] [0, 1, 4]

    • B

      [0, 1, 4] [0, 1, 4, 9]

    • [0, 1, 4] []

    • D

      [0, 1, 2] []

    A generator is exhausted after the first list(g), so the second call returns an empty list.

  9. 9

    What does this code print?

    a = [1, 2, 3]
    for x in a:
        if x == 2:
            a.remove(x)
    print(a)
    • A

      [3]

    • [1, 3]

    • C

      [1, 2, 3]

    • D

      [1]

    Removing during iteration shifts the indices. After removing 2 the loop moves on and skips what is now at that position. Here the result is [1, 3]; the lesson is never mutate a list you iterate over.

  10. 10

    What does this code print?

    x = 5
    print(1 < x < 10, 1 < x < 3)
    • A

      True True

    • B

      False False

    • C

      False True

    • True False

    Chained comparisons are evaluated as 1 < x and x < 10 and 1 < x and x < 3.

  11. 11

    What does this code print?

    print(list(zip([1, 2, 3], "ab")))
    • [(1, 'a'), (2, 'b')]

    • B

      [(1, 'a'), (2, 'b'), (3, None)]

    • C

      [(1, 'a'), (2, 'b'), (3, 'a')]

    • D

      [(1, 'a'), (2, 'b')] followed by a ValueError

    zip stops at the shortest input, producing only two pairs. zip_longest would pad with None.

  12. 12

    What does this code print?

    from collections import defaultdict
    dd = defaultdict(list)
    for word in ["apple", "avocado", "banana"]:
        dd[word[0]].append(word)
    print(dict(dd))
    • A

      {'a': 'avocado', 'b': 'banana'}

    • B

      {'a': ['apple'], 'b': ['avocado', 'banana']}

    • {'a': ['apple', 'avocado'], 'b': ['banana']}

    • D

      KeyError

    defaultdict creates the missing empty list on first access, so words are grouped by first letter.

  13. 13

    What does this code print?

    import copy
    a = [[1], [2]]
    b = copy.deepcopy(a)
    b[0].append(9)
    print(a, b)
    • A

      [[1, 9], [2]] [[1, 9], [2]]

    • [[1], [2]] [[1, 9], [2]]

    • C

      [[1], [2]] [[1], [2]]

    • D

      [[1, 9], [2]] [[1], [2]]

    deepcopy copies nested lists too, so changing b leaves a untouched. With a shallow copy both would show 9.

  14. 14

    What does this code print?

    print(sum([True, False, True]), any([]), all([]))
    • 2 False True

    • B

      3 True False

    • C

      2 True True

    • D

      3 False True

    True counts as 1, so the sum is 2. any([]) is False (no element is true) and all([]) is True (no counterexample).

  15. 15

    What happens with d = {[1, 2]: "x"}?

    • A

      It works: the key is the list [1, 2]

    • B

      KeyError: [1, 2]

    • C

      The list is converted to a tuple automatically

    • TypeError: unhashable type: 'list'

    Dict keys must be hashable. Lists are mutable and therefore unhashable. A tuple such as (1, 2) works.

  16. 16

    Two different keys are mapped by the hash function to the same slot of the table in the figure. What is this called, and what does it do to lookup time?

    • A

      A cache miss; lookups become O(log n) always

    • B

      A collision; Python raises an error

    • A collision; lookups stay close to O(1) on average if collisions are rare, but get slower as they pile up

    • D

      A rehash; it is always O(n)

    Hash tables resolve collisions (here by using the next free slot). With a good hash function and resizing, average lookups stay near constant time. Image: Jorge Stolfi, CC BY-SA 3.0, via Wikimedia Commons.