Bird
0
0

What will be the output when running this Python file directly?

medium📝 Predict Output Q13 of 15
Python - Modules and Code Organization
What will be the output when running this Python file directly?
def greet():
    print('Hello!')

if __name__ == '__main__':
    greet()
AHello! Hello!
BNo output
CError: greet() undefined
DHello!
Step-by-Step Solution
Solution:
  1. Step 1: Check if condition when run directly

    Since the file runs directly, __name__ == '__main__' is True, so greet() is called.
  2. Step 2: Understand greet() function output

    The function prints "Hello!" once.
  3. Final Answer:

    Hello! -> Option D
  4. Quick Check:

    Function called once prints "Hello!" [OK]
Quick Trick: Direct run triggers greet() printing once [OK]
Common Mistakes:
  • Thinking greet() runs twice
  • Assuming no output without main guard
  • Confusing function call with definition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes