Bird
0
0

What will be the result of this pytest assertion?

medium📝 Predict Output Q4 of 15
PyTest - Writing Assertions
What will be the result of this pytest assertion?
list1 = [1, 2, 3]
list2 = list1
assert list1 is list2
ASyntax error due to incorrect assertion
BThe assertion fails because the lists have the same values but are different objects
CThe assertion passes because both variables refer to the same list object
DRuntime error because lists cannot be compared with 'is'
Step-by-Step Solution
Solution:
  1. Step 1: Analyze variable assignment

    Both list1 and list2 point to the same list object because list2 = list1 assigns the reference.
  2. Step 2: Understand the assertion

    assert list1 is list2 checks identity, which is true here.
  3. Final Answer:

    The assertion passes because both variables refer to the same list object -> Option C
  4. Quick Check:

    Identity assertion passes = A [OK]
Quick Trick: Assigning one variable to another shares the same object [OK]
Common Mistakes:
MISTAKES
  • Assuming different variables always mean different objects
  • Confusing 'is' with '=='
  • Expecting failure when references are the same

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes