PCEP-30-02 · Question #107
Which of the following code snippets will print True to the monitor? (Choose two.)
The correct answer is A. ``` 1 print('is' in 'This IS Python code.') ``` D. ``` 1 print('t' in 'Peter') ```. Options A and D both use the in operator to test substring membership, which is case-sensitive: 'is' is found inside 'This...' and 't' is found inside 'Peter', so both return True. B is wrong because small integers like 42 are cached by CPython (integers -5 to 256 share the…
Question
Options
- A
1 print('is' in 'This IS Python code.') - B
1 x = 42 2 y = 42 3 print(x is not y) - C
1 x = 'Peter Wellert' 2 y = 'Peter Wellert'.lower() 3 print(x is y) - D
1 print('t' in 'Peter') - E
1 x = ['Peter', 'Paul', 'Mary'] 2 y = ['Peter', 'Paul', 'Mary'] 3 print(x is y)
How the community answered
(28 responses)- A71% (20)
- B14% (4)
- C4% (1)
- E11% (3)
Explanation
Options A and D both use the in operator to test substring membership, which is case-sensitive: 'is' is found inside 'Th**is**...' and 't' is found inside 'Pe**t**er', so both return True.
B is wrong because small integers like 42 are cached by CPython (integers -5 to 256 share the same object), so x is y is actually True, making x is not y → False. C is wrong because .lower() produces a new string object ('peter wellert'), which is a different object than x ('Peter Wellert'), so x is y is False. E is wrong because two separately created lists - even with identical contents - are always distinct objects in memory, so x is y is False.
Memory tip: Think of in as "is it inside?" (checks content/membership) and is as "is it the identical object?" (checks memory address). Confusing them is the core trap in this question - always reach for == to compare values and reserve is for identity checks like x is None.
Community Discussion
No community discussion yet for this question.