Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
nmims post
Objective Type Set
Online MCQ Assignment
Question Solution
Solved Question
Uncategorized

Python MCQ Set 1

1. Which of these definitions correctly describes a module?
a) Denoted by triple quotes for providing the specification of certain program elements
b) Design and implementation of specific functionality to be incorporated into a program
c) Defines the specification of how it is to be used
d) Any program that reuses code

View Answer

Answer: b [Reason:] The term “module” refers to the implementation of specific functionality to be incorporated into a program.

2. Which of the following is not an advantage of using modules?
a) Provides a means of reuse of program code
b) Provides a means of diving up tasks
c) Provides a means of reducing the size of the program
d) Provides a means of testing individual parts of the program

View Answer

Answer: c [Reason:] The total size of the program remains the same regardless of whether modules are used or not. Modules simply divide the program.

3. Program code making use of a given module is called a ______ of the module.
a) Client
b) Docstring
c) Interface
d) Modularity

View Answer

Answer: a [Reason:] Program code making use of a given module is called the client of the module. There may be multiple clients for a module.

4. ______ is a string literal denoted by triple quotes for providing the specifications of certain program elements.
a) Interface
b) Modularity
c) Client
d) Docstring

View Answer

Answer: d [Reason:] Docstring used for providing the specifications of program elements.

5. Which of the following is true about top-down design process?
a) The details of a program design are addressed before the overall design
b) Only the details of the program are addressed
c) The overall design of the program is addressed before the details
d) Only the design of the program is addressed

View Answer

Answer: c [Reason:] Top-down design is an approach for deriving a modular design in which the overall design.

6. In top-down design every module is broken into same number of submodules? True or False?
a) True
b) False

View Answer

Answer: b [Reason:] In top-down design every module can even be broken down into different number of submodules.

7. All modular designs are because of a top-down design process? True or False?
a) True
b) False

View Answer

Answer: b [Reason:] The details of the program can be addressed before the overall design too. Hence, all modular designs are not because of a top-down design process.

8. What is the output of the following piece of code?

#mod1
def change(a):
    b=[x*2 for x in a]
    print(b)
#mod2
def change(a):
    b=[x*x for x in a]
    print(b)
from mod1 import change
from mod2 import change
#main
s=[1,2,3]
change(s)

a) [2,4,6].
b) [1,4,9].
c) [2,4,6].
[1,4,9].
d) There is a name clash

View Answer

Answer: d [Reason:] A name clash is when two different entities with the same identifier become part of the same scope. Since both the modules have the same function name, there is a name clash.

9. Which of the following isn’t true about main modules?
a) When a python file is directly executed, it is considered main module of a program
b) Main modules may import any number of modules
c) Special name given to main modules is: __main__
d) Other main modules can import main modules

View Answer

Answer: d [Reason:] Main modules are not meant to be imported into other modules.

10. Which of the following is not a valid namespace?
a) Global namespace
b) Public namespace
c) Built-in namespace
d) Local namespace

View Answer

Answer: b [Reason:] During a Python program’s execution, there are as many as three namespaces – built-in namespace, global namespace and local namespace.

11. Which of the following is false about “import modulename” form of import?
a) The namespace of imported module becomes part of importing module
b) This form of import prevents name clash
c) The namespace of imported module becomes available to importing module
d) The identifiers in module are accessed as: modulename.identifier

View Answer

Answer: a [Reason:] In the “import modulename” form of import, the namespace of imported module becomes available to, but not part of, the importing module.

12. Which of the following is false about “from-import” form of import?
a) The syntax is: from modulename import identifier
b) This form of import prevents name clash
c) The namespace of imported module becomes part of importing module
d) The identifiers in module are accessed directly as: identifier

View Answer

Answer: b [Reason:] In the “from-import” form of import, there may be name clashes because names of the imported identifiers aren’t specified along with the module name.

13. Which of the statements about modules is false?
a) In the “from-import” form of import, identifiers beginning with two underscores are private and aren’t imported
b) dir() built-in function monitors the items in the namespace of the main module
c) In the “from-import” form of import, all identifiers regardless of whether they are private or public are imported
d) When a module is loaded, a compiled version of the module with file extension .pyc is automatically produced

View Answer

Answer: c [Reason:] In the “from-import” form of import, identifiers beginning with two underscores are private and aren’t imported.

14. What is the output of the following piece of code?

from math import factorial
print(math.factorial(5))

a) 120
b) Nothing is printed
c) Error, method factorial doesn’t exist in math module
d) Error, the statement should be: print(factorial(5))

