Bird
0
0

What will be the output of the following code snippet?

medium📝 Predict Output Q5 of 15
Python - Modules and Code Organization
What will be the output of the following code snippet?
from random import randint
print(randint(1, 3))
import random
print(random.randint(4, 6))
AA random integer between 1 and 3, then a random integer between 4 and 6
BError because randint is imported twice
CTwo random integers both between 1 and 6
DError because random module is imported after from-import
Step-by-Step Solution
Solution:
  1. Step 1: Using from-import

    The statement from random import randint imports the function randint directly, so randint(1, 3) works and prints a random integer between 1 and 3.
  2. Step 2: Importing the module

    The statement import random imports the whole module, so random.randint(4, 6) calls the function from the module and prints a random integer between 4 and 6.
  3. Final Answer:

    A random integer between 1 and 3, then a random integer between 4 and 6 -> Option A
  4. Quick Check:

    from-import and import can coexist [OK]
Quick Trick: from-import imports names directly, import imports module [OK]
Common Mistakes:
  • Thinking importing twice causes error
  • Confusing function ranges
  • Assuming import order causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes