nerdexam
Python_Institute

PCEP-30-02 · Question #352

What is the expected result of the following code? ``python def velocity(x): return speed + x speed = 10 new_speed = velocity(10) new_speed = velocity(speed) print(new_speed) ``

The correct answer is C. 20. Option C (20) is correct because Python functions can access global variables - when velocity(speed) is called, speed is 10 in the global scope, so the function computes 10 + 10 = 20, which is what gets printed. Option A is wrong because the code runs without error; Python…

Question

What is the expected result of the following code?
def velocity(x):
 return speed + x

speed = 10
new_speed = velocity(10)
new_speed = velocity(speed)
print(new_speed)

Options

  • AThe code is erroneous and cannot be run.
  • B10
  • C20
  • D30

How the community answered

(45 responses)
  • A
    4% (2)
  • B
    13% (6)
  • C
    76% (34)
  • D
    7% (3)

Explanation

Option C (20) is correct because Python functions can access global variables - when velocity(speed) is called, speed is 10 in the global scope, so the function computes 10 + 10 = 20, which is what gets printed. Option A is wrong because the code runs without error; Python resolves speed as a global variable inside the function. Option B (10) is wrong because the function always adds x to speed, so the minimum return value is 20. Option D (30) is a common trap - test-takers may think the two calls accumulate (20 + 10), but the second assignment new_speed = velocity(speed) simply overwrites the first, so new_speed is 20, not 30.

Memory tip: When you see a variable used inside a function but never defined there, Python looks it up in the outer (global) scope - and remember that = always replaces the previous value, it never adds to it.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice