Programming and Problem Solving Through Python Language Theory Question
Modules
What is a module in Python ?
A built-in data type
A programming language
A file containing Python code
An external hardware component
Answer : C
Explain : Module is a file that contains a collection of related functions and other definitions. To use module we use import -module name.
How do you import a module named "math" in Python ?
import math
use math
include math
importing math
Answer : A
Explain : To use math we just use 'import module_name'.
What is the purpose of the 'from' keyword when importing a module?
It renames the module.
It imports all functions from the module.
It imports specific functions or attributes from the module.
It includes the module's documentation.
Answer : C
Explain : The from keyword is used to import only a specified section from a module.
What is the purpose of the 'as' keyword when importing a module?
It imports all functions from the module.
It renames the module.
It imports specific functions or attributes from the module.
It includes the module's documentation.
Answer : B
Explain : Like import math as hi - means hi is now name of math module.
Which module is used for handling dates and times in Python ?
time
datetime
date
clock
Answer : B
Explain : datetime module is use for both.
How can you install a third-party module in Python ?
It is automatically installed with Python.
Download and copy the module file to your project directory.
Use the 'pip' package manager.
Use the import statement.
Answer : C
Explain : We us pip to install a module. Like- pip install numpy means install numpy .
Which module is used for working with files and directories ?
fileio
os
math
directory
Answer : B
Explain : The OS module in Python provides functions for interacting with the operating system.
What is the purpose of the random module in Python ?
It generates random numbers and provides random-related functions.
It handles file I/O operations.
It performs mathematical operations.
It provides access to the system's operating system functions.
Answer : A
Explain : Python Random module is an in-built module of Python that is used to generate random numbers in Python.
What will be the output of the following Python code? from math import * floor(3.7)
3
4
3.0
None of these
Answer : A
Explain : floor () is used to round the number but not greater than given number.
What will be the output of the following ? import sys sys.stdout.write('Welcome\n') sys.stdout.write('All\n')
Welcome All
Welcome All
Compilation Error
Runtime Error
Answer : B
Explain : The python sys module provides functions and variables which are used to manipulate different parts of the Python Runtime Environment.
The syntax used to rename a file :
os.rename(existing_name, new_name)
fp.name = „new_name.txt‟
os.rename(fp, new_name)
os.set_name(existing_name, new_name)
Answer : A
Explain : OS module in Python provides functions for interacting with the operating system. rename() method is used to rename the name of file or directory. Syntax : os.rename(exist_file, new_name)
Which of the following is false about “from …. import ……” form of import ?
The syntax is: from modulename import identifier
This form of import does not import anything
The namespace of imported module becomes part of importing module
The identifiers in module are accessed directly as: identifier
Answer :
Explain :
What will be the output of the following Python code? from math import pow print(math.pow(2,3))
Nothing is printed
8
Error, method pow doesn‟t exist in math module
Error, the statement should be: print(pow(2, 3))
Answer : D
Explain : To print we don't need to use module name because we import pow from moudle.
Which module is to be imported for using randint( ) function ?
random
randrange
randomrange
rand
Answer : A
Explain : The "randint()" is a built-in function of the random module in Python.
Which statement is correct to import all modules from the package ?
from package import all
from package import *
from package include all
from package include *
Answer : B
Explain : Like we have to import datetime module use : from datetime import * - this import all datetime module.