A. RuntimeError because lists cannot be compared with is
B. No error; assertion passes because lists have same values
C. SyntaxError due to wrong use of is
D. Assertion fails because lists are different objects despite same values
Solution
Step 1: Understand list object identity
Two lists with same values are different objects in memory unless explicitly assigned.
Step 2: Check assertion behavior
The assertion assert list1 is list2 fails because is checks identity, not equality.
Final Answer:
Assertion fails because lists are different objects despite same values -> Option D
Quick Check:
Same values ≠ same object [OK]
Hint: Use == for value equality, is for identity [OK]
Common Mistakes:
Expecting is to check value equality
Confusing object identity with content equality
Assuming lists with same content share identity
5. You want to write a pytest test to confirm that a function returns the exact same singleton object config every time it is called. Which assertion is best?
hard
A. assert get_config() is config
B. assert get_config() == config
C. assert get_config() is not config
D. assert get_config() != config
Solution
Step 1: Understand singleton identity check
To confirm the function returns the exact same object, use is to check identity.
Step 2: Evaluate options
assert get_config() == config checks value equality, which may be true for different objects. assert get_config() is config correctly asserts identity. Options C and D check for difference, which is not desired.
Final Answer:
assert get_config() is config -> Option A
Quick Check:
is for singleton identity [OK]
Hint: Use is to confirm same singleton object returned [OK]
Common Mistakes:
Using == which checks value, not identity
Using is not by mistake
Not understanding singleton pattern in tests
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.