0% found this document useful (0 votes)
117 views

Class 12 Computer Science

The document discusses functions in Python with examples and questions. It covers topics like function scope, returning multiple values from functions, purpose of return statement, built-in functions like randint() and input(), passing arguments to functions, function headers, calling functions, global and local variables in functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views

Class 12 Computer Science

The document discusses functions in Python with examples and questions. It covers topics like function scope, returning multiple values from functions, purpose of return statement, built-in functions like randint() and input(), passing arguments to functions, function headers, calling functions, global and local variables in functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

CLASS 12- COMPUTER SCIENCE

CHAPTER 3
Working with functions
TOPIC EXPLANATION /NOTES/QUESTIONS
Questions 1. What is the scope of a variable defined inside a function?
a) Global scope
b) Local scope
c) Universal scope
d) Function scope

2. In Python, can a function return multiple values simultaneously?


a) Yes
b) No
3. What is the purpose of the "return" statement in a function?
a) It specifies the type of the function.
b) It defines the input parameters of the function.
c) It indicates the end of a function.
d) It returns a value from the function to the caller.
4. Which of the following module functions generates an integer?
a) randint()
b) uniform()
c) random()
d) all of these
5. The return type of the input() function is
a) string
b) integer
c) list
d) tuple

6. The values being passed through a function call statements are called
a) Actual parameter
b) Formal parameter
c) default parameter
d) None of these

7. Which of the following components are part of a function header in Python?


a) Function Name
b) Return Statement
c) Parameter List
d) Both a and c

8. Which of the following function header is correct?


a) def cal_si(p=100, r, t=2)
b) def cal_si(p=100, r=8, t)
c) def cal_si(p, r=8, t)
d) def cal_si(p, r=8, t=2)
9. Which of the following is the correct way to call a function?
a) my_func()
b) def my_func()
c) return my_func
d) call my_func()
10. Consider the code given below:
Which of the following statements should be given in the blank for #Missing Statement, if
the output produced is 110?

a) global a
b) global b=100
c) global b
d) global a=100
11. What will be the output?

a) 5 b) 6
c) 4 d) This code will raise an error.
12. What will be the output of the following code?

13. What will be the output of the following code?

a) 100#52 b) 85#52
c) 85#33 d) 185#52

14. What will be the output of the following code?

a) [1, 2, 3, 4, 5, 6] b) [100, 2, 3, 4, 5, 6]
c) [100, 2, 3, 4, 5] d) [1, 2, 3, 4, 5]
15. Find and write the output of following python code:

16. Predict output:

17. What will be the output of the following code?

18. What will be the output of the following code?

19. What is the output of the following code snippet?

20. What is the output of the following code snippet?

21. What is the output of the following code snippet?


def Display(str):
m=""
for i in range(0,len(str)):
if(str[i].isupper()):
m=m+str[i].lower()
elif str[i].islower():
m=m+str[i].upper()
else:
if i%2==0:
m=m+str[i-1]
else:
m=m+"#"
print(m)
Display('Fun@Python3.0')
22. What is the output of the following code snippet?

23. Choose the correct output of the following Python code:


def ChangeList():
l=[]
l1=[]
l2=[]
for I in range(1,10):
l.append(i)
for i in range(0,1,-2):
l1.append(i)
for i in range (len(l1)):
l2.append(l1[i]+l[i])
l2.append(len(l)-len(l1))
print(l2)
ChangeList()
a)[11,10,9,8,7,4] b)[11,10,9,8,7,6]
c)[11,10,9,8,7] d)[10,9,8,7,6,5]
24. Choose the correct output of the following Python code:

25. What is the output of the following function call?

a. 8-3 b. Syntax Error c. -5 d. 5

26. What will be the output of the following code?


def fun(x=10, y=20):
x+=5
y=y-3
return x*y
print(fun(5),fun())
a. 20, 200 b. 170, 255 c. 85, 200 d. 300, 500
27. What will be the output of the following code?
v = 80
def display(n):
global v
v = 15
if n%4==0:
v += n
else:
v -= n
print(v, end="#")
display(20)
print(v)
a. 80#80 b. 80#100 c. 80#35 d. d 80#20
28. Find the output of the following:

29. Which of the following functions header is correct?


a. def study(a=2, b=5, c) : b. def study(a=2, b, c=5) :
c. def study(a, b=2, c=5): d. none of the above
30. In _______________ arguments we can skip the default argument, but
all the keyword arguments should match the parameters in the function
definitions.
a. keyword b. required
c. default d. none of the above.
31. In which part of memory does the system stores the parameter and
local variables of function call.
a. Heap b. Stack
c. Both a and b d. None of the above
32. Which of the following is the correct way to call a function?
a. my_func() b. def my_func()
c. return my_func d. call my_func()
33. Predict the output of the Python code given below:

