Select Page
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. The output of the code shown below is:

```def f1():
x=15
print(x)
x=12
f1()```

a) Error
b) 12
c) 15
d) 1512

Answer: c [Reason:] In the code shown above, x=15 is a local variable whereas x=12 is a global variable. Preference is given to local variable over global variable. Hence the output of the code shown above is 15.

2. What is the output of the code shown below?

```def f1():
x=100
print(x)
x=+1
f1()```

a) Error
b) 100
c) 101
d) 99

Answer: b [Reason:] The variable x is a local variable. It is first printed and then modified. Hence the output of this code is 100.

3. What is the output of the code shown below?

```def san(x):
print(x+1)
x=-2
x=4
san(12)```

a) 13
b) 10
c) 2
d) 5

Answer: a [Reason:] The value passed to the function san() is 12. This value is incremented by one and printed. Hence the output of the code shown above is 13.

4. What is the output of the code shown?

```def f1():
global x
x+=1
print(x)
x=12
print("x")```

a) Error
b) 13
c) 13
x
d) x

Answer: d [Reason:] In the code shown above, the variable ‘x’ is declared as global within the function. Hence the output is ‘x’. Had the variable ‘x’ been a local variable, the output would have been: 13 x

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

```def f1(x):
global x
x+=1
print(x)
f1(15)
print("hello")```

a) error
b) hello
c) 16
d) 16
hello

Answer: a [Reason:] The code shown above will result in an error because ‘x’ is a global variable. Had it been a local variable, the output would be: 16 hello

6. What is the output of the following code?

```x=12
def f1(a,b=x):
print(a,b)
x=15
f1(4)```

a) Error
b) 12 4
c) 4 12
d) 4 15

Answer: c [Reason:] At the time of leader processing, the value of ‘x’ is 12. It is not modified later. The value passed to the function f1 is 4. Hence the output of the code shown above is 4 12.

7. What is the output of the code shown?

```def f():
global a
print(a)
a = "hello"
print(a)
a = "world"
f()
print(a)```

a) hello
hello
world
b) world
world
hello
c) hello
world
world
d) world
hello
world

Answer: b [Reason:] Since the variable ‘a’ has been explicitly specified as a global variable, the value of a passed to the function is ‘world’. Hence the output of this code is: world world hello

8. What is the output of the code shown below?

```def f1(a,b=[]):
b.append(a)
return b
print(f1(2,[3,4]))```

a) [3,2,4]
b) [2,3,4]
c) Error
d) [3,4,2]

Answer: d [Reason:] In the code shown above, the integer 2 is appended to the list [3,4]. Hence the output of the code is [3,4,2]. Both the variables a and b are local variables.

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

```def f(p, q, r):
global s
p = 10
q = 20
r = 30
s = 40
print(p,q,r,s)
p,q,r,s = 1,2,3,4
f(5,10,15)```

a) 1 2 3 4
b) 5 10 15 4
c) 10 20 30 40
d) 5 10 15 40

Answer: c [Reason:] The above code shows a combination of local and global variables. The output of this code is: 10 20 30 40

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

```def f(x):
print("outer")
def f1(a):
print("inner")
print(a,x)
f(3)
f1(1)```

a) outer
error
b) inner
error
c) outer
inner
d) error

Answer: a [Reason:] The error will be caused due to the statement f1(1) because the function is nested. If f1(1) had been called inside the function, the output would have been different and there would be no error.

11. The output of code shown below is:

```x = 5
def f1():
global x
x = 4
def f2(a,b):
global x
return a+b+x
f1()
total = f2(1,2)
print(total)```

a) Error
b) 7
c) 8
d) 15

Answer: b [Reason:] In the code shown above, the variable ‘x’ has been declared as a global variable under both the functions f1 and f2. The value returned is a+b+x = 1+2+4 = 7.

12. What is the output of the code shown below?

```x=100
def f1():
global x
x=90
def f2():
global x
x=80
print(x)```

a) 100
b) 90
c) 80
d) Error

Answer: a [Reason:] The output of the code shown above is 100. This is because the variable ‘x’ has been declared as global within the functions f1 and f2.

13. Read the code shown below carefully and point out the global variables:

```y, z = 1, 2
def f():
global x
x = y+z```

a) x
b) y and z
c) x, y and z
d) Neither x, nor y, nor z

Answer: c [Reason:] In the code shown above, x, y and z are global variables inside the function f. y and z are global because they are not assigned in the function. x is a global variable because it is explicitly specified so in the code. Hence, x, y and z are global variables.

## Python MCQ Set 2

1. Which of the following data structures is returned by the functions globals() and locals()?
a) list
b) set
c) dictionary
d) tuple

Answer: c [Reason:] Both the functions, that is, globals() and locals() return value of the data structure dictionary.

2. What is the output of the code shown below?

```x=1
def cg():
global x
x=x+1
cg()
x```

a) 2
b) 1
c) 0
d) Error

Answer: a [Reason:] Since ‘x’ has been declared a global variable, it can be modified very easily within the function. Hence the output is 2.

3. On assigning a value to a variable inside a function, it automatically becomes a global variable. State whether true or false.
a) True
b) False

Answer: b [Reason:] On assigning a value to a variable inside a function, t automatically becomes a local variable. Hence the above statement is false.

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

```e="butter"
def f(a): print(a)+e
f("bitter")```

a) error
b) butter
error
c) bitter
error
d) bitterbutter

Answer: c [Reason:] The output of the code shown above will be ‘bitter’, followed by an error. The error is because the operand ‘+’ is unsupported on the types used above.

5. What happens if a local variable exists with the same name as the global variable you want to access?
a) Error
b) The local variable is shadowed
c) Undefined behavior
d) The global variable is shadowed

Answer: d [Reason:] If a local variable exists with the same name as the local variable that you want to access, then the global variable is shadowed. That is, preference is given to the local variable.

6. What is the output of the code shown below?

```a=10
globals()['a']=25
print(a)```

a) 10
b) 25
c) Junk value
d) Error

Answer: b [Reason:] In the code shown above, the value of ‘a’ can be changed by using globals() function. The dictionary returned is accessed using key of the variable ‘a’ and modified to 25.

7. What is the output of this code?

```def f(): x=4
x=1
f()
x```

a) Error
b) 4
c) Junk value
d) 1

Answer: d [Reason:] In the code shown above, when we call the function f, a new namespace is created. The assignment x=4 is performed in the local namespace and does not affect the global namespace. Hence the output is 1.

8. ______________ returns a dictionary of the module namespace.
________________ returns a dictionary of the current namespace.
a) locals()
globals()
b) locals()
locals()
c) globals()
locals()
d) globals()
globals()

Answer: c [Reason:] The function globals() returns a dictionary of the module namespace, whereas the function locals() returns a dictionary of the current namespace.

## Python MCQ Set 3

1. Which of the following best describes inheritance?
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 implementation of elegant software that is well designed and easily modified

Answer: a [Reason:] If the class definition is class B(A): then class B inherits the methods of class A. This is called inheritance.

2. Which of the following statements is wrong about inheritance?
a) Protected members of a class can be inherited
b) The inheriting class is called a subclass
c) Private members of a class can be inherited and accessed
d) Inheritance is one of the features of OOP

Answer: c [Reason:] Any changes made to the private members of the class in the subclass aren’t reflected in the original members.

3. What is the output of the following code?

```class Demo:
def __new__(self):
self.__init__(self)
print("Demo's __new__() invoked")
def __init__(self):
print("Demo's __init__() invoked")
class Derived_Demo(Demo):
def __new__(self):
print("Derived_Demo's __new__() invoked")
def __init__(self):
print("Derived_Demo's __init__() invoked")
def main():
obj1 = Derived_Demo()
obj2 = Demo()
main()```

a)
Derived_Demo’s __init__() invoked
Derived_Demo’s __new__() invoked
Demo’s __init__() invoked
Demo’s __new__() invoked
b)
Derived_Demo’s __new__() invoked
Demo’s __init__() invoked
Demo’s __new__() invoked
c)
Derived_Demo’s __new__() invoked
Demo’s __new__() invoked
d)
Derived_Demo’s __init__() invoked
Demo’s __init__() invoked

Answer: b [Reason:] Since the object for the derived class is declared first, __new__() method of the derived class is invoked first, followed by the constructor and the __new__() method of main class.

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

```class Test:
def __init__(self):
self.x = 0
class Derived_Test(Test):
def __init__(self):
self.y = 1
def main():
b = Derived_Test()
print(b.x,b.y)
main()```

a) 0 1
b) 0 0
c) Error because class B inherits A but variable x isn’t inherited
d) Error because when object is created, argument must be passed like Derived_Test(1)

Answer: c [Reason:] Since the invoking method, Test.__init__(self), isn’t present in the derived class, variable x can’t be inherited.

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

```class A():
def disp(self):
print("A disp()")
class B(A):
pass
obj = B()
obj.disp()```

a) Invalid syntax for inheritance
b) Error because when object is created, argument must be passed
c) Nothing is printed
d) A disp()

Answer: d [Reason:] Class B inherits class A hence the function disp () becomes part of class B’s definition. Hence disp() method is properly executed and the line is printed.

6. All subclasses are a subtype in object-oriented programming. Is the statement true or false?
a) True
b) False

Answer: b [Reason:] A subtype is something that be substituted for and behave as its parent type. All subclass may not be a subtype in object-oriented programming.

7. When defining a subclass in Python that is meant to serve as a subtype, the subtype Python keyword is used. Is the statement true or false?
a) True
b) False

Answer: b [Reason:] B is a subtype of B if instances of type B can substitute for instances of type A without affecting semantics.

8. Suppose B is a subclass of A, to invoke the __init__ method in A from B, what is the line of code you should write?
a) A.__init__(self)
b) B.__init__(self)
c) A.__init__(B)
d) B.__init__(A)

Answer: a [Reason:] To invoke the __init__ method in A from B, either of the following should be written: A.__init__(self) or super().__init__(self).

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

```class Test:
def __init__(self):
self.x = 0
class Derived_Test(Test):
def __init__(self):
Test.__init__(self)
self.y = 1
def main():
b = Derived_Test()
print(b.x,b.y)
main()```

a) Error because class B inherits A but variable x isn’t inherited
b) 0 0
c) 0 1
d) Error, the syntax of the invoking method is wrong

Answer: c [Reason:] Since the invoking method has been properly invoked, variable x from the main class has been properly inherited and it can also be accessed.

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

```class A:
def __init__(self, x= 1):
self.x = x
class der(A):
def __init__(self,y = 2):
super().__init__()
self.y = y
def main():
obj = der()
print(obj.x, obj.y)
main()```

a) Error, the syntax of the invoking method is wrong
b) The program runs fine but nothing is printed
c) 1 0
d) 1 2

Answer: d [Reason:] In the above piece of code, the invoking method has been properly implemented and hence x=1 and y=2.

11. What does built-in function type do in context of classes?
a) Determines the object name of any value
b) Determines the class name of any value
c) Determines class description of any value
d) Determines the file name of any value

Answer: b [Reason:] For example: >>> type((1,)) gives .

12. Which of the following is not a type of inheritance?
a) Double-level
b) Multi-level
c) Single-level
d) Multiple

Answer: a [Reason:] Multiple, multi-level, single-level and hierarchical inheritance are all types of inheritance.

13. What does built-in function help do in context of classes?
a) Determines the object name of any value
b) Determines the class identifiers of any value
c) Determines class description of any built-in type
d) Determines class description of any user-defined built-in type

Answer: c [Reason:] help() usually gives information of the class on any built-in type or function.

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'
obj1=A()
obj2=B()
print(obj1.two(),obj2.two())```

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

Answer: b [Reason:] obj1.two() invokes the method two() in class A which returns ‘A’ and obj2.two() invokes the method two() in class B which returns ‘B’.

15. What type of inheritance is illustrated in the following piece of code?

```class A():
pass
class B():
pass
class C(A,B):
pass```

a) Multi-level inheritance
b) Multiple inheritance
c) Hierarchical inheritance
d) Single-level inheritance

Answer: b [Reason:] In multiple inheritance, two or more subclasses are derived from the superclass as shown in the above piece of code.

## Python MCQ Set 4

1. What type of inheritance is illustrated in the following piece of code?

```class A():
pass
class B(A):
pass
class C(B):
pass```

a) Multi-level inheritance
b) Multiple inheritance
c) Hierarchical inheritance
d) Single-level inheritance

Answer: a [Reason:] In multi-level inheritance, a subclass derives from another class which itself is derived from another class.

2. What does single-level inheritance mean?
a) A subclass derives from a class which in turn derives from another class
b) A single superclass inherits from multiple subclasses
c) A single subclass derives from a single superclass
d) Multiple base classes inherit a single derived class

Answer: c [Reason:] In single-level inheritance, there is a single subclass which inherits from a single superclass. So the class definition of the subclass will be: class B(A): where A is the superclass.

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

```class A:
def __init__(self):
self.__i = 1
self.j = 5