View Answer

Answer: d [Reason:] In the “from-import” form of import, the imported identifiers (in this case factorial()) aren’t specified along with the module name.

15. What is the order of namespaces in which Python looks for an identifier?
a) Python first searches the global namespace, then the local namespace and finally the built-in namespace
b) Python first searches the local namespace, then the global namespace and finally the built-in namespace
c) Python first searches the built-in namespace, then the global namespace and finally the local namespace
d) Python first searches the built-in namespace, then the local namespace and finally the global namespace

View Answer

Answer: b [Reason:] Python first searches for the local, then the global and finally the built-in namespace.

Python MCQ Set 2

1. What is the output of print 0.1 + 0.2 == 0.3?
a) True
b) False
c) Machine dependent
d) Error

View Answer

Answer: b [Reason:] Neither of 0.1, 0.2 and 0.3 can be represented accurately in binary. The round off errors from 0.1 and 0.2 accumulate and hence there is a difference of 5.5511e-17 between (0.1 + 0.2) and 0.3.

2. Which of the following is not a complex number?
a) k = 2 + 3j
b) k = complex(2, 3)
c) k = 2 + 3l
d) k = 2 + 3J

View Answer

Answer: c [Reason:] l (or L) stands for long.

3. What is the type of inf?
a) Boolean
b) Integer
c) Float
d) Complex

View Answer

Answer: c [Reason:] Infinity is a special case of floating point numbers. It can be obtained by float(‘inf’).

4. What does ~4 evaluate to?
a) -5
b) -4
c) -3
d) +3

View Answer

Answer: a [Reason:] ~x is equivalent to -(x+1).

5. What does ~~~~~~5 evaluate to?
a) +5
b) -11
c) +11
d) -5

View Answer

Answer: a [Reason:] ~x is equivalent to -(x+1).

6. Which of the following is incorrect?
a) x = 0b101
b) x = 0x4f5
c) x = 19023
d) x = 03964

View Answer

Answer: d [Reason:] Numbers starting with a 0 are octal numbers but 9 isn’t allowed in octal numbers.

7. What is the result of cmp(3, 1)?
a) 1
b) 0
c) True
d) False

View Answer

Answer: a [Reason:] cmp(x, y) returns 1 if x > y, 0 if x == y and -1 if x < y.

8. Which of the following is incorrect?
a) float(‘inf’)
b) float(‘nan’)
c) float(’56’+’78’)
d) float(’12+34′)

View Answer

Answer: d [Reason:] ‘+’ cannot be converted to a float.

9. What is the result of round(0.5) – round(-0.5)?
a) 1.0
b) 2.0
c) 0.0
d) None of the mentioned

View Answer

Answer: b [Reason:] Python rounds off numbers away from 0 when the number to be rounded off is exactly halfway through. round(0.5) is 1 and round(-0.5) is -1.

10. What does 3 ^ 4 evaluate to?
a) 81
b) 12
c) 0.75
d) 7

View Answer

Answer: d [Reason:] ^ is the Binary XOR operator.

Python MCQ Set 3

1. The process of pickling in Python includes:
a) conversion of a list into a datatable
b) conversion of a byte stream into Python object hierarchy
c) conversion of a Python object hierarchy into byte stream
d) conversion of a datatable into a list

View Answer

Answer: c [Reason:] Pickling is the process of sterilizing a Python object, that is, conversion of a byte stream into Python object hierarchy. The reverse of this process is known as unpickling.

2. To sterilize an object hierarchy, the _____________ function must be called.
To desterilize a data stream, the ______________ function must be called.
a) dumps(), undumps()
b) loads(), unloads()
c) loads(), dumps()
d) dumps(), loads()

View Answer

Answer: d [Reason:] To sterilize an object hierarchy, the dumps() function must be called. To desterilize a data steam, the loads function must be called.

3. Pick the correct statement regarding pickle and marshal modules.
a) The pickle module supports primarily .pyc files whereas marshal module is used to sterilize Python objects
b) The pickle module keeps track of the objects that have already been sterilized whereas the marshal module does not do this
c) The pickle module cannot be used to sterilize user defined classes and their instances whereas marshal module can be used to perform this task
d) The format of sterilization of the pickle module is not guaranteed to be supported across all versions of Python. The marshal module sterilization is compatible across all the versions of Python

View Answer

Answer: b [Reason:] The correct statement from among the above options is that the python module keeps track of the objects that have already been sterilized whereas the marshal module does not do this. The rest of the statements are incorrect.

4. What is the output of the line of code shown below?
pickle.HIGHEST_PROTOCOL
a) 4
b) 5
c) 3
d) 6

View Answer

Answer: a [Reason:] There are five protocol versions available of the pickle module, namely, 0, 1, 2, 3 and 4. In the code shown above, the highest protocol version is returned, that is, 4.

5. Which of the codes shown below will result in an error?
Given that:

object = ‘a’

a) >>> pickle.dumps(object)
b) >>> pickle.dumps(object, 3)
c) >>> pickle.dumps(object, 3, True)
d) >>> pickle.dumps(‘a’, 2)

View Answer

Answer: c [Reason:] The function pickle.dumps requires either 1 or 2 arguments. If any other number of arguments are passed to it, it results in an error. An error is thrown even when no arguments are passed to it.

6. Which of the following functions can ve used to find the protocol version of the pickle module currently being used?
a) pickle.DEFAULT
b) pickle.CURRENT
c) pickle.CURRENT_PROTOCOL
d) pricle.DEFAULT_PROTOCOL

View Answer

Answer: d [Reason:] The function pickle.DEFAULT_PROTOCOL can be used to find the protocol version of the pickle module currently being used by the system.

7. The output of the two codes shown below is exactly the same. State whether true or false.

object
'a'
CODE 1
>>> pickle.dumps('a', 3)
CODE 2
>>> pickle.dumps(object, 3)

a) True
b) False

View Answer

Answer: a [Reason:] The two codes shown above result in the same output, that is, b’x80x03Xx01x00x00x00aqx00.’ Hence this statement is true.

8. Which of the following functions can accept more than one positional argument?
a) pickle.dumps
b) pickle.loads
c) pickle.dump
d) pickle.load

View Answer

Answer: a [Reason:] The functions pickle.loads, pickle.dump and pickle.load accept only one argument. The function pickle.dumps can accept more than one argument.

9. Which of the following functions raises an error when an unpicklable object is encountered by Pickler?
a) pickle.PickleError
b) pickle.PicklingError
c) pickle.UnpickleError
d) pickle.UnpicklingError

View Answer

Answer: b [Reason:] The function pickle.PicklingError raises an error when an unpickable object is encountered by Pickler.

10. The pickle module defines ______ exceptions and exports _______ classes.
a) 2, 3
b) 3, 4
c) 3, 2
d) 4, 3

View Answer

Answer: c [Reason:] The pickle module defines three exceptions, namely, pickle.PickleError, pickle.PicklingError, pickle.UnpickleError and exports two classes, namely, pickle.Pickler and pickle.Unpickler.

11. Which of the following cannot be pickled?
a) Functions which are defined at the top level of a module with lambda
b) Functions which are defined at the top level of a module with def
c) Built-in functions which are defined at the top level of a module
d) Classes which are defined at the top level of a module

View Answer

Answer: a [Reason:] Functions which are defined at the top level of a module with lambda cannot be pickled.

12. If __getstate__() returns _______________ the __setstate__() module will not be called on pickling.
a) True value
b) False value
c) ValueError
d) OverflowError

View Answer

Answer: b [Reason:] If getstate__() returns a false value, the __setstate__() module will not be called on pickling.

13. Lambda functions cannot be pickled because:
a) Lambda functions only deal with binary values, that is, 0 and 1
b) Lambda functions cannot be called directly
c) Lambda functions cannot be identified by the functions of the pickle module
d) All lambda functions have the same name, that is, <lambda>

View Answer

Answer: d [Reason:] Lambda functions cannot be pickled because all the lambda functions have the same name, that is, <lambda>

14. The module _______________ is a comparatively faster implementation of the pickle module.
a) cPickle
b) nPickle
c) gPickle
d) tPickle

View Answer

Answer: a [Reason:] The module cPickle is a comparatively faster implementation of the pickle module.

15. The copy module uses the ___________________ protocol for shallow and deep copy.
a) pickle
b) marshal
c) shelve
d) copyreg

View Answer

Answer: a [Reason:] The copy module uses the pickle protocol for shallow and deep copy.

Python MCQ Set 4

1. Which of the following best describes polymorphism?
a) Ability of a class to derive members of another class as a part of its own definition
b) Means of bundling instance variables and methods in order to restrict access to certain class members
c) Focuses on variables and passing of variables to functions
d) Allows for objects of different types and behaviour to be treated as the same general type

View Answer

Answer: d [Reason:] Polymorphism is a feature of object-oriented programming languages. It allows for the implementation of elegant software that is well designed and easily modified.

2. What is the biggest reason for the use of polymorphism?
a) It allows the programmer to think at a more abstract level
b) There is less program code to write
c) The program will have a more elegant design, and will be easier to maintain and update
d) Program code takes up lesser space

View Answer

Answer: c [Reason:] Polymorphism allows for the implementation of elegant software.

3. What is the use of duck typing?
a) More restriction on the type values that can be passed to a given method
b) No restriction on the type values that can be passed to a given method
c) Less restriction on the type values that can be passed to a given method
d) Makes the program code smaller

View Answer

Answer: c [Reason:] In Python, any set of classes with a common set of methods can be treated similarly. This is called duck typing. Hence duck typing imposes lesser restrictions.

4. What is the output of the following piece of code?

class A:
    def __str__(self):
        return '1'
class B(A):
    def __init__(self):
        super().__init__()
class C(B):
    def __init__(self):
        super().__init__()
def main():
    obj1 = B()
    obj2 = A()
    obj3 = C()
    print(obj1, obj2,obj3)
main()

a) 1 1 1
b) 1 2 3
c) ‘1’ ‘1’ ‘1’
d) An exception is thrown

View Answer

Answer: a [Reason:] The super().__init__() in the subclasses has been properly invoked and none of other subclasses return any other value. Hence 1 is returned each time the object is created and printed.

5. What is the output of the following piece of code?

class Demo:
    def __init__(self):
        self.x = 1
    def change(self):
        self.x = 10
class Demo_derived(Demo):
    def change(self):
        self.x=self.x+1
        return self.x
def main():
    obj = Demo_derived()
    print(obj.change())
 
main()

a) 11
b) 2
c) 1
d) An exception is thrown

View Answer

Answer: d [Reason:] The derived class method change() overrides the base class method.

6. A class in which one or more methods are only implemented to raise an exception is called an abstract class. True or False?
a) True
b) False

View Answer

Answer: a [Reason:] A class in which one or more methods are unimplemented or implemented for the methods throw an exception is called an abstract class.

7. Overriding means changing behaviour of methods of derived class methods in the base class. Is the statement true or false?
a) True
b) False

View Answer

Answer: b [Reason:] Overriding means if there are two same methods present in the superclass and the subclass, the contents of the subclass method are executed.

8. What is the output of the following piece of code?

class A:
    def __repr__(self):
        return "1"
class B(A):
    def __repr__(self):
        return "2"
class C(B):
    def __repr__(self):
        return "3"
o1 = A()
o2 = B()
o3 = C()
print(obj1, obj2, obj3)

a) 1 1 1
b) 1 2 3
c) ‘1’ ‘1’ ‘1’
d) An exception is thrown

View Answer

Answer: b [Reason:] When different objects are invoked, each of the individual classes return their individual values and hence it is printed.

9. What is the output of the following piece of code?

class A:
    def __init__(self):
        self.multiply(15)
        print(self.i)
 
    def multiply(self, i):
        self.i = 4 * i;
class B(A):
    def __init__(self):
        super().__init__()
 
    def multiply(self, i):
        self.i = 2 * i;
obj = B()

a) 15
b) 60
c) An exception is thrown
d) 30

View Answer

Answer: d [Reason:] The derived class B overrides base class A.

10. What is the output of the following piece of code?

class Demo:
    def check(self):
        return " Demo's check "  
    def display(self):
        print(self.check())
class Demo_Derived(Demo):
    def check(self):
        return " Derived's check "
Demo().display()
Demo_Derived().display()

a) Demo’s check Derived’s check
b) Demo’s check Demo’s check
c) Derived’s check Demo’s check
d) Syntax error

View Answer

Answer: a [Reason:] Demo().display() invokes the display() method in class Demo and Demo_Derived().display() invokes the display() method in class Demo_Derived.

11. What is the output of the following piece of code?

class A:
    def __init__(self):
        self.multiply(15)
    def multiply(self, i):
        self.i = 4 * i;
class B(A):
    def __init__(self):
        super().__init__()
        print(self.i)
 
    def multiply(self, i):
        self.i = 2 * i;
obj = B()

a) 15
b) 30
c) An exception is thrown
d) 60

View Answer

Answer: b [Reason:] The derived class B overrides base class A.

12. What is the output of the following piece of code?

class Demo:
    def __check(self):
        return " Demo's check "
    def display(self):
        print(self.check())
class Demo_Derived(Demo):
    def __check(self):
        return " Derived's check "
Demo().display()
Demo_Derived().display()

a) Demo’s check Derived’s check
b) Demo’s check Demo’s check
c) Derived’s check Demo’s check
d) Syntax error

View Answer

Answer: b [Reason:] The method check is private so it can’t be accessed by the derived class. Execute the code in the Python shell.

13. What is the output of the following piece of code?

class A:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __str__(self):
        return 1
    def __eq__(self, other):
        return self.x * self.y == other.x * other.y
obj1 = A(5, 2)
obj2 = A(2, 5)
print(obj1 == obj2)

a) False
b) 1
c) True
d) An exception is thrown

View Answer

Answer: c [Reason:] Since 5*2==2*5, True is printed. Execute it in the Python shell to verify.

14. What is the output of the following piece of code?

class A:
    def one(self):
        return self.two()    	
    def two(self):
        return 'A'   
class B(A):
    def two(self):
        return 'B'
obj2=B()
print(obj2.two())

a) A
b) An exception is thrown
c) A B
d) B

View Answer

Answer: d [Reason:] The derived class method two() overrides the method two() in the base class A.

15. Which of the following statements is true?
a) A non-private method in a superclass can be overridden
b) A subclass method can be overridden by the superclass
c) A private method in a superclass can be overridden
d) Overriding isn’t possible in Python

View Answer

Answer: a [Reason:] A public method in the base class can be overridden by the same named method in the subclass.

Python MCQ Set 5

1. The value of the expressions 4/(3*(2-1)) and 4/3*(2-1) is the same. State whether true or false.
a) True
b) False

View Answer

Answer: a [Reason:] Although the presence of parenthesis does affect the order of precedence, in the case shown above, it is not making a difference. The result of both of these expressions is 1.333333333. Hence the statement is true.

2. The value of the expression:

4 + 3 % 5

a) 4
b) 7
c) 2
d) 0

View Answer

Answer: b [Reason:] The order of precedence is: %, +. Hence the expression above, on simplification results in 4 + 3 = 7. Hence the result is 7.

3. Evaluate the expression given below if A= 16 and B = 15.

A % B // A

a) 0.0
b) 0
c) 1.0
d) 1

View Answer

Answer: b [Reason:] The above expression is evaluated as: 16%15//16, which is equal to 1//16, which results in 0.

4. Which of the following operators has its associativity from right to left?
a) +
b) //
c) %
d) **

View Answer

Answer: d [Reason:] All of the operators shown above have associativity from left to right, except exponentiation operator (**) which has its associativity from right to left.

5. What is the value of x if:

x = int(43.55+2/2)

a) 43
b) 44
c) 22
d) 23

View Answer

Answer: b [Reason:] The expression shown above is an example of explicit conversion. It is evaluated as int(43.55+1) = int(44.55) = 44. Hence the result of this expression is 44.

6. What is the value of the following expression?

2+4.00, 2**4.0

a) (6.0, 16.0)
b) (6.00, 16.00)
c) (6, 16)
d) (6.00, 16.0)

View Answer

Answer: a [Reason:] The result of the expression shown above is (6.0, 16.0). This is because the result is automatically rounded off to one decimal place.

7. Which of the following is the truncation division operator?
a) /
b) %
c) //
d) |

View Answer

Answer: c [Reason:] // is the operator for truncation division. It it called so because it returns only the integer part of the quotient, truncating the decimal part. For example: 20//3 = 6.

8. What are the values of the following expressions:

 2**(3**2)
 (2**3)**2
 2**3**2

a) 64, 512, 64
b) 64, 64, 64
c) 512, 512, 512
d) 512, 64, 512

View Answer

Answer: d [Reason:] Expression 1 is evaluated as: 2**9, which is equal to 512.Expression 2 is evaluated as 8**2, which is equal to 64. The last expression is evaluated as 2**(3**2). This is because the associativity of ** operator is from right to left. Hence the result of the third expression is 512.

9. What is the value of the following expression:

8/4/2, 8/(4/2)

a) (1.0, 4.0)
b) (1.0, 1.0)
c) (4.0. 1.0)
d) (4.0, 4.0)

View Answer

Answer: a [Reason:] The above expressions are evaluated as: 2/2, 8/2, which is equal to (1.0, 4.0).

10. What is the value of the following expression:

float(22//3+3/3)

a) 8
b) 8.0
c) 8.3
d) 8.33

View Answer

Answer: b [Reason:] The expression shown above is evaluated as: float( 7+1) = float(8) = 8.0. Hence the result of this expression is 8.0.

Synopsis and Project Report

You can buy synopsis and project from distpub.com. Just visit https://distpub.com/product-category/projects/ and buy your university/institute project from distpub.com