34. What is the output of the following code snippet?


35. What will be the output of the following code?

36. Function writing question1.

37. Function writing question2

38. Function writing question3

39. Function writing question4


40. Function writing question5

41. Function writing question6

Write a function countNow(PLACES) in Python, that takes the dictionary, PLACES as an


argument and displays the names (in uppercase)of the places whose names are longer than 5
characters.
For example, Consider the following dictionary PLACES={1:"Delhi",2:"London",3:"Paris",4:"New
York",5:"Doha"}

The output should be:


LONDON
NEW YORK
def COUNTNOW(PLACES):
for P in PLACES:
if len(P)>5:
print( P)

42. Function writing question7


Write a Python function to sum all the numbers in a list. Sample List :
[8, 2, 3, 0, 7]
Expected Output : 20
43. Function writing question8
Write a function, lenWords(STRING), that takes a string as an argument and returns a tuple
containing length of each word of a string. For example, if the string is "Come let us have some
fun", the tuple will have (4, 3, 2, 4, 4, 3)

44. Function writing question9


Write a function INDEX_LIST(L), where L is the list of elements passed as argument to the
function. The function returns another list named ‘indexList’ that stores the indices of all Non-
Zero Elements of L. For example: If L contains [12,4,0,11,0,56]
The indexList will have - [0,1,3,5]

45. Function writing question10


Write a function AddZero(Scores) to add all those values in the list of Scores which are
ending with zero, and display the sum.
For example,
If the Scores contain[200,456,300,500,233,453]
The sum of scores should be displayed as 1000.

46. Function writing question11


Write a program that rotates the elements of a list so that the element at the first index moves to the second
index, the element in the second index moves to the third index ,etc., and the element in the last index
moves to the first index.

lis=eval(input("Enter list"))
last=lis[-1]
for i in range(len(lis)-1,0,-1):
lis[i]=lis[i-1]
lis[0]=last
print(lis)
47. Function writing question12
Write a function LShift(Arr,n) in Python, which accepts a list Arr of numbers and n is a numeric
value by which all elements of the list are shifted to left.
Sample Input Data of the list Arr= [ 10,20,30,40,12,11], n=2
Output
Arr = [30,40,12,11,10,20]

48. Function writing question13


Write a function bubble_sort (Ar, n) in python, Which accepts a list Ar of numbers and n is a
numeric value by which all elements of the list are sorted by Bubble sort Method

49. Function writing question14


Write code in Python to calculate and display the frequency of each item in a list
L=[10,12,14,17,10,12,15,24,27,24]
L1=[ ] L2=[ ]
for i in L:
if i not in L2:
c=L.count(i)
L1.append(c)
L2.append(i)
print('Item','\t\t','frequency')
for i in range(len(L1)):
print(L2[i],'\t \t', L1[i])
50 Function writing question15.
Write a program to input a date as an integer in the format MMDDYYYY. The program should
call a user-defined function to print out the date in the format , Example:
Input - 11272020
Output - November 27, 2020
51 Function writing question16.
Write a function in python named SwapHalfList(Array), which accepts a list Array of numbers
and swaps the elements of 1st Half of the list with the 2nd Half of the list ONLY if the sum of 1st
Half is greater than 2nd Half of the list.]
Sample Input Data of the list Array= [ 100, 200, 300, 40, 50, 60],
Output Arr = [40, 50, 60, 100, 200, 300]

51 Function writing question17.


Write a function listchange(Arr)in Python, which accepts a list Arr of numbers , the function will
replace the even number by value 10 and multiply odd number by 5 . Sample Input Data of the
list is:
a=[10,20,23,45]
listchange(a,4)
output : [10, 10, 115, 225]

52 Function writing question18.


Write a function in REP which accepts a list of integers and its size as arguments and replaces
elements having even values with its half and elements having odd values with twice its value .
eg: if the list contains 3, 4, 5, 16, 9 then
the function should rearranged list as 6, 2,10,8, 18

53 Function writing question19.


Take the two lists, and write a program that returns a list only the elements that are common
between both the lists (without duplicates) in ascending order. Make sure your program works on
two lists of different sizes.
e.g. L1= [1,1,2,3,5,8,13,21,34,55,89]
L2= [20,1,2,3,4,5,6,7,8,9,10,11,12,13]
The output should be: [1,2,3,5,8,13]
54 Function writing question20
Write definition of a method/function AddOddEven(VALUES) to display sum of odd and even
values separately from the list of VALUES.

55 Function writing question21


Write definition of a method/function HowMany(ID,Val) to count and display number of times
the value of Val is present in the list ID.

56 Function writing question21


