Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q5 of Q15
Python - Standard Library Usage
What will be the output of this code?
import random
print(random.choice(['apple', 'banana', 'cherry']))
APrints the whole list
BAlways prints 'apple'
CError: choice not defined
DRandomly prints one fruit from the list
Step-by-Step Solution
Solution:
  1. Step 1: Understand random.choice function

    random.choice picks one random item from the given list.
  2. Step 2: Predict output behavior

    The output will be one fruit randomly selected each time the code runs.
  3. Final Answer:

    Randomly prints one fruit from the list -> Option D
  4. Quick Check:

    random.choice(list) = random element [OK]
Quick Trick: random.choice picks one random item [OK]
Common Mistakes:
MISTAKES
  • Expecting fixed output
  • Thinking choice is undefined

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes