PCEP Practice Question: Data Types, Variables, Basic I/O and Operators
A developer needs to store the result of dividing two numbers, a/b, but only if b is not zero. They write: result = a / b if b != 0 else 'undefined'. What is the data type of result when b is zero?
⚠ Common exam trap
Python Institute often tests the ternary conditional expression to see if candidates mistakenly think the else branch returns a special 'undefined' value (like in JavaScript) instead of recognizing it as a plain Python string literal.
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
✓
str
When `b` is zero, the expression `a / b if b != 0 else 'undefined'` evaluates to the string literal `'undefined'`. Therefore, the variable `result` is assigned a value of type `str` (string). The conditional expression explicitly returns a string in the else branch, making option D correct.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
float
Why it's wrong here
When b equals zero the conditional expression evaluates the else branch, producing the string 'undefined', whose type is str, not float. Float would be correct only when b is non-zero, since true division of two numbers always returns float in Python 3, which is what makes this option tempting.
- ✗
NoneType
Why it's wrong here
The else branch returns the string literal 'undefined', so the type is str; NoneType arises only from functions lacking a return statement or explicit None. NoneType tempts because 'undefined' suggests absence of a value, but Python has no undefined keyword and the literal is plain text.
- ✗
bool
Why it's wrong here
The else branch yields the string 'undefined', giving type str, not bool. Bool tempts because the condition b != 0 itself evaluates to a boolean, but that condition only selects the branch; it is never assigned to result, so no boolean value reaches the variable.
- ✓
str
Why this is correct
When b equals zero, the conditional expression selects the literal 'undefined', a string, so result holds str. The division branch is never evaluated, avoiding ZeroDivisionError; the ternary returns whichever operand's type applies, and here that operand is textual.
Go deeper
Related to this question
About these practice questions
One of 482 original PCEP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCEP 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 PCEP exam.