In: Computer Science
I don't know which one is wrong. Can you please check.
Thank you
QUESTION 1
In the following code, how many and which functions have been defined/declared?
def powerNum(power):
pNum = n ** power
return pNum
n = int(input("Enter a number whose power you want to calculate: "))
def main():
p = int(input("Enter the power number: "))
print ("Power of the number you entered is", powerNum (p))
main()
1 function: powerNum |
||
1 function: powerNum |
||
3 functions: powerNum, and main is defined/declared twice. |
||
2 functions: powerNum and main |
QUESTION 2
In the following code, which of the variable(s) is (are) considered as global variable(s):
def powerNum(power):
pNum = n ** power
return pNum
n = int(input("Enter a number whose power you want to calculate: "))
def main():
p = int(input("Enter the power number: "))
print ("Power of the number you entered is", powerNum (p))
main()
p and power |
||
pNum |
||
p |
||
power |
||
n |
QUESTION 3
In the following code, which line of code is returning the value for function powerNum?
def powerNum(power):
pNum = n ** power
return pNum
n = int(input("Enter a number whose power you want to calculate: "))
def main():
p = int(input("Enter the power number: "))
print ("Power of the number you entered is", powerNum (p))
main()
def powerNum(power): |
||
print ("Power of the number you entered is", powerNum (p)) |
||
return pNum |
||
pNum = n ** power |
QUESTION 4
In the following code, which of the variable(s) is (are) considered as parameter variable(s):
def powerNum(power):
pNum = n ** power
return pNum
n = int(input("Enter a number whose power you want to calculate: "))
def main():
p = int(input("Enter the power number: "))
print ("Power of the number you entered is", powerNum (p))
main()
n |
||
pNum |
||
p and power |
||
p |
||
power |
QUESTION 5
After learning about the issues with global variables, Amita took the old code with global variables in cell A and re-wrote without global variables. Based on the partial code done in the cell below, what changes, if any, need to be made to the code in cell-B below?
cell-A: Old code with global variables | cell-B: Partial new code without global variables |
def powerNum(power): pNum = n ** power return pNum n = int(input("Enter a number whose power you want to calculate: ")) def main(): p = int(input("Enter the power number: ")) print ("Power of the number you entered is", powerNum (p)) main() |
def powerNum(power): pNum = n ** power return pNum def main(): n = int(input("Enter a number whose power you want to calculate: ")) p = int(input("Enter the power number: ")) print ("Power of the number you entered is", powerNum (p)) main() |
We need to just change powerNum function to accept two parameters: one for power value and one for the number as below def powerNum(power, n): |
||
No changes need to be made to main or powerNum functions |
||
Yes, powerNum should now be defined with two parameters: one for power value and one for the number as below def powerNum(power, n): Also we will need to change how the function is called in main function as below: powerNum(p,n) |
||
We need to just pass the additional argument when the powerNum function is called in main function as below: powerNum(p,n) |
QUESTION 6
In the following code, which line of code is calling or invoking function powerNum?
def powerNum(power):
pNum = n ** power
return pNum
n = int(input("Enter a number whose power you want to calculate: "))
def main():
p = int(input("Enter the power number: "))
print ("Power of the number you entered is", powerNum (p))
main()
return pNum |
||
def powerNum(power): |
||
pNum = n ** power |
||
print ("Power of the number you entered is", powerNum (p)) |
QUESTION 7
In the following code, which line of code is defining or declaring function powerNum?
def powerNum(power):
pNum = n ** power
return pNum
n = int(input("Enter a number whose power you want to calculate: "))
def main():
p = int(input("Enter the power number: "))
print ("Power of the number you entered is", powerNum (p))
main()
def powerNum(power): |
||
return pNum |
||
pNum = n ** power |
||
print ("Power of the number you entered is", powerNum (p)) |
QUESTION 8
In the following code, which of the variable(s) is(are) considered as argument(s):
def powerNum(power):
pNum = n ** power
return pNum
n = int(input("Enter a number whose power you want to calculate: "))
def main():
p = int(input("Enter the power number: "))
print ("Power of the number you entered is", powerNum (p))
main()
p and power |
||
n |
||
power |
||
p |
||
pNum |
QUESTION 9
In python, how can you pass arguments to a function?
by keyword arguments only and can be in any order |
||
default is by position but you can also pass by keyword arguments as in the case below: show_interest (rate=0.01, periods=10, principal=10000.0) |
||
by position only and they must be passed in the same order in which they are defined in the function header |
||
by position only and they can be passed in any order irrespective of how are they are defined |
QUESTION 10
In the following code, in which order do the lines of code get executed?
def powerNum(power):
pNum = n ** power
return pNum
n = int(input("Enter a number whose power you want to calculate: "))
def main():
p = int(input("Enter the power number: "))
print ("Power of the number you entered is", powerNum (p))
main()
getting input n from the user >> calling main function >> calling powerNum function >> ending in powerNum function |
||
calling powerNum function >> getting input n from the user >> calling main function |
||
getting input n from the user >> calling main function >> calling powerNum function >> returning back to main function from powerNum function |
Answer 1:-
In this question 2 function were defined/declaired, one is 'main' and other one is 'powerNum', so correct option is,
2 functions: powerNum and main
Answer 2:-
Variable 'n' doesn't belong to any function scope, it is defined outside any function. Hence it is global variable, so correct option is,
n
Answer 3:-
"return pNum" statement is returning value from "powerNum" function
Answer 4:-
Variables present in function signature is called function parameters, so correct option is,
power
Answer 5:-
Since there is no local variable with name 'n', so there won't be any issue, correct option is,
No changes need to be made to main or powerNum functions
Answer 6:-
Function call contains function names followed by paranthesis, so correct option is,
print ("Power of the number you entered is", powerNum (p))
Answer 7:-
Function declairation/defination starts with "def" keyword, so correct option is,
def powerNum(power)
Answer 8:-
Variables that are written inside the paranthesis during function call are called function arguments, correct option is,
p
Answer 9:-
default is by position but you can also pass by keyword arguments as in the case below:
show_interest (rate=0.01, periods=10, principal=10000.0)
Answer 10:-
getting input n from the user >> calling main function >> calling powerNum function >> returning back to main function from powerNum function