def display(self):
print(self.__i, self.j)
class B(A):
def __init__(self):
super().__init__()
self.__i = 2
self.j = 7
c = B()
c.display()```

a) 2 7
b) 1 5
c) 1 7
d) 2 5

Answer: c [Reason:] Any change made in variable i isn’t reflected as it is the private member of the superclass.

4. Which of the following statements isn’t true?
a) A non-private method in a superclass can be overridden
b) A derived class is a subset of superclass
c) The value of a private variable in the superclass can be changed in the subclass
d) When invoking the constructor from a subclass, the constructor of superclass is automatically invoked

Answer: c [Reason:] If the value of a private variable in a superclass is changed in the subclass, the change isn’t reflected.

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

```class A:
def __init__(self,x):
self.x = x
def count(self,x):
self.x = self.x+1
class B(A):
def __init__(self, y=0):
A.__init__(self, 3)
self.y = y
def count(self):
self.y += 1
def main():
obj = B()
obj.count()
print(obj.x, obj.y)
main()```

a) 3 0
b) 3 1
c) 0 1
d) An exception in thrown

Answer: b [Reason:] Initially x=3 and y=0. When obj.count() is called, y=1.

6. What is the output of the following piece of code when executed in the Python shell?

```>>> class A:
pass
>>> class B(A):
pass
>>> obj=B()
>>> isinstance(obj,A)```

a) True
b) False
c) Wrong syntax for isinstance() method
d) Invalid method for classes

Answer: a [Reason:] isinstance(obj,class) returns True if obj is an object class.

7. Which of the following statements is true?
a) The __new__() method automatically invokes the __init__ method
b) The __init__ method is defined in the object class
c) The __eq(other) method is defined in the object class
d) The __repr__() method is defined in the object class

Answer: c [Reason:] The __eq(other) method is called if any comparison takes place and it is defined in the object class.

8. Method issubclass() checks if a class is a subclass of another class. True or False?
a) True
b) False

Answer: a [Reason:] Method issubclass() returns True if a class is a subclass of another class and False otherwise.

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

```class A:
def __init__(self):
self.__x = 1
class B(A):
def display(self):
print(self.__x)
def main():
obj = B()
obj.display()
main()```

a) 1
b) 0
c) Error, invalid syntax for object declaration
d) Error, private class member can’t be accessed in a subclass

Answer: d [Reason:] Private class members in the superclass can’t be accessed in the subclass.

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

```class A:
def __init__(self):
self._x = 5
class B(A):
def display(self):
print(self._x)
def main():
obj = B()
obj.display()
main()```

a) Error, invalid syntax for object declaration
b) Nothing is printed
c) 5
d) Error, private class member can’t be accessed in a subclass

Answer: c [Reason:] The class member x is protected, not private and hence can be accessed by subclasses.

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

```class A:
def __init__(self,x=3):
self._x = x
class B(A):
def __init__(self):
super().__init__(5)
def display(self):
print(self._x)
def main():
obj = B()
obj.display()

main()```

a) 5
b) Error, class member x has two values
c) 3
d) Error, protected class member can’t be accessed in a subclass

Answer: a [Reason:] The super() method re-assigns the variable x with value 5. Hence 5 is printed.

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

```class A:
def test1(self):
print(" test of A called ")
class B(A):
def test(self):
print(" test of B called ")
class C(A):
def test(self):
print(" test of C called ")
class D(B,C):
def test2(self):
print(" test of D called ")
obj=D()
obj.test()```

a) test of B called
test of C called
b) test of C called
test of B called
c) test of B called
d) Error, both the classes from which D derives has same method test()

Answer: c [Reason:] Execute in Python shell to verify. If class D(B,C): is switched is class D(C,B): test of C is called.

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

```class A:
def test(self):
print("test of A called")
class B(A):
def test(self):
print("test of B called")
super().test()
class C(A):
def test(self):
print("test of C called")
super().test()
class D(B,C):
def test2(self):
print("test of D called")
obj=D()
obj.test()```

a) test of B called
test of C called
test of A called
b) test of C called
test of B called
c) test of B called
test of C called
d) Error, all the three classes from which D derives has same method test()

Answer: a [Reason:] Since the invoking method, super().test() is called in the subclasses, all the three methods of test() in three different classes is called.

## Python MCQ Set 5

1. What is the output of the following?

`k = [print(i) for i in my_string if i not in "aeiou"]`

a) prints all the vowels in my_string
b) prints all the consonants in my_string
c) prints all characters of my_string that aren’t vowels
d) prints only on executing print(k)

Answer: c [Reason:] print(i) is executed if the given character is not a vowel.

2. What is the output of print(k) in the following?

```k = [print(i) for i in my_string if i not in "aeiou"]
print(k)```

a) all characters of my_string that aren’t vowels
b) a list of Nones
c) list of Trues
d) list of Falses

Answer: b [Reason:] print() returns None.

3. What is the output of the following?

```my_string = "hello world"
k = [(i.upper(), len(i)) for i in my_string]
print(k)```

a) [(‘HELLO’, 5), (‘WORLD’, 5)].
b) [(‘H’, 1), (‘E’, 1), (‘L’, 1), (‘L’, 1), (‘O’, 1), (‘ ‘, 1), (‘W’, 1), (‘O’, 1), (‘R’, 1), (‘L’, 1), (‘D’, 1)].
c) [(‘HELLO WORLD’, 11)].
d) none of the mentioned

Answer: b [Reason:] We are iterating over each letter in the string.

4. Which of the following is the correct expansion of list_1 = [expr(i) for i in list_0 if func(i)] ?
a)

```list_1 = []
for i in list_0:
if func(i):
list_1.append(i)```

b)

```for i in list_0:
if func(i):
list_1.append(expr(i))```

c)

```list_1 = []
for i in list_0:
if func(i):
list_1.append(expr(i))```

d) none of the mentioned

Answer: c [Reason:] We have to create an empty list, loop over the contents of the existing list and check if a condition is satisfied before performing some operation and adding it to the new list.

5. What is the output of the following?

`x = [i**+1 for i in range(3)]; print(x);`

a) [0, 1, 2].
b) [1, 2, 5].
c) error, **+ is not a valid operator
d) error, ‘;’ is not allowed

Answer: a [Reason:] i**+1 is evaluated as (i)**(+1).

6. What is the output of the following?

`print([i.lower() for i in "HELLO"])`

a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’].
b) ‘hello’
c) [‘hello’].
d) hello

Answer: a [Reason:] We are iterating over each letter in the string.

7. What is the output of the following?

`print([i+j for i in "abc" for j in "def"])`

a) [‘da’, ‘ea’, ‘fa’, ‘db’, ‘eb’, ‘fb’, ‘dc’, ‘ec’, ‘fc’].
b) [[‘ad’, ‘bd’, ‘cd’], [‘ae’, ‘be’, ‘ce’], [‘af’, ‘bf’, ‘cf’]].
c) [[‘da’, ‘db’, ‘dc’], [‘ea’, ‘eb’, ‘ec’], [‘fa’, ‘fb’, ‘fc’]].
d) [‘ad’, ‘ae’, ‘af’, ‘bd’, ‘be’, ‘bf’, ‘cd’, ‘ce’, ‘cf’].

Answer: d [Reason:] If it were to be executed as a nested for loop, i would be the outer loop and j the inner loop.

8. What is the output of the following?

`print([[i+j for i in "abc"] for j in "def"])`

a) [‘da’, ‘ea’, ‘fa’, ‘db’, ‘eb’, ‘fb’, ‘dc’, ‘ec’, ‘fc’].
b) [[‘ad’, ‘bd’, ‘cd’], [‘ae’, ‘be’, ‘ce’], [‘af’, ‘bf’, ‘cf’]].
c) [[‘da’, ‘db’, ‘dc’], [‘ea’, ‘eb’, ‘ec’], [‘fa’, ‘fb’, ‘fc’]].
d) [‘ad’, ‘ae’, ‘af’, ‘bd’, ‘be’, ‘bf’, ‘cd’, ‘ce’, ‘cf’].

Answer: b [Reason:] The inner list is generated once for each value of j.

9. What is the output of the following?

`print([if i%2==0: i; else: i+1; for i in range(4)])`

a) [0, 2, 2, 4].
b) [1, 1, 3, 3].
c) error
d) none of the mentioned

10. Which of the following is the same as list(map(lambda x: x**-1, [1, 2, 3]))?
a) [x**-1 for x in [(1, 2, 3)]].
b) [1/x for x in [(1, 2, 3)]].
c) [1/x for x in (1, 2, 3)].
d) error