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 the following functions is a built-in function in python?
a) seed()
b) sqrt()
c) factorial()
d) print()

View Answer

Answer: d [Reason:] The function seed is a function which is present in the random module. The functions sqrt and factorial are a part of the math module. The print function is a built-in function which prints a value directly to the system output.

2. What is the output of the expression:

round(4.576)

a) 4.5
b) 5
c) 4
d) 4.6

View Answer

Answer: b [Reason:] This is a built-in function which rounds a number to give precision in decimal digits. In the above case, since the number of decimal places has not been specified, the decimal number is rounded off to a whole number. Hence the output will be 5.

3. The function pow(x,y,z) is evaluated as:
a) (x**y)**z
b) (x**y) / z
c) (x**y) % z
d) (x**y)*z

View Answer

Answer: c [Reason:] The built-in function pow() can accept two or three arguments. When it takes in two arguments, they are evaluated as: x**y. When it takes in three arguments, they are evaluated as: (x**y)%z.

4. What is the output of the function shown below?

all([2,4,0,6])

a) Error
b) True
c) False
c) 0

View Answer

Answer: c [Reason:] The function all returns false if any one of the elements of the iterable is zero and true if all the elements of the iterable are non zero. Hence the output of this function will be false.

5. What is the output of the expression?

round(4.5676,2)?

a) 4.5
b) 4.6
c) 4.57
d) 4.56

View Answer

Answer: c [Reason:] The function round is used to round off the given decimal number to the specified decimal places. In this case the number should be rounded off to two decimal places. Hence the output will be 4.57.

6. What is the output of the following function?

any([2>8, 4>2, 1>2])

a) Error
b) True
c) False
d) 4>2

View Answer

Answer: b [Reason:] The built-in function any() returns true if any or more of the elements of the iterable is true (non zero), If all the elements are zero, it returns false.

7. What is the output of the function shown below?

import math
abs(math.sqrt(25))

a) Error
b) -5
c) 5
d) 5.0

View Answer

Answer: d [Reason:] The abs() function prints the absolute value of the argument passed. For example: abs(-5)=5. Hence , in this case we get abs(5.0)=5.0.

8. What are the outcomes of the functions shown below?

sum(2,4,6)
sum([1,2,3])

a) Error, 6
b) 12, Error
c) 12, 6
d) Error, Error

View Answer

Answer: a [Reason:] The first function will result in an error because the function sum() is used to find the sum of iterable numbers. Hence the outcomes will be Error and 6 respectively.

9. What is the output of the function:

all(3,0,4.2)

a) True
b) False
c) Error
d) 0

View Answer

Answer: c [Reason:] The function all() returns ‘True’ if any one or more of the elements of the iterable are non zero. In the above case, the values are not iterable, hence an error is thrown.

10. What is the output of the functions shown below?

min(max(False,-3,-4), 2,7)

a) 2
b) False
c) -3
d) -4

View Answer

Answer: b [Reason:] The function max() is being used to find the maximum value from among -3, -4 and false. Since false amounts to the value zero, hence we are left with min(0, 2, 7) Hence the output is 0 (false).

Python MCQ Set 2

1. What are the outcomes of the following functions?

chr(97)
chr(97)

a) a
Error
b) ‘a’
a
c) Error
a
d) Error
Error

View Answer

Answer: c [Reason:] The built-in function chr() returns the alphabet corresponding to the value given as an argument. This function accepts only integer type values. In the first function, we have passed a string. Hence the first function throws an error.

2. What is the output of the following function?

complex(1+2j)

a) Error
b) 1
c) 2j
d) 1+2j

View Answer

Answer: d [Reason:] The built-in function complex() returns the argument in a complex form. Hence the output of the function shown above will be 1+2j.

3. What is the output of the function complex() ?
a) 0j
b) 0+0j
c) 0
d) Error

View Answer

Answer: a [Reason:] The complex function returns 0j if both of the arguments are omitted, that is, if the function is in the form of complex() or complex(0), then the output will be 0j.

4. The function divmod(a,b), where both ‘a’ and ‘b’ are integers is evaluated as:
a) (a%b, a//b)
b) (a//b, a%b)
c) (a//b, a*b)
c) (a/b, a%b)

View Answer

Answer: b [Reason:] The function divmod(a,b) is evaluated as a//b, a%b, if both ‘a’ and ‘b’ are integers.

5. What is the output of the functions shown below?

divmod(10.5,5)
divmod(2.4,1.2)

a) (2.00, 0.50)
(2.00, 0.00)
b) (2, 0.5)
(2, 0)
c) (2.0, 0.5)
(2.0, 0.0)
d) (2, 0.5)
(2)

View Answer

Answer: c [Reason:] See python documentation for the function divmod.

6. The function complex(‘2-3j’) is valid but the function complex(‘2 – 3j’) is invalid. State whether this statement is true or false.
a) True
b) False

View Answer

Answer: a [Reason:] When converting from a string, the string must not contain any blank spaces around the + or – operator. Hence the function complex(‘2 – 3j’) will result in an error.

7. What is the output of the function shown below?

list(enumerate([2, 3]))

a) Error
b) [(1, 2), (2, 3)]
c) [(0, 2), (1, 3)]
d) [(2, 3)]

View Answer

Answer: c [Reason:] The built-in function enumerate() accepts an iterable as an argument. The function shown in the above case returns containing pairs of the numbers given, starting from 0. Hence the output will be: [(0, 2), (1,3)].

8. What are the outcomes of the function shown below?

x=3
eval('x^2')

a) Error
b) 1
c) 9
d) 6

View Answer

Answer: b [Reason:] The function eval is use to evaluate the expression that it takes as an argument. In the above case, the eval() function is used to perform XOR operation between 3 and 2. Hence the output is 1.

9. What is the output of the functions shown below?

float('1e-003')
float('2e+003')

a) 3.00
300
b) 0.001
2000.0
c) 0.001
200
d) Error
2003

View Answer

Answer: b [Reason:] The output of the first function will be 0.001 and that of the second function will be 2000.0. The first function created a floating point number up to 3 decimal places and the second function adds 3 zeros after the given number.

10. Which of the following functions does not necessarily accept only iterables as arguments?
a) enumerate()
b) all()
c) chr()
d) max()

View Answer

Answer: c [Reason:] The functions enumerate(), all() and max() accept iterables as arguments whereas the function chr() throws an error on receiving an iterable as an argument. Also note that the function chr() accepts only integer values.

Python MCQ Set 3

1. Which of the following functions accepts only integers as arguments?
a) ord()
b) min()
c) chr()
d) any()

View Answer

Answer: c [Reason:] The function chr() accepts only integers as arguments. The function ord() accepts only strings. The functions min() and max() can accept floating point as well as integer arguments.

2. Suppose there is a list such that: l=[2,3,4].
If we want to print this list in reverse order, which of the following methods should be used?
a) reverse(l)
b) list(reverse[(l)])
c) reversed(l)
d) list(reversed(l))

View Answer

Answer: d [Reason:] The built-in function reversed() can be used to reverse the elements of a list. This function accepts only an iterable as an argument. To print the output in the form of a list, we use: list(reversed(l)). The output will be: [4,3,2].

3. The output of the function:

float('   -12345n')

(Note that the number of blank spaces before the number is 5)
a) -12345.0 (5 blank spaces before the number)
b) -12345.0
c) Error
d) -12345.000000000…. (infinite decimal places)

View Answer

Answer: b [Reason:] The function float() will remove all the blank spaces and covert the integer to a floating point number. Hence the output will be: -12345.0.

4. What is the output of the functions shown below?

ord(65)
ord(‘A’)

a) A
65
b) Error
65
c) A
Error
c) Error
Error

View Answer

Answer: b [Reason:] The built-in function ord() is used to return the ASCII value of the alphabet passed to it as an argument. Hence the first function results in an error and the output of the second function is 65.

5. What is the output of the functions shown below?

float(‘-infinity’)
float(‘inf’)

a) –inf
inf
b) –infinity
inf
c) Error
Error
d) Error
Junk value

View Answer

Answer: a [Reason:] The output of the first function will be –inf and that of the second function will be inf.

6. Which of the following functions will not result in an error when no arguments are passed to it?
a) min()
b) divmod()
c) all()
d) float()

View Answer

Answer: d [Reason:] The built-in functions min(), max(), divmod(), ord(), any(), all() etc throw an error when no arguments are passed to them. However there are some built-in functions like float(), complex() etc which do not throw an error when no arguments are passed to them. The output of float() is 0.0.

7. What is the output of the function shown below?

hex(15)

a) f
b) 0xF
c) 0Xf
d) 0xf

View Answer

Answer: d [Reason:] The function hex() is used to convert the given argument into its hexadecimal representation, in lower case. Hence the output of the function hex(15) is 0xf.

8. Which of the following functions does not throw an error?
a) ord()
b) ord(‘ ‘)
c) ord(”)
d) ord(“”)

View Answer

Answer: b [Reason:] The function ord() accepts a character. Hence ord(), ord(”) and ord(“”) throw errors. However the function ord(‘ ‘) does not throw an error because in this case, we are actually passing a blank space as an argument. The output of ord(‘ ‘) is 32 (ASCII value corresponding to blank space).

9. What is the output of the function:

len(["hello",2, 4, 6])

a) 4
b) 3
c) Error
d) 6

View Answer

Answer: a [Reason:] The function len() returns the length of the number of elements in the iterable. Therefore the output of the function shown above is 4.

10. What is the output of the function shown below?

oct(7)
oct(7)

a) Error
07
b) 0o7
Error
c) 0o7
Error
d) 07
0o7

View Answer

Answer: c [Reason:] The function oct() is used to convert its argument into octal form. This function does not accept strings. Hence the second function results in an error while the output of the first function is 0o7.

Python MCQ Set 4

1. _____ represents an entity in the real world with its identity and behaviour.
a) A method
b) An object
c) A class
d) An operator

View Answer

Answer: b [Reason:] An object represents an entity in the real world that can be distinctly identified. A class may define an object.

2. _____ is used to create an object.
a) class
b) constructor
c) User-defined functions
d) In-built functions

View Answer

Answer: b [Reason:] The values assigned by the constructor to the class members is used to create the object.

3. What is the output of the following code?

class test:
     def __init__(self,a="Hello World"):
         self.a=a
 
     def display(self):
         print(self.a)
obj=test()
obj.display()

a) The program has an error because constructor can’t have default arguments
b) Nothing is displayed
c) “Hello World” is displayed
d) The program has an error display function doesn’t have parameters

View Answer

Answer: c [Reason:] The program has no error. “Hello World” is displayed. Execute in python shell to verify.

4. What is setattr() used for?
a) To access the attribute of the object
b) To set an attribute
c) To check if an attribute exists or not
d) To delete an attribute

View Answer

Answer: b [Reason:] setattr(obj,name,value) is used to set an attribute. If attribute doesn’t exist, then it would be created.

5. What is getattr() used for?
a) To access the attribute of the object
b) To delete an attribute
c) To check if an attribute exists or not
d) To set an attribute

View Answer

Answer: a [Reason:] getattr(obj,name) is used to get the attribute of an object.

6. What is the output of the following code?

class change:
    def __init__(self, x, y, z):
        self.a = x + y + z
 
x = change(1,2,3)
y = getattr(x, 'a')
setattr(x, 'a', y+1)
print(x.a)

a) 6
b) 7
c) Error
d) 0

View Answer

Answer: b [Reason:] First, a=1+2+3=6. Then, after setattr() is invoked, x.a=6+1=7.

7. What is the output of the following code?

 class test:
     def __init__(self,a):
         self.a=a
 
     def display(self):
         print(self.a)
obj=test()
obj.display()

a) Runs normally, doesn’t display anything
b) Displays 0, which is the automatic default value
c) Error as one argument is required while creating the object
d) Error as display function requires additional argument

View Answer

Answer: c [Reason:] Since, the __init__ special method has another argument a other than self, during object creation, one argument is required. For example: obj=test(“Hello”)

8. Is the following piece of code correct?

>>> class A:
	def __init__(self,b):
		self.b=b
	def display(self):
		print(self.b)
>>> obj=A("Hello")
>>> del obj

a) True
b) False

View Answer

Answer: a [Reason:] It is possible to delete an object of the class. On further typing obj in the python shell, it throws an error because the defined object has now been deleted.

9. What is the output of the following code?

class test:
    def __init__(self):
        self.variable = 'Old'
        self.Change(self.variable)
    def Change(self, var):
        var = 'New'
obj=test()
print(obj.variable)

a) Error because function change can’t be called in the __init__ function
b) ‘New’ is printed
c) ‘Old’ is printed
d) Nothing is printed

View Answer

Answer: c [Reason:] This is because strings are immutable. Hence any change made isn’t reflected in the original string.

10. What is Instantiation in terms of OOP terminology?
a) Deleting an instance of class
b) Modifying an instance of class
c) Copying an instance of class
d) Creating an instance of class

View Answer

Answer: d [Reason:] Instantiation refers to creating an object/instance for a class.

11. What is the output of the following code?

class fruits:
    def __init__(self, price):
        self.price = price
obj=fruits(50)
 
obj.quantity=10
obj.bags=2
 
print(obj.quantity+len(obj.__dict__))

a) 12
b) 52
c) 13
d) 60

View Answer

Answer: c [Reason:] In the above code, obj.quantity has been initialised to 10. There are a total of three items in the dictionary, price, quantity and bags. Hence, len(obj.__dict__) is 3.

12. What is the output of the following code?

 class Demo:
    def __init__(self):
        pass
 
    def test(self):
        print(__name__)
 
obj = Demo()
obj.test()

a) Exception is thrown
b) __main__
c) Demo
d) test

View Answer

Answer: b [Reason:] Since the above code is being run not as a result of an import from another module, the variable will have value “__main__”.

Python MCQ Set 5

1. The assignment of more than one function to a particular operator is _______
a) Operator over-assignment
b) Operator overriding
c) Operator overloading
d) Operator instance

View Answer

Answer: c [Reason:] The assignment of more than one function to a particular operator is called as operator overloading.

2. Which of the following is not a class method?
a) Non-static
b) Static
c) Bounded
d) Unbounded

View Answer

Answer: a [Reason:] The three different class methods in Python are static, bounded and unbounded methods.

3. What is the output of the following code?

    c.test=c.test+1
    k=k+1
class A:
    def __init__(self):
        self.test = 0
def main():
    Count=A()
    k=0
 
    for i in range(0,25):
        add(Count,k)
    print("Count.test=", Count.test)
    print("k =", k)
main()

a) Exception is thrown
b) Count.test=25
k=25
c) Count.test=25
k=0
d) Count.test=0
k=0

View Answer

Answer: c [Reason:] The program has no error. Here, test is a member of the class while k isn’t. Hence test keeps getting incremented time while k remains 0.

4. Which piece of code creates an empty class?

a)

class A:
    return

b)

class A:
    pass

c)

class A:

d) It is not possible to create an empty class.

View Answer

Answer: b [Reason:] Execute in python shell to verify.

5. Is the following piece of code valid?

class B(object):
  def first(self):
    print("First method called")
  def second():
    print("Second method called")
ob = B()
B.first(ob)

a) It isn’t as the object declaration isn’t right
b) It isn’t as there isn’t any __init__ method for initializing class members
c) Yes, this method of calling is called unbounded method call
d) Yes, this method of calling is called bounded method call

View Answer

Answer: c [Reason:] The method may be created in the method demonstrated in the code as well and this is called as the unbounded method call. Calling the method using obj.one() is the bounded method call.

6. What are the methods which begin and end with two underscore characters called?
a) Special methods
b) In-built methods
c) User-defined methods
d) Additional methods

View Answer

Answer: a [Reason:] Special methods like __init__ begin and end with two underscore characters.

7. Special methods need to be explicitly called during object creation. True or False?
a) True
b) False

View Answer

Answer: b [Reason:] Special methods are automatically called during object creation.

8. What is the output of the following code?

>>> class demo():
	def __repr__(self):
		return '__repr__ built-in function called'
	def __str__(self):
		return '__str__ built-in function called'
>>> s=demo()
>>> s

a) Error
b) Nothing is printed
c) __str__ called
d) __repr__ called

View Answer

Answer: d [Reason:] __repr__ is used for producing a string representation of an object’s value that Python can evaluate. Execute in python shell to verify.

9. What is the output of the following code?

>>> class demo():
	def __repr__(self):
		return '__repr__ built-in function called'
	def __str__(self):
		return '__str__  built-in function called'
>>> s=demo()
>>> print(s)

a) __str__ called
b) __repr__ called
c) Error
d) Nothing is printed

View Answer

Answer: a [Reason:] __str__ is used for producing a string representation of an object’s value that is most readable for humans. Execute in python shell to verify.

10. What is hasattr(obj,name) used for?
a) To access the attribute of the object
b) To delete an attribute
c) To check if an attribute exists or not
d) To set an attribute

View Answer

Answer: c [Reason:] hasattr(obj,name) checks if an attribute exists or not and returns True or False.

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

class stud:
   def __init__(self, roll_no, grade):
      self.roll_no = roll_no
      self.grade = grade
   def display (self):
      print("Roll no : ", self.roll_no,  ", Grade: ", self.grade)
stud1 = stud(34, ‘S’)
stud1.age=7
print(hasattr(stud1, 'age'))

a) Error as age isn’t defined
b) True
c) False
d) 7

View Answer

Answer: a [Reason:] Execute in python shell to verify.

12. What is delattr(obj,name) used for?
a) To print deleted attribute
b) To delete an attribute
c) To check if an attribute is deleted or not
d) To set an attribute

View Answer

Answer: b [Reason:] delattr(obj,name) deletes an attribute in a class.

13. __del__ method is used to destroy instances of a class. True or False?
a) True
b) False

View Answer

Answer: a [Reason:] ___del__ method acts as a destructor and is used to destroy objects of classes.

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

class stud:
   ‘Base class for all students’
   def __init__(self, roll_no, grade):
      self.roll_no = roll_no
      self.grade = grade
   def display (self):
      print("Roll no : ", self.roll_no,  ", Grade: ", self.grade)
print(student.__doc__)

a) Exception is thrown
b) __main__
c) Nothing is displayed
d) Base class for all students

View Answer

Answer: d [Reason:] ___doc__ built-in class attribute is used to print the class documentation string or none, if undefined.

15. What does print(Test.__name__) display (assuming Test is the name of the class) ?
a) ()
b) Exception is thrown
c) Test
d) __main__

View Answer

Answer: c [Reason:] __name__ built-in class attribute is used to display the class name.

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