0% found this document useful (0 votes)
749 views6 pages

Chapter 4 - Type B - Application Based Questions

This document discusses Python libraries and application-based questions. It includes 10 questions about importing modules and functions, using random number generation, and summarizing code outputs. Key topics covered are importing modules using import and from, calling functions from imported modules, random number ranges generated by random.randint() and random.random(), and analyzing code snippets to determine possible outputs and minimum/maximum values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
749 views6 pages

Chapter 4 - Type B - Application Based Questions

This document discusses Python libraries and application-based questions. It includes 10 questions about importing modules and functions, using random number generation, and summarizing code outputs. Key topics covered are importing modules using import and from, calling functions from imported modules, random number ranges generated by random.randint() and random.random(), and analyzing code snippets to determine possible outputs and minimum/maximum values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CHAPTER 4

USING PYTHON LIBRARIES


TYPE B - APPLICATION BASED QUESTIONS

1. Create module tmpConvenion.py as given in Fig 4.2 in the chapter.

If you invoke the module with two different types of import statement, how would the function call
statement for imported module’s functions be affected?

Answer
If we use ‘import’ command then we will able to call all function which is present in file ‘tmpConvenion.py’
For example:
import tmpConvenion

If we use ‘from import’ command then we able to call selected function


For example:-
from tmpConvenion import to_centigrade()

2. A function checkMain() defined in module Allchecks.py is being used in two different programs. In
program 1 as
Allchecks.checkMain(3, ‘A’)
and in program 2 as
checkMain(4, ‘Z’)
Why are these two-function call statement different from one another when the function being invoked
is just the same?
Answer
Program 1 give result like
import Allchecks
Allchecks.checkMain(3, ‘A’)
While Program 2 call checkMain(4, ‘Z’) after running the module Allchecks.py

3. Given below is semi-complete code of module basic.py: Complete the code. Save as a module
# """___________"""
def square (x):
"""_________"""
return mul(x, x):
___mul(x,y) :"""___________"""
return x*y
def div(x, y) :
"""________"""

1
return float(x)/y
____def fdiv(x, y)____
"""_______"""
________x//y
def floordiv(x, y)____
_______fdiv(x,y)

Answer

4. After importing the above module some of its functions are executed as per following statements.
Find errors, if any

a) square(print 3) - Error in syntax:

b) basic.div() - Error because there is no argument.

c) basic.floordiv(7.0,7) - There is no error found.

d) div(100, 0) - Error in syntax Correct way is: basic.div(100, 0)

e) basic.mul(3,5) - There is no error found.

f) print (basic.square(3.5)) - There is no error found.

g) z = basic.div(13,3) - There is no error found.

2
5. Import the above module basics.py and write statements for the following:

(a) Compute square of 19.23


>>>basic.square(19.23)
>>>369.79290000000003

(b) Compute floor division of 1000.01 with 100.23


>>>basic.floordiv(1000.01 ,100.23 )
>>>9.0

(c) Compute product of 3, 4 and 5 (Hint: use a function multiple times)


>>> file.mul(3,4)
12
>>> file.mul(12,5)
60

(d) What is the difference between basic.div(100,0 ) and basic.div(0, 100)?

basics.div(100,0 ) it will give error (i.e., ZeroDivisionError)


While
basics.div(0, 100) it will give output i.e., is 0.

6. Suppose that after we import the random module, we define the following function called diff in a
Python session:
import random
def diff():
x = random.random() - random.random()
return(x)
What would be the result you now evaluate?
y = diff ()
print(y)
At the Python prompt? Give reasons for your answer.
Answer
It will give output subtracted number which is generated by random() function. Because random() function
generate number between 0.0 <= N < 1.0

3
7. What are the possible outcome(s) executed from the following code? Also specify the maximum and
minimum values that can be assigned to variable NUMBER.
Import random
STRING="CBSEONLINE"
NUMBER = random.randint (0, 3)
N=9
while STRING[N] != "L":
print (STRING[N] + STRING[NUMBER] + "#", end = " ")
NUMBER = NUMBER +1
N=N-1
(i) ES#NE#IO# (ii)LE#NO#ON# (iii)NS#IE#LO# (iv)EC#NB#IS#
Answer
Outcomes are (i)Maximum value of NUMBER is 6.
Minimum value of NUMBER is 3.Because Minimum value of NUMBER from random.randint (0, 3) is 0 and
while loop will run only 3 times. So, Minimum value of NUMBER is 3.Maximum value of NUMBER from
random.randint (0, 3) is 3 and while loop will run only 3 times. So, Maximum value NUMBER is 6.
Output of Code :-
EC# NB# IS# 3
EB# NS# IE# 4
ES# NE# IO# 5
EE# NO# IN# 6

8. Consider the following code:


import random
print (int( 20 + random.random()*5), end =" ")
print (int( 20+ random.random()*5), end =" ")
print (int(20 + random.random()*5), end = " ")
print (int( 20 + random.random()*5))
Find the suggested output options (i) to (iv). Also, write the least value and highest value that can be
generated.
(i) 20 22 24 25
(ii) 22 23 24 25
(iii) 23 24 23 24
(iv) 21 21 21 21
4
Answer
Option (iii) & (iv) will generate only.
Highest value is 24
Least value is 20

9. Consider the following code:


import random
print (100 + random.randint(5, 10), end = ' ')
print (100 + random.randint(5, 10), end = ' ')
print (100 + random.randint(5, 10), end = ' ')
print (100 + random.randint(5, 10))
Find the suggested output options (i) to (iv). Also, write the least value and highest value that can be
generated.
(i) 102 105 104 105
(ii) 110 103 104 105
(iii) 105 107 105 110 (iv) 110 105 105 110

Answer
Option (iii) & (iv) will generate only.
Highest value is 110
Least value is 105

10. Consider the following package

Each of the above modules contain functions play(), writefile() and readfile().

5
(a) If the module wavwrite() is imported using command import music.formats.wavwrite. How will you
invoke its writefile() function? Write command for it.

(b) If the module wavwrite() is imported using command from music.formats import wavwrite. How will
you invoke its writefile() function? Write command for it.

Answer
(a)import music.formats.wavwrite
music.formats.wavwrite.writefile()

(b)from music.formats import wavwrite


wavwrite.writefile()
11. What are the possible outcome(s) executed from the following code ? Also specify the maximum and
minimum values that can be assigned to variable PICKER.

import random
PICK = random.randint (0, 3)
CITY = ["DELHI", "MUMBAI", "CHENNAI", "KOLKATA"]
for I in CITY :
for j in range(1, PICK):
print(I, end =" ")
print()

Answer
Option (i) & (iii) will generate only.
Highest value is 3
Least value is 0

You might also like