Working With Funtion || Important Questions || Sumita-Arora || Preeti-Arora || Computer Science || Class 12
Q. What is a function? How is it useful?
Q. A program having multiple function is considered better designed than a program without any function. Why?
Q. What is an argument? Give an example.
Q. What is Python module? What is its significance?
Q. What is the utility of: -
(I) default arguments
(II) Keyword arguments?
Q. What is the utility of the built-in function help()?
Q. What is the significance of having functions in a program?
Q. Describe the different styles of functions in Python using appropriate examples.
Q. What will the following function return?
def addEm(x, y, z):
print (x + y + z)
Q. Trace the following code and predict the output produced by it
1. def power (b, p):
2. y = b ** P
3. return y
4.
5. diff calcsquare (x) :
6. a = power (x, 2)
7. return a
8.
9. n = 5
10. result = calcsquare (n) + power (3,3)
11. print (result)
Q .What is the output of following code fragments?
(i)
def increment(n):
n. append([4])
return n
L = [1, 2, 3]
M = increment(L)
print(L, M)
(ii)
def increment(n):
n. append([49])
return n[0], n[1], n[2], n[3]
L = [23, 35, 47]
mi, m2, m3, m4 = increment (L)
print(L)
print(mi, m2, m3, m4)
print(L[3] == m4)
Q .In the following code, which variables are in the same scope?
def func1():
a = 1
b = 2
def func2():
c = 3
d = 4
e = 5
Q. What is the difference between local variable and global variable? Also give a suitable Python code to illustrate both.
Q. Is return statement optional? Compare and comment on the following two return statements:
return
return val
Q . 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.
Q. Write a Python function to find the maximum of three numbers.
Q. Write a Python function that accepts a string and calculate the number of uppercase letters and lowercase letters.
Sample String : PythonProgramminG
Expected Output:
Original String : PythonProgramminG
No. of Uppercase characters : 3
No. of Lowercase characters : 14
Q. Write a function that takes a number n and then returns a randomly generated number having exactly n digits (not starting with zero) eg ., if n is 2 then function can randomly return a number 10-99 but 07, 02 etc. are not valid two digit numbers.
Q. What is the difference between the formal parameters and actual parameters? What are their alternative names? Also, give a suitable Python code to illustrate both.
Q. Write a program that generates 4 terms of an AP by providing initial and step values to a function that returns first four terms of the series.
Q. Write a Python function that takes a number as a parameter and checks whether the number is prime or not.
Q. Write a Python function that checks whether a passed string is a palindrome or not.
Q. Write a function that takes a character and returns true if it is a vowel, false otherwise.
Q. From the program code given below, identify the parts mentioned below:
def processNumber(x):
x = 72
return x + 3
y = 54
res = process Number (y)
Identify these parts: function header, function call, arguments, parameters, function body, main program
Q. Consider a function with following header:
def info(object, spacing = 10, collapse = 1):
Here are some function calls given below. Find out which of these are correct and which of these are incorrect stating reasons:
a. info( obj1)
b. info(spacing= 20)
c. info( obj2, 12)
d. info( obj11, object = obj12)
e. info( obj3, collapse = 0)
f. info()
g. info(collapse = 0, obj3)
h. info( spacing= 15, object = obj4)
Q. What is the default return value for a function that does not return any value explicitly?
(a) None
(b) int
(c) double
(d) null
Q. What will following code print?
def addEm(x, y, z):
print (x + y + z)
def prod (x, y, z):
return x * y * z
a = addEm (6, 16, 26)
b = prod (2, 3, 6)
print(a, b)
Why did Python not report an error when void functions do not return a value?
Q. Consider below given function headers. Identify which of these will cause error and why?
(i) def func(a = 1, b):
(ii) def func(a = 1, b, c = 2):
(iii) def func(a = 1, b = 1, c = 2):
(iv) def func (a = 1, b = 1, c = 2, d):
Q. Following code intends to swap the values of two variables through a function, but upon running the code, the output shows that the values are swapped inside the switch() function but back again in main program, the variables remain un-swapped. What could be the reason? Suggest a remedy.
def switch(x, y):
x, y = y, x
print("inside switch :", end = ' ')
print("x", x, "y=", y)
X = 5
y = 7
print("x", x, "y=", y)
switch(x, y)
print("x", x, "y=", y)
Q. Following code intends to add a given value to global variable a. What will the following code produce ?
def increase(x): #1
a = a + x #2
return #3
#4
a = 20 #5
b = 5 #6
increase(b) #7
print(a) #8
Q. Which names are local, which are global and which are built-in in the following code fragment?
invaders = 'Big names'
pos = 200
level = 1
def play():
max_level = level + 10
print(len (invaders ) == 0)
return max_level
res = play()
print (res)
Q. Find and write the output of the following python code:
a = 10
def call():
global a
a = 15
b = 20
print(a)
call()
Q. Predict the output of the following code fragment ?
def func(message, num = 1):
print (message * num)
func("Python")
func('Easy', 3)
Q. Find and write the output of the following python code:
def fun(s):
k = len(s)
m = ""
for i in range(0,k):
if(s[i].isupper()):
m = m+s[i].lower()
elif s[i].isalpha():
m = m+s[i].upper()
else:
m = m + 'bb'
print(m)
fun('school2@com')
Q. Find and write the output of the following python code:
def Change (P, Q = 30):
P = P + Q
Q = P - Q
print (P, "#", Q)
return (P)
R = 150
S = 100
R = Change (R, S)
print (R, "#", S)
S = Change (S)
Q. Predict the output of the following code fragment ?
def check (n1 = 1, n2 = 2):
n1 = n1 + n2
n2 += 1
print (n1, n2)
check()
check (2, 1)
check (3)
Q. What is the output of the following code?
a = 1
def f ( ) :
a = 10
print (a)
Q. What will be the output of following code?
def interest (prnc, time = 2, rate = 0.10) :
return (prnc* time * rate)
print (interest (6100, 1))
print (interest (5000, rate = 0.05))
print (interest (5000, 3, 0.12 ))
print (interest (time = 4, prnc = 5000))
Q. Is return statement optional ? Compare and comment on the following two return statements :
return
return val
Q. Write a function that takes a positive integer and returns the one's position digit of the integer.
Q. Write a function that receives an octal number and prints the equivalent number in other number bases i.e., in decimal, binary and hexadecimal equivalents.
Q. Write a program that generates 4 terms of an AP by providing initial and step values to a function that returns first four terms of the series.
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )