Computer Science
What is the output of the program given below?
x = 50
def func(x):
x = 2
func(x)
print('x is now', x)
- x is now 50
- x is now 2
- x is now 100
- Error
Answer
x is now 50
Reason — The output of the program will be x is now 50
. This is because the variable x
inside the func
function is a local variable, so any changes made to it inside the function do not affect the global variable x
defined outside the function. Therefore, after calling the func
function, the value of x
remains unchanged and is still 50 when printed.
Related Questions
Which of the following arguments works with implicit values that are used if no value is provided?
- keyword
- required
- variable-length
- default
Which values are used by the functions to communicate information back to the caller?
- local
- global
- return
- random
Which is the most appropriate definition for recursion?
- A function that calls itself
- A function execution instance that calls another execution instance of the same function
- A class method that calls another class method
- An inbuilt method that is automatically called
Fill in the line of code for calculating the factorial of a number:
def fact(num): if num == 0: return 1 else: return...............
- num*fact (num-1)
- (num-1) * (num-2)
- num* (num-1)
- fact (num) *fact (num-1)