All sample questions / Python
What does print(a is b) print? Python Data Types, Numbers and Strings
Block 1, Data Types, Numbers and Strings, question 4 of the Python sample
a = 'hello'
b = 'hello'
print(a is b)
What does this print?
Show the answer and why each option is right or wrong
Answer, B. True
- A. False
- Assumes only numeric literals benefit from caching, and that string literals are always built as separate objects even when identical.
- B. True
- Correct. Short string literals that look like identifiers are automatically interned by CPython, so both names end up pointing to the same object.
- C. None
- Confuses a successful identity check with a failed lookup that would produce a missing value.
- D. 1
- Reports the truthy integer one instead of the actual boolean literal that is produces.
CPython automatically interns string literals that look like valid identifiers, reusing a single object for every occurrence of the same text within the same compiled code. This is an implementation detail, the language itself never promises that equal strings share the same object.
The answer above is what the CPython interpreter printed when the snippet was run, not what someone expected it to do.
This question comes from a bank of 600 Python interview questions, all built the same way, sold as one PDF with the full answer key.
Get the 600 Python questions, 9 euros
Or SQL, Python, pandas and NumPy together, 1,636 questions, 19 euros
Or play the free Python sample, twenty questions in the browser, also as a free PDF.
Previous question Next question
Did this save you a lookup? Buy me a coffee on Ko-fi.