Question:

What will be the output of the following Python code?

def sayHello():
    print("Hello World")

sayHello()
sayHello()

Show Hint

In Python, a function runs only when it is called. If a function containing a print statement is called multiple times, the output will be printed the same number of times.
Updated On: Mar 14, 2026
  • Hello World
  • Hello World 
    Hello World 
     

  • Hello World Hello World
  • None of the above
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Solution and Explanation

Step 1: Understand the function definition.
The code first defines a function named \texttt{sayHello()}. Inside this function, there is a print statement that displays the text \[ \texttt{"Hello World"} \] The function does not execute immediately when it is defined. It only runs when the function is called.
Step 2: Observe how many times the function is called.
After defining the function, the program calls the function twice: \[ \texttt{sayHello()} \] \[ \texttt{sayHello()} \] Each time the function is called, the print statement inside the function executes once.
Step 3: Determine the output produced by each call.
The first function call prints \[ Hello\ World \] The second function call prints the same text again on the next line.
Thus the total output becomes \[ Hello\ World \] \[ Hello\ World \] Step 4: Conclusion.
Since the function is executed twice and each execution prints the same message, the output contains two lines of "Hello World". Therefore the correct answer is option (2).
Was this answer helpful?
0
0