Computer Science
Predict the output of the following code.
def code (n) :
if n==0:
print('Finally')
else:
print(n)
code (2-2)
code (15)
Answer
Finally
15
Working
The code defines a function code(n)
that takes a single argument n
. Inside the function, if the input n
is equal to 0, it prints "Finally", otherwise, it prints the value of n
. In the first function call code(2-2)
, the expression 2-2 evaluates to 0, so the function prints "Finally". Conversely, in the second function call code(15)
, the value 15 is not equal to 0, so the function prints 15.
Related Questions
Compare and contrast the use of iteration and recursion in terms of memory space and speed.
Predict the output of the following code.
def code(n): if n == 0: print ( 'Finally' ) else: print (n) code (3) code (15)
Predict the output of the following code.
def code(n): if n == 0: print(' Finally') else: print (n) code (10)
Predict the output of the following code.
def code (n) : if n==0: print ( 'Finally' ) else: print (n) print (n-3) code (10)