Function || Preeti Arora || Class 12 || Unsolved Question || Computer science || Solution
Q1. A program having multiple function is considered better designed than a program without any function. Why?
Q2. Write a function called calculate_area() that takes base and height as input arguments and returns area of a triangle as an output. The formula used is:
Triangle Area = 1/2*base*height
Q3. Modify the above function to take a third parameter called shape type. Shape type should be either triangle or rectangle. Based on the shape, it should calculate the area.
Formula used: Rectangle Area = length*width
Q4. Write a function called print_pattern that takes integer number as argument and prints the following pattern.
If the input number is 3.
*
* *
* * *
If input is 4, then it should print:
*
* *
* * *
* * * *
Q5. What is the utility of: -
(I) default arguments
(II) Keyword arguments?
Q6. What are operators? Give examples of some unary and binary operators.
Q7. Describe the different styles of functions in Python using appropriate examples.
Q8. What is scope? What is the scope resolving rule of Python?
Q9. What is the difference between local and global variables?
Q10. Write the term suitable for following description:
(a) A name inside the parentheses of a function header that can receive a value.
(b) An argument passed to a specific parameter using the parameter name.
(c) A value passed to a function parameter.
(d) A value assigned to a parameter name in the function header.
(e) A value assigned to a parameter name in the function call.
(f) A name defined outside all function definitions.
(g) A variable created inside a function body.
Q11. Consider the following code and write the flow of execution for this. Line numbers have been given for our reference.
def power (b, p): 1
y = b ** p 2
return y 3
4
def calcSquare(x): 5
a = power (x, 2) 6
return a 7
8
n = 5 9
result = calcSquare(n) 10
print (result) 11
Q12. What will the following function return?
def addEm(x, y, z):
print (x + y + z)
Q13. What will be the output displayed when addEM() is called/executed?
def addEm(x, y, z):
return x + y + z
print (x+ y + z)
Q14. What will be the output of the following programs?
(i)
num = 1
def myfunc ():
return num
print(num)
print(myFunc())
print(num)
(ii)
num = 1
def myfunc():
num = 10
return num
print (num)
print(myfunc())
print(num)
(iii)
num = 1
def myfunc ():
global num
num = 10
return num
print (num)
print(myfunc())
print(num)
(iv)
def display():
print("Hello", end = ' ')
display()
print("there!")
Q15. Predict the output of the following code:
a = 10
y = 5
def myfunc():
y = a
a = 2
print("y =", y, "a =", a)
print("a+y =", a + y)
return a + y
print("y =", y, "a =", a)
print(myfunc())
print("y =", y, "a =", a)
Q16. What is wrong with the following function definition?
def addEm(x, y, z):
return x + y + z
print("the answer is", x + y + z)
Q17. Find the errors in the code given below:
(a)
def minus (total, decrement)
output = total - decrement
print(output)
return (output)
(b)
define check()
N = input ("Enter N: ")
i = 3
answer = 1 + i ** 4 / N
Return answer
(c)
def alpha (n, string ='xyz', k = 10) :
return beta(string)
return n
def beta (string)
return string == str(n)
print(alpha("Valentine's Day") :)
print(beta (string = 'true' ))
print(alpha(n = 5, "Good-bye") :)
Q18. Define flow of execution. What does it do with functions?
Q19. Write a function that takes amount-in-dollars and dollar-to-rupee conversion price; it then returns the amount converted to rupees. Create the function in both void and non-void forms.
Q20. Write a function to calculate volume of a box with appropriate default values for its parameters. Your function should have the following input parameters:
(a) Length of box;
(b) Width of box;
(c) Height of box.
Test it by writing complete program to invoke it.
Q21. Write a program to display first four multiples of a number using recursion.
Q22. What do you understand by recursion? State the advantages and disadvantages of using recursion.
Q23. Write a recursive function to add the first 'n' terms of the series:
1+1/2-1/3+1/4-1/5……
Q24. Write a program to find the greatest common divisor between two numbers.
Q25. Write a Python function to multiply all the numbers in a list.
Sample List: (8, 2, 3, -1, 7)
Expected Output: -336
Q26. Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number whose factorial is to be calculated as the argument.
Q27. Write a Python function that takes a number as a parameter and checks whether the number is prime or not.
Q28. Write a Python function that checks whether a passed string is a palindrome or not.
Note: A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.
Q29. Write a Python program that accepts a hyphen-separated sequence of words as input and prints the words in a hyphen-separated sequence after sorting them alphabetically.
Sample Items: green-red-yellow-black-white
Expected Result: black-green-red-white-yellow
Q30. What is a recursive function? Write one advantage of recursive functions.
Q31. What are the two cases required in a recursive function?
Q32. What is base case?
Q33. What is recursive case?
Q34. Is it necessary to have a base case in a recursive function? Why/Why not?
Q35. Identify the base case(s) in the following recursive function:
def function(n) :
if n == 0 :
return 5
elif n == 1 :
return 8
elif n > 0 :
return function1(n-1) + function1(n-2)
else:
return -1
Q36. Why are recursive functions considered slower than their iterative counterparts?
Q37. Compare and contrast the use of iteration and recursion in terms of memory space and speed.
Q38. Predict the output of following codes.
(a)
def codo (n):
if n== 0:
print('Finally ')
else:
print(n)
codo(n - 3)
codo(15)
(b)
def codo (n):
if n== 0:
print('Finally ')
else:
print(n)
codo(n - 2)
codo(15)
(c)
def codo (n):
if n== 0:
print('Finally')
else:
print(n)
codo(n - 2)
codo(10)
(d)
def codo (n):
if n== 0:
print('Finally')
else:
print(n)
codo(n - 3)
codo(10)
Q39. Predict the output of following code.
def express(x, n):
if n== 0:
return 1
elif n%2 == 0:
return express(x*x, n/2))
else:
return x * express (x, n-1)
express (2,5)
Q40. Consider following Python function that uses recursion:
def check(n):
if n <= 1:
return True
elif n % 2 == 0 :
return check(n/2)
else:
return check(n/1)
What is the value returned by check(8)?
Q41. Can you find an error or problem with the above code? Think about what happens if we evaluate check(3). What output would be produced by Python? Explain the reason(s) behind the output.
Q42. Consider the following Python function Fn(), that follows:
def Fn(n):
print(n, end=" ")
if n< 3:
return n
else:
return Fn(n // 2) - Fn(n//3)
What will be the result produced by following function calls?
(a) Fn(12)
(b) Fn(10)
(c) Fn(7)
Q43. Figure out the problem with following code that may occur when it is run?
def recur(p) :
if p == 0:
print("##")
else:
recur(p)
p=p-1
recur(5)
where are the solutions to these questions?
ReplyDeleteClick on question to get answer of that question.
DeletePlease add more programs as per new edition 2022 of preeti arora. Some of the programs are not here. Kindly add them.
ReplyDeletePlease send me questions.
Deletethere are 49 questions in the 2022 edition
ReplyDeletePlease send me that question.
DeleteI need explanation for question 42???
ReplyDeletethere are more question pls give them too!!!!
ReplyDeleteok 10000000000000000000000 q
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )