Programming and Problem Solving Through Python Language Theory Question

Modules

  1. What is a module in Python ?
    1. A built-in data type
    2. A programming language
    3. A file containing Python code
    4. 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.
  2. How do you import a module named "math" in Python ?
    1. import math
    2. use math
    3. include math
    4. importing math
    Answer : A
    Explain : To use math we just use 'import module_name'.
  3. What is the purpose of the 'from' keyword when importing a module?
    1. It renames the module.
    2. It imports all functions from the module.
    3. It imports specific functions or attributes from the module.
    4. It includes the module's documentation.
    Answer : C
    Explain : The from keyword is used to import only a specified section from a module.
  4. What is the purpose of the 'as' keyword when importing a module?
    1. It imports all functions from the module.
    2. It renames the module.
    3. It imports specific functions or attributes from the module.
    4. It includes the module's documentation.
    Answer : B
    Explain : Like import math as hi - means hi is now name of math module.
  5. Which module is used for handling dates and times in Python ?
    1. time
    2. datetime
    3. date
    4. clock
    Answer : B
    Explain : datetime module is use for both.
  6. How can you install a third-party module in Python ?
    1. It is automatically installed with Python.
    2. Download and copy the module file to your project directory.
    3. Use the 'pip' package manager.
    4. Use the import statement.
    Answer : C
    Explain : We us pip to install a module. Like- pip install numpy means install numpy .
  7. Which module is used for working with files and directories ?
    1. fileio
    2. os
    3. math
    4. directory
    Answer : B
    Explain : The OS module in Python provides functions for interacting with the operating system.
  8. What is the purpose of the random module in Python ?
    1. It generates random numbers and provides random-related functions.
    2. It handles file I/O operations.
    3. It performs mathematical operations.
    4. 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.
  9. What will be the output of the following Python code?
    from math import *
    floor(3.7)
    1. 3
    2. 4
    3. 3.0
    4. None of these
    Answer : A
    Explain : floor () is used to round the number but not greater than given number.
  10. What will be the output of the following ?
    import sys
    sys.stdout.write('Welcome\n')
    sys.stdout.write('All\n')
    1. Welcome All
    2. Welcome
      All
    3. Compilation Error
    4. 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.
  11. The syntax used to rename a file :
    1. os.rename(existing_name, new_name)
    2. fp.name = „new_name.txt‟
    3. os.rename(fp, new_name)
    4. 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)
  12. Which of the following is false about “from …. import ……” form of import ?
    1. The syntax is: from modulename import identifier
    2. This form of import does not import anything
    3. The namespace of imported module becomes part of importing module
    4. The identifiers in module are accessed directly as: identifier
    Answer :
    Explain :
  13. What will be the output of the following Python code?
    from math import pow
    print(math.pow(2,3))
    1. Nothing is printed
    2. 8
    3. Error, method pow doesn‟t exist in math module
    4. 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.
  14. Which module is to be imported for using randint( ) function ?
    1. random
    2. randrange
    3. randomrange
    4. rand
    Answer : A
    Explain : The "randint()" is a built-in function of the random module in Python.
  15. Which statement is correct to import all modules from the package ?
    1. from package import all
    2. from package import *
    3. from package include all
    4. from package include *
    Answer : B
    Explain : Like we have to import datetime module use : from datetime import * - this import all datetime module.

Next Set

2