Bird
0
0

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

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

if __name__ == '__main__':
    main()
ANo output
BRunning mainmain
CError: main() not defined
DRunning main
Step-by-Step Solution
Solution:
  1. Step 1: Check if condition when run directly

    When run directly, __name__ == '__main__' is True, so main() is called.
  2. Step 2: Output of main()

    The main() function prints "Running main" once.
  3. Final Answer:

    Running main -> Option D
  4. Quick Check:

    Direct run calls main() and prints output [OK]
Quick Trick: Direct run triggers main() inside __name__ check [OK]
Common Mistakes:
  • Expecting no output
  • Thinking main() is not defined
  • Assuming main() prints twice

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes