PCEP-30-02 · Question #250
Consider the following code. 1 data = eval(input('Input: ')) 2 print('Output: ', data) Which of the inputs below would produce the specified output?
The correct answer is D. Input: [x**2 for x in range(1, 4)] Output: [1, 4, 9]. Option D is correct because eval() executes its argument as a Python expression - [x2 for x in range(1, 4)] is a valid list comprehension that evaluates to [1, 4, 9], matching the expected output exactly. Option B fails because Hello Python (without quotes) is not a valid…
Question
Options
- ANone of the above.
- BInput: Hello Python Output: Hello Python
- CBoth are correct.
- DInput: [x**2 for x in range(1, 4)] Output: [1, 4, 9]
How the community answered
(28 responses)- A11% (3)
- B4% (1)
- C7% (2)
- D79% (22)
Explanation
Option D is correct because eval() executes its argument as a Python expression - [x**2 for x in range(1, 4)] is a valid list comprehension that evaluates to [1, 4, 9], matching the expected output exactly.
Option B fails because Hello Python (without quotes) is not a valid Python expression - eval() would try to interpret Hello and Python as variable names, raising a NameError. To produce the string Hello Python, the input would need to be 'Hello Python' (with quotes).
Since B is incorrect, option C ("Both are correct") is also wrong, and since D does work, option A ("None of the above") is wrong as well.
Memory tip: Think of eval() as a mini Python interpreter - whatever you type must be something Python could run as a standalone expression. Bare words without quotes aren't valid expressions, but literals, operators, and comprehensions are.
Community Discussion
No community discussion yet for this question.