Random Functions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

RANDOM FUNCTIONS

1. What possible output(s) will be obtained when the following code is executed?

a. RED* b. YELLOW* c. WHITE*WHITE* d. YELLOW*


WHITE* WHITE* YELLOW*YELLOW* WHITE*WHITE*
BLACK* BLACK* BLACK*BLACK* BLACK*BLACK*BLACK*
RED* RED* RED*RED* RED*RED*RED*RED*

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


import random
List=["Delhi", "Mumbai", "Chennai", "Kolkata"]
for y in range(4):
x = random.randint(1,3)
print(List[x],end="#")
a. Delhi#Mumbai#Chennai#Kolkata# b. Mumbai#Chennai#Kolkata#Mumbai#
c. Mumbai# Mumbai #Mumbai # Delhi# d. Mumbai# Mumbai #Chennai # Mumbai#

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

11. Write the possible outputs(s) when this code is executed?


import random
n=random.randint(0,3)
color=[“Y”, ”W” , ”B”, ”R”]
for i in range (1,n):
print(color[i], end=”*”)
print( )
a) R * b) W*
W* B*
B*
c) W* W* d) Y*
B* B* W* W*
B* B* B*
12. What possible outputs(s) will be obtained when the following code is executed?
from random import randint
Vibgyor=[ ['V','Violet'],['I','Indigo'],['B','Blue'],['G','Green'],['Y','Yellow'],
['O','Orange'],['R','Red'] ]
for i in range(3):
first=randint(0,1)
last=randint(1,2)+1
print(Vibgyor[last-first], end= ':')
a) ['G','Green'] : ['G','Green'] : ['Y','Yellow']: b) ['G','Green'] : ['B','Blue'] : ['G','Green']:
c) ['V','Violet'] : ['B','Blue'] : ['B','Blue']: d) ['I','Indgo'] : ['B','Blue'] : ['B','Blue']:
13. Find the correct possible output(s):
i. 29: 26:25 :28 : ii. 29: 26:24 :28 : iii) 24: 28:25:26: iv. 29: 26:25:26:

14. Find the correct possible output(s):


Area=["NORTH","SOUTH","EAST","WEST"]
for I in range(3):
ToGo=random.randint(0,1) + 1
print(Area[ToGo],end=":")
print()
a) SOUTH : EAST : SOUTH : b) NORTH : SOUTH : EAST :
c) SOUTH : EAST : WEST : d) SOUTH : EAST : EAST :

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”)

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


value = 50
def display(N):
global value
value = 25
if N%7==0
value = value + N
else:
value = value — N
print(value, end= “#”)
display(20)
print(value)
5. Find the output of the following code :
Name= “PythoN3@1”
R=“ ”
for x in range(len(Name)):
if Name[x].isupper():
R=R+Name[x].lower()
elif Name[x].islower():
R=R+Name[x].upper()
elif Name[x].isdigit():
R=R+Name[x–1]
else:
R=R+ “#”
print(R)

6. Predict the output of the Python code given below:


def Diff(N1,N2):
if N1>N2:
return N1-N2
else:
return N2-N1
NUM= [10,23,14,54,32]
for CNT in range (4,0,-1):
A=NUM[CNT]
B=NUM[CNT-1]
print(Diff(A,B),'#', end=' ')
7. Write the output of the code given below:
p=5
def sum(q,r=2):
global p
p=r+q**2
print(p, end= '#')

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

9. 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:"NewYork",5:"Doha"}
The output should be:
LONDON
NEW YORK

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.

For example, if the string is "Come let us have some fun",


The tuple will have (4, 3, 2, 4, 4, 3)

11. Predict the output of the following code:

def Cahange( P, Q=10) :


P = P /Q
Q= P % Q
return P
A= 200
B= 20
A = Change( A, B)
print (A , B, sep =’S’)
B= Change( B)
print( A, B, sep =’S’, end=”###”)
12. What will be the output of the following code?
x=3
def myfunc():
global x
x+=2
print(x, end=' ')
print(x, end=' ')
myfunc()
print(x, end=' ')

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)

15. 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()

16. What will be the output of the following Python code?


def add (num1, num2):
sum = num1 + num2
sum = add(20,30)
print(sum)
a. 50 b. 0 c. Null d. None
17. What will be the output of the following code?
def my_func(var1=100, var2=200):
var1+=10
var2 = var2 - 10
return var1+var2
print( my_func(50), my_func())

a. 100 200 b. 150 300 c. 250 75 d. 250 300

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


def ChangeVal(M,N):
for i in range(N):
if M[i]%5 == 0:
M[i]//=5
if M[i]%3 == 0:
M[i]//=3
L = [25,8,75,12]
ChangeVal(L,4)
for i in L:
print(i,end="#")
a) 5#8#15#4# b) 5#8#5#4# c) 5#8#15#14# d) 5#18#15#4#
19. Predict the output of the Python code given below:

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


L = [5,10,15,1]
G=4
def Change(X):
global G
N=len(X)
for i in range(N):
X[i] += G
Change(L)
for i in L:
print(i,end='$')

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.

You might also like