nerdexam
Python_Institute

PCEP-30-02 · Question #184

What is the expected output of the following code? def func1(a): return a * a def func2(a): return func1(a) func1(a) print(func2(2))

The correct answer is D. 16. D (16) is correct because tracing the execution reveals two nested calls: func1(2) computes 2 * 2 = 4, and func2(2) multiplies that result by itself - 4 4 = 16. A (4) is wrong because it reflects only func1(2) in isolation, ignoring that func2 calls func1 twice and multiplies…

Question

What is the expected output of the following code? def func1(a): return a ** a def func2(a): return func1(a) * func1(a) print(func2(2))

Options

  • A4
  • BThe code is erroneous.
  • C2
  • D16

How the community answered

(17 responses)
  • B
    6% (1)
  • C
    12% (2)
  • D
    82% (14)

Explanation

D (16) is correct because tracing the execution reveals two nested calls: func1(2) computes 2 ** 2 = 4, and func2(2) multiplies that result by itself - 4 * 4 = 16.

A (4) is wrong because it reflects only func1(2) in isolation, ignoring that func2 calls func1 twice and multiplies the results. C (2) is wrong because it represents neither operation - it's just the original input. B is wrong because the code is syntactically and semantically valid Python; the ** operator is exponentiation, not a syntax error.

Memory tip: Read inside-out. Resolve the innermost function call first (func1(2) = 4), then substitute that value everywhere func1(a) appears in func2 - making it 4 * 4 = 16. Think of func2 as "double the power."

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice