94 views
1 1 vote

Consider the following Python code:

def first():
    print("A")

def second():
    print("B")
    first()
    print("C")

print("Start")
second()
print("End")

What is the output of the code above?

  1. Start
    A
    B
    C
    End
  2. Start
    B
    A
    C
    End
  3. B
    A
    C
    Start
    End
  4. Start
    B
    C
    A
    End

1 Answer

0 0 votes

Function bodies do not execute when the function is defined. They execute only when the function is called.

First, Python executes:

print("Start")

So the first output is $\texttt{Start}$.

Then $\texttt{second()}$ is called. Inside $\texttt{second()}$, Python first prints $\texttt{B}$.

Then $\texttt{first()}$ is called, so Python enters $\texttt{first()}$ and prints $\texttt{A}$.

After $\texttt{first()}$ finishes, control returns to $\texttt{second()}$, and $\texttt{C}$ is printed.

Finally, after $\texttt{second()}$ finishes, Python prints $\texttt{End}$.

Therefore, the output is:

Start
B
A
C
End

Correct Option: B

Answer:
Position:
Show:

Related questions

0 0 votes
1 1 answer
123
123 views
GO Classes asked Jun 19
123 views
Consider the following Python code:def update(lst): lst = lst[0] + lst lst.append(lst[-1] + 1) return lst[:2] nums = [3, 5] result = update(nums) print(nums, result)Wha...
1 1 vote
1 1 answer
106
106 views
GO Classes asked Jun 19
106 views
Consider the following Python code:def search(nums, target): for x in nums: print(x) if x == target: return x + 10 return -1 print(search([2, 4, 6, 8], 6))What is the out...
1 1 vote
1 1 answer
104
104 views
GO Classes asked Jun 19
104 views
Consider the following Python code:def show(x): print(x + 1) return x * 2 value = show(3) print(value)What is the output of the code above?4 66 44 None3 6
0 0 votes
1 1 answer
111
111 views
GO Classes asked Jun 19
111 views
Consider the following Python code:def compute(a, b): a = a + 2 b = b * 2 return a + b x = 3 y = 4 ans = compute(x, y) print(ans, x, y)What is the output of the code abov...