PCAP Strings Practice Question
Under CPython, what is the result of the following code? a = 'hello'; b = 'hello'; print(a is b)
⚠ Common exam trap
The Python Institute often tests the distinction between 'is' (identity) and '==' (equality), and the trap here is that candidates assume 'is' compares values, leading them to incorrectly choose False when they think two separate string objects are created.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
True
CPython interns short strings as an optimization, meaning both variables 'a' and 'b' reference the same immutable string object in memory. The 'is' operator checks object identity, not value equality, so it returns True when both variables point to the same interned object.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
True
Why this is correct
True is correct because in CPython, string literals are automatically interned, meaning that two identical string literals are stored in the same memory location. When the code compares them with the identity operator 'is', it compares object memory addresses, and since both variables point to the same interned object, the result is True. This is a well-known CPython behavior, though it is an implementation detail rather than part of the Python language specification. The exact code likely creates two variables with the same string literal, and interning makes their identities match.
- ✗
False
Why it's wrong here
False is incorrect because in CPython, identical string literals are interned, so the two strings actually refer to the exact same object in memory. If interning did not occur, two separate objects would have different identities, and 'a is b' would return False, but that is not the case for string literals processed by the CPython compiler. False would be the outcome only when comparing two distinct objects that share the same value, such as dynamically constructed strings of certain types, but that is not applicable here. Therefore, expecting False contradicts CPython's interning of literal strings.
- ✗
NameError
Why it's wrong here
NameError is unrelated because the code successfully defines both variables by assigning string literals to them, so no name lookup fails. A NameError is raised only when code attempts to reference a variable or name that has not been defined or is out of scope, which does not happen in this snippet. Since the variables are clearly assigned before the comparison, the expression executes without any name-resolution problem. Thus, the possibility of a NameError is entirely absent in this scenario.
- ✗
None
Why it's wrong here
None is impossible because the 'is' operator always returns a boolean value, either True or False, when it compares two object identities. The expression 'a is b' evaluates to a bool, never to None, which is a special singleton object used to represent the absence of a value. While None is itself an object, it cannot be the result of an identity comparison because the comparison operation is defined to yield a boolean. Since both a and b are valid references, the result must be a boolean, not None.
Go deeper
Related to this question
About these practice questions
This PCAP question is part of Courseiva's 421-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.