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 & TechComputer ScienceProgramming Quiz: Python Output Prediction and Pitfalls (Hard)

Quiz · Hard

Programming Quiz: Python Output Prediction and Pitfalls (Hard)

Predict the output of tricky Python snippets: closures, mutable defaults, aliasing, scope, generators and slicing.

by Kill h.17 questionsCreated Nov 20, 2026, 08:26 PM
Computer Science· ProgrammingTags#programming#python#code-reading#quiz

17 questions

  1. 1

    What does this print?

    fs = [lambda: i for i in range(3)]
    print([f() for f in fs])
    • A

      [0, 1, 2]

    • B

      [3, 3, 3]

    • [2, 2, 2]

    • D

      Error

    The lambdas capture the variable i, not its value at creation. After the loop i is 2, so all return 2. Use lambda i=i: i to capture values.

  2. 2

    What does this print?

    def add(x, bucket=[]):
        bucket.append(x)
        return bucket
    print(add(1))
    print(add(2))
    • [1] then [1, 2]

    • B

      [1] then [2]

    • C

      [1, 2] then [1, 2]

    • D

      Error

    The default list is created once when the function is defined and is shared between calls.

  3. 3

    What does this print?

    grid = [[0] * 2] * 2
    grid[0][0] = 1
    print(grid)
    • A

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

    • B

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

    • C

      [[0, 0], [0, 0]]

    • [[1, 0], [1, 0]]

    The outer multiplication repeats a reference to the same inner list, so both rows change.

  4. 4

    What does this print?

    def f():
        try:
            return 'a'
        finally:
            print('b')
    print(f())
    • A

      a

    • b then a

    • C

      a then b

    • D

      b

    The finally block prints 'b' while the function is returning; then print shows 'a'.

  5. 5

    What is sum(x for x in range(5) if x % 2)?

    • A

      10

    • B

      6

    • 4

    • D

      0

    The odd numbers below 5 are 1 and 3: 1 + 3 = 4. The truthy test x % 2 is 1 for odd numbers.

  6. 6

    What does this print?

    x = 10
    def f():
        print(x)
        x = 5
    f()
    • UnboundLocalError

    • B

      10

    • C

      5

    • D

      None

    Because x is assigned inside f, Python treats it as local for the whole function; reading it before assignment raises UnboundLocalError.

  7. 7

    What is 'abcdef'[1:5:2]?

    • A

      'bcde'

    • B

      'ace'

    • C

      'bdf'

    • 'bd'

    Start at index 1, stop before 5, step 2: indices 1 and 3, so 'b' and 'd'.

  8. 8

    What is the output?

    print(len({1, 2, 2, 3}), len([1, 2, 2, 3]))
    • A

      4 4

    • 3 4

    • C

      3 3

    • D

      4 3

    The set removes the duplicate 2 (3 elements); the list keeps it (4 elements).

  9. 9

    What does list(zip('ab', [1, 2, 3])) return?

    • A

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

    • B

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

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

    • D

      Error: lengths differ

    zip stops at the shortest iterable.

  10. 10

    What is (lambda x, y=2: x ** y)(3)?

    • 9

    • B

      6

    • C

      8

    • D

      Error

    y defaults to 2, so 3 ** 2 = 9.

  11. 11

    Which expression correctly tests that n is a positive power of two?

    • A

      n % 2 == 0

    • B

      n & 1 == 0

    • C

      n >> 1 == n

    • n > 0 and n & (n - 1) == 0

    A power of two has a single 1 bit, so clearing the lowest set bit with n & (n − 1) gives 0. n % 2 == 0 is true for 6 too.

  12. 12

    What does '3' + '4' give in Python, and what does int('3') + int('4') give?

    • A

      7 and 7

    • '34' and 7

    • C

      '7' and 7

    • D

      Error and 7

    + concatenates strings and adds integers.

  13. 13

    Which statement about is and == is correct?

    • A

      is compares values

    • B

      == compares identity

    • is checks whether two names refer to the same object

    • D

      They are always interchangeable

    == compares values (it calls __eq__). is compares object identity; use it for None.

  14. 14

    What does this print?

    def gen():
        yield 1
        yield 2
    g = gen()
    print(next(g), next(g))
    • 1 2

    • B

      1 1

    • C

      [1, 2]

    • D

      StopIteration

    Each next() resumes the generator to its next yield.

  15. 15

    What is the result of divmod(-7, 2)?

    • A

      (-3, -1)

    • B

      (-3, 1)

    • C

      (-4, -1)

    • (-4, 1)

    Floor division gives −4 and the remainder has the sign of the divisor: −7 = (−4) × 2 + 1.

  16. 16

    A function takes def f(*args, **kwargs) and is called f(1, 2, a=3). What are args and kwargs?

    • A

      [1, 2] and ['a']

    • (1, 2) and {'a': 3}

    • C

      (1, 2, 3) and {}

    • D

      {1, 2} and {'a': 3}

    args is a tuple of positional arguments and kwargs a dict of keyword arguments.

  17. 17

    A recursive function lacks a base case. What happens in Python?

    • A

      It returns None

    • B

      It runs forever without error

    • RecursionError after about 1,000 nested calls

    • D

      SyntaxError

    Each call adds a frame to the call stack until the recursion limit is exceeded.