nerdexam
Python_Institute

PCEP-30-02 · Question #228

What is the expected output of the following code? ``python def func(x): if x % 2 == 0: return 1 else: return 2 print(func(func(2))) ``

The correct answer is A. 2. Option A is correct because evaluating func(func(2)) requires working from the inside out: func(2) checks whether 2 % 2 == 0 (true), so it returns 1; then func(1) checks whether 1 % 2 == 0 (false), so it returns 2, which is what gets printed. Option B is wrong because the code…

Question

What is the expected output of the following code?
def func(x):
 if x % 2 == 0:
 return 1
 else:
 return 2

print(func(func(2)))

Options

  • A2
  • BThe code is erroneous.
  • C0
  • D1

How the community answered

(49 responses)
  • A
    71% (35)
  • B
    8% (4)
  • C
    4% (2)
  • D
    16% (8)

Explanation

Option A is correct because evaluating func(func(2)) requires working from the inside out: func(2) checks whether 2 % 2 == 0 (true), so it returns 1; then func(1) checks whether 1 % 2 == 0 (false), so it returns 2, which is what gets printed.

Option B is wrong because the code is syntactically valid Python - single-space indentation is unusual but legal, as long as it's consistent within each block. Option D is the classic trap: 1 is the result of the inner call, but the outer call receives that 1 as input and returns 2, not 1. Option C is wrong simply because 0 is never returned by either branch of the function.

Memory tip: For nested function calls like f(f(x)), always evaluate the innermost call first and treat its return value as the argument to the outer call - then re-check the condition fresh with the new input.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice