Python Multiple Choice Questions(MCQs)& Answers

Sports GK Questions and Answers 2024 (Latest Updated)

Awards & Honours GK Questions 2024 (Latest Updated)

Questions
41 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

Answer: To set an attribute
42 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

Answer: Special methods
Advertisement
Article and Schedule Quiz Start Test!
DOWNLOAD CURRENT AFFAIRS PDF FROM APP
43 Which of these is a private data field?

def Demo:

def __init__(self):

__a = 1

self.__b = 1

self.__c__ = 1

__d__= 1

A __a
B __b
C __c__
D __b__

Answer: __b
44 When will the else part of try-except-else be executed?
A always
B when an exception occurs
C when no exception occurs
D when an exception occurs in to except block

Answer: when no exception occurs
45 What is the output of the code shown below?

def f(x):

yield x+1

print("test")

yield x+2

g=f(9)

A Error
B test
C test 10 12
D No output

Answer: No output
46 The output of the code shown below is:

int('65.43')

A ImportError
B ValueError
C TypeError
D NameError

Answer:ValueError
47 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:A disp()
48 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: The __eq(other) method is defined in the object class
49 Which function overloads the == operator?
A __eq__()
B __equ__()
C __isequal__()
D none of the mentioned

Answer: __eq__()
50 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

Answer: An exception is thrown
51 _____ represents an entity in the real world with its identity and behaviour.
A A method
B An object
C A class
D An operator

Answer:An object
52 _____ is used to create an object.
A class
B constructor
C User-defined functions
D In-built functions

Answer:constructor
53 What will be the output of the following Python 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

Answer: “Hello World” is displayed
54 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

Answer: To set an attribute
55 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

Answer: To access the attribute of the object
56 What will be the output of the following Python 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

Answer:7
57 What will be the output of the following Python 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

Answer: Error as one argument is required while creating the object
58 What will be the output of the following Python code?

advertisement

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

Answer: ‘Old’ is printed
59 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

Answer: Creating an instance of class
60 What will be the output of the following Python 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

Answer: 13

Random GK Questions

Assam Direct Recruitment Test Series

Teacher Eligibility Test

Assam Direct Recruitment Test Series