Write a python method/function Swapper(Numbers) to swap the first half of the content of a
list Numbers with second half of the content of list Numbers and display the swapped values.
Note:
Assuming that the list has even number of values in it For Example: If the list Numbers contain
[35,67,89,23,12,45]
After swapping the list content should be displayed as
[23,12,45,35,67,89]

57 Function writing question22


Write a python method/function Count3and7(N) to find and display the count of all those
numbers which are between 1 and N, which are either divisible by 3 or by 7.
For Example: If the value of N is 15
The sum should be displayed as 7
58 Function writing question23
Write definition of a method ZeroEnding(SCORES) to add all those values in the list of SCORES,
which are ending with zero (0) and display the sum.
For example,
If the SCORES contain
[200,456,300,100,234,678]
The sum should be displayed as 600

59 Function writing question24


Write definition of a Method MSEARCH(STATES) to display all the state names from a list of
STATES, which are starting with alphabet M.
For example:
If the list STATES contains
["MP","UP","WB","TN","MH","MZ","DL","BH","RJ","HR"]
The following should get displayed
MP MH MZ

60. Function writing question25


Write definition of a method EvenSum(NUMBERS) to add those values in the list of NUMBERS,
which are odd.

61 Function writing question26


Write a method in python to display the elements of list twice, if it is a number and
display the element terminated with ‘*’ if it is not a number
For example,
if the content of list is as follows:
MyList=['RAMAN',’21’,'YOGRAJ', '3', 'TARA']
The output should be
RAMAN*
2121
YOGRAJ*
33
TARA*
62. Function writing question27
Write a method in python to find and display the prime numbers between 2 to N. Pass N as
argument to the method.

63. Function writing question28


Declare a dictionary D1 mapping each integer from 0 to 9 with their respective number
names. And also define a function NumName ( N) that find the number name of N, using the
dictionary D1
Example:
If the number passed to the function is 2951
, then the string to be returned is
“Two Nine Five One”

64. Function writing question29


Write a user-defined function frequencyCount() that accepts a dictionary with a list of
elements (keys) as arguments and returns the frequency of the element’s occurrence in the
form of dictionary.

65. Function writing question30.

Write a user-defined function with string as a parameter

which replaces all vowels in the string with '$'.


66. Function writing question31.

67. Function writing question32


Write a function SQUARE_LIST(L), where L is the list of elements passed as
argument to the function. The function returns another list named ‘SList’ that
stores the Squares of all Non-Zero Elements of L.

68.. Function writing question33


Write a function AdjustList(L), where L is a list of integers. The function
should reverse the contents of the list without slicing the list and without
using any second list.
Example:
If the list initially contains
2, 15, 3, 14, 7, 9, 19, 6, 1, 10,
then after reversal the list should contain
10, 1, 6, 19, 9, 7, 14, 3, 15, 2
69.. Function writing question34
Write a function EVEN_LIST(L), where L is the list of elements passed as
argument to the function.
The function returns another list named ‘evenList’ that stores the indices of
all even numbers of L.
For example:
If L contains [12,4,3,11,13,56]
The evenList will have - [12,4,5]
70.. Function writing question35
Write a function INDEX_LIST(S), where S is a string. The function
returns a list named ‘indexList’ that stores the indices of all vowels
of S.
For example:
If S is "Computer", then indexList should be [1,4,6]
71. Function writing question36

72. Function writing question37

A list of books is maintained in a library. The list consists of the


book titles.
Write a function that deletes a given book (title to be inout by the
user) from the list.

73. Function writing question38

Rohit is a python programmer. He wants to calculate square root


of n. so what module and function he must use to complete his
program.
74. Function writing question39

Write a function Interchange (num) in Python, which accepts a list num of


integers, and interchange the adjacent elements of the list and print the
modified list as shown below: (Number of elements in the list is assumed as
even)
Original List:
num = [5,7,9,11,13,15]
After Rearrangement
num = [7,5,11,9,15,13]
75. Function writing question40

Write a Recursive function in Python RecsumNat(N), to return the sum of the first N natural
numbers. For example, if N is 10 then the function should return (1 + 2 + 3 + ... + 10 = 55).

76. Function writing question41

Write a Recursive function in Python Power(X,N), to return the result of X raised to the power
N where X and N are non-negative integers. For example, if X is 5 and N is 3 then the function
should return the result of (5)3 i.e. 125.

77. Function writing question42

The function should calculate and return the average of the scores. Additionally, display
the scores that are above the calculated average.
For example, consider the following list of scores:
scores = [85, 92, 78, 95, 88]

78. Function writing question43

The function should return the average (rounded to two decimal places)
and print the scores that are above the calculated average. (Note: Do not
use in-built functions)
79. Function writing question44

Write a userdefined function parser(L) that accepts a list as parameter and creates
another two lists storing the numbers from the original list , that are even and
numbers that are odd

80. Function writing question45

Write a program to reverse a string using stack.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy