Random Functions
Random Functions
Random Functions
1. What possible output(s) will be obtained when the following code is executed?
3. What are the possible output(s) of the following code? Also specify the maximum and minimum
values that can be assigned to variable x.
import random
m= [“cat”, “bat”, “mat”, “rat”, “ sat” , “pat”]
x= random.randomint((0,2) +2
for i in range(x+1):
print(m[i], end=” “)
a) bat mat b) cat bat mat rat c) cat bat d) rat sat
4.
5. What possible outputs(s) are expected to be displayed on screen at the time of execution of the
program from the following code?
import random
AR=[20,30,40,50,60,70];
FROM=random.randint(1,3)
TO=random.randint(2,4)
for K in range(FROM,TO+1):
print (AR[K],end="#")
a) 10#40#70# b) 30#40#50# c) 50#60#70# d)40#50#70#
6. Study the following program and select the possible output(s) and write maximum and
minimum value assigned to the variable y
import random
x=random.random( )
y=random.randint(0,4)
print(int(x),”:”,y+int(x))
(a) 0:0 (b) 1: 6 (c) 2:4 (d) 0:3
7. What possible outputs(s) are expected to be displayed on screen at the time of execution of the
program from the following code. Select which option/s is/are correct
import random
print(random.randint(15,25) , end=' ')
print((100) + random.randint(15,25) , end = ' ' )
print((100) -random.randint(15,25) , end = ' ' )
print((100) *random.randint(15,25) )
(i) 15 122 84 2500 (ii) 21 120 76 1500 (iii) 105 107 105 1800 (iv) 110 105 105 1900
8. What possible outputs are expected to be displayed on screen at the time of execution of the
program from the following code? Also specify the maximum value that can be assigned to each of
the variables L and U.
import random
Arr=[10,30,40,50,70,90,100]
L=random.randrange(1,3)
U=random.randrange(3,6)
for i in range(L,U+1):
print(Arr[i],"@",end="")
(i) 40 @50 @ (ii) 10 @50 @70 @90 @ (iii) 40 @50 @70 @90 @ (iv) 40 @100 @
9. What possible output(s) will be obtained when the following code is executed
import random
k=random.randint(1,3)
fruits=[‘mango’, ‘banana’, ‘grapes’, ‘water melon’, ‘papaya’]
for j in range(k):
print(fruits[j], end=“*”)
(a) mango*banana*grapes* (b) banana*grapes
(c) banana*grapes*watermelon (d) mango*grapes*papaya
10. Find the correct possible output(s):
Guess=65
for I in range(1,5):
New=Guess+random.randint(0,I)
print(chr(New),end=' ')
a) A B B C b) A C B A c) B C D A d) C A B D
FUNCTIONS
1. Write the output of the code given below:
def printMe(q,r=2):
p=r+q**3
print(p)
#main-code
a=10
b=5
printMe(a,b)
printMe(r=4,q=2)
printMe(5)
2. Find the output of the following:
def calcresult ():
i=9
while i>1 :
if(i%2 == 0):
x = i%2
i = i -1
else:
i=i-2
x=i
print(x**2)
calcresult()
3. Find the output of the following Python program:
def makenew (mystr):
newstr = “”
count = 0
for i in mystr:
if count%2 != 0:
newstr = newstr + str (count)
else:
if i.islower ():
newstr = newstr + i.upper ()
else:
newstr = newstr + i
count + = 1
newstr = newstr + mystr [:1]
print(“The new string is:”, newstr)
makenew(“sTUdeNT”)
a=10
b=5
sum(a,b)
sum(b)
sum(r=5,q=1)
8. 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 0 c) global b d)global a=100
10. Write a function, lenWords(STRING), that takes a string as an argument and returns a
tuple containing length of each word of a string.
a) 3 3 3 b) 3 4 5 c) 3 3 5 d) 3 5 5
13. 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
14. 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)
21.
22. The values being passed through a function call statement are called:
a) Actual parameter b) Formal parameter c) default parameter d) None of these
23. 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
3 MARKS
1. 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.
For example:
If L contains [9,4,0,11,0,6,0]
The SList will have - [81,16,121,36]
2. 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]
3.Write a function dispBook(BOOKS) in Python, that takes a dictionary BOOKS as an
argument and displays the names in uppercase of those books whose name starts with a
consonant.
For example, Consider the following dictionary
BOOKS = {1:"Python", 2:"Internet Fundamentals ", 3:"Networking ",
4:"Oracle sets", 5:"Understanding HTML"}
The output should be:
PYTHON
NETWORKING
4. Write a Python Program containing a function FindWord(STRING, SEARCH), that accepts
two arguments : STRING and SEARCH, and prints the count of occurrence of SEARCH in
STRING. Write appropriate statements to call the function.
For example,
if STRING = "Learning history helps to know about history with interest in history"
and SEARCH = 'history', the function should display
The word history occurs 3 times.
ERROR CORRECTION
1. Rao has written a code to input a number and check whether it is prime or not. His
code is having errors. Rewrite the correct code and underline the corrections made.
def prime():
n=int(input("Enter number to check :: ")
for i in range (2, n//2):
if n%i=0:
print("Number is not prime \n")
break
else:
print("Number is prime \n’)
2. Rewrite the following code in Python after removing all the syntax errors. Underline
each correction done in the code.
num1, num2 = 10, 45
While num1 % num2 == 0
num1+= 20
num2+= 30
Else:
print('hello')
3. The code given below accepts a number as an argument and returns the reverse
number. Observe the following code carefully and rewrite it after removing all syntax and
logical errors. Underline all the corrections made.