Python Programming Quiz – 7th Week Quiz Solution

12
5217
Python Programming Quiz – 7th Week Quiz Solution
Python Programming Quiz – 7th Week Quiz Solution

Disclaimer for ReGyan

If you require any more information or you have any problem regarding Copyright or have any questions about our site’s disclaimer, please feel free to contact us by email at hello@regyan.com.

Disclaimers for ReGyan

All the information on this website is published in good faith and for general information and educational purpose only. ReGyan does not make any warranties about the completeness, reliability, and accuracy of this information. Any action you take upon the information you find on this website (regyan.com), is strictly at your own risk. will not be liable for any losses and/or damages in connection with the use of our website.




[1] What should be the name of the python file that defines a module foo?

  • (a): module.py
  • (b): foo_module.py
  • (c): module_foo.py
  • (d): foo.py

Answer: (d) foo.py

[2]  The following code is saved in a file mod.py (using python 3.X version):

def printHi():
print (“Hi”)

Now consider the following interaction on Python console, assuming mod.py
is in the current directory where the console is invoked:

>>> import mod
>>> printHi()
Traceback
(most recent call last): File “”, line 1, in
NameError: name ‘printHi’ is not defined

What is the reason for the NameError error?

  • (a): printHi is a keyword in Python
  • (b): printHi is not a valid function name
  • (c): We need to qualify function name with module name, as mod.printHi()
  • (d): import does not import files in the current directory




Answer: (c) We need to qualify function name with module name, as mod.printHi()

[3] The following code is saved in a file mod.py (using python 3.X version):

def printHi():
print (“Hi”)

Now consider the following interaction on Python console, assuming
mod.py is in the current directory where the console is invoked:

>>> import mod
>>> print (mod.__name__)

What will be the output on the console?

  • (a): Hi
  • (b): mod.Hi
  • (c): mod
  • (d): Error since __name__ is not defined

Answer: (c) mod




[4] True or False: It is possible to import more than one modules in the same program?

  • (a): True
  • (b): False

Answer: (a) True

[5] Suppose we have a python module as file mdl.py defining four functions f1, f2, f3 and f4. Now we give following python command at the python shell:

>>> from mdl import f2

Which of the following functions from mdl.py can now be invoked from calling script –

  • (a): f1
  • (b): f2
  • (c): f3
  • (d): f4

Answer: (b) f2

[6] A python module file mdl.py can be used as a script as well as an importable module by adding which of the following code at the end of the file mdl.py:

(A) if _ main _ == “ _ name _ “ :
… # suitable code
(B) if _ main _ == “ _ mdl _ “ :
… # suitable code
(C) if _ name _ == “ _ main _ “ :
… # suitable code
(D) if _ name _ == “ _ mdl _ “ :
… # suitable code

  • (a): A
  • (b): B
  • (c): C
  • (d): D




Answer: (c) C

[7]  Suppose we have a python module as file mdl.py defining three functions f1, f2 and f3. Now we give the following python command at the python shell:

>>> from mdl import *

Which of the following functions from mdl.py can be invoked from calling script –

  • (a): only f1 and f3
  • (b): only f2 and f3
  • (c): only f3 and f1
  • (d): all of f1, f2 and f3

Answer: (d) all of f1, f2 and f3

[8]  Suppose a python module file mdl.py contains the required code at the end of the file mdl.py so that it can be used as a script as well as an importable module. Now we can run script mdl.py by the following command at python shell:

>>> import mdl

  • (a): True
  • (b): False

Answer:(b) False

[9] Class construct in python contains:

  • (a): data as attributes of objects
  • (b): functions as actions on data
  • (c): both (a) and (b)
  • (d): either (a) or (b)

Answer:(c) both (a) and (b)

Also see:  LIC Scholarship 2019 Application Form : Apply Online

[10]  In python, class is itself an object of type: type.

  • (a) True
  • (b): False

Answer: (a) True

[11]  What is the output of the following python code?

class hello:
def __init__(self,a=”Visit Prutor.ai website”):
self.a=a

def display(self):
print(self.a)
obj=hello()
obj.display()

  • (a): The program has an error because constructor can’t have default arguments
  • (b): Nothing is displayed
  • (c): “Visit Prutor.ai website” is displayed
  • (d): The program has an error because display function doesn’t have parameters




Answer: (c) “Visit Prutor.ai website” is displayed

[12] What is the output of the following python code?

class test:
def __init__(self,x):
self.x=x

def display(self):
print(self.x)
obj=test()
obj.display()

  • (a): Executes normally and doesn’t display anything
  • (b): Displays 0, which is the automatic default value
  • (c): Error as at least one argument is required while creating an object
  • (d): Error as display function requires additional argument

Answer: (c) Error as at least one argument is required while creating an object

[13] What does Instantiation mean in terms of Object Oriented Programming?

  • (a): Deleting an instance of class
  • (b): Creating an instance of class
  • (c): Copying an instance of class
  • (d): Modifying an instance of class

Answer: (b) Creating an instance of class

[14] Select the correct alternative for special method __init__ in python –

  • (a): Is used to create a new object for a class
  • (b): Only one constructor is allowed per class
  • (c): Uses default arguments
  • (d): All of the above




Answer: (d) All of the above

[15] The return value of special method __str__ can be an object of any type in python.

  • (a): True
  • (b): False

Answer: (b) False

That’s all for the  Python programming Quiz – 7th Week Quiz Solution. if you have any questions, please comment down below, we will try to answer within the first 24 hours. I hope you liked this content. We will come to another week’s quiz. Thanks for reading.

12 COMMENTS

  1. Which of the following is true:-
    a. Python modules can be renamed when importing
    b. To rename a module we use ‘to’
    c. Both (a) and (b)
    d. None of the above
    please send me correct answers

  2. What is the following is true:-

    (a) python modules can be renamed when importing
    (b) to rename a module we use ‘to’
    (c) both a and b
    (d) none of the above

  3. Suppose a file named test.txt has content 1 2 3 4 5 6 7 in it.Now what will be the output of the following code :-
    file = open(“test.txt”,’r’)
    file.read(5)

LEAVE A REPLY

Please enter your comment!
Please enter your name here