Python — Learn

Python Mcq Set 19

Review each question and reveal answers to strengthen your understanding

1 Which function is used to close a file in python?
✅ Correct Answer: 1
2 Is it possible to create a text file in python?
✅ Correct Answer: 1
3 Which of the following are the modes of both writing and reading in binary format in file?
✅ Correct Answer: 1
4 Which of the following is not a valid mode to open a file?
✅ Correct Answer: 2
5 What is the difference between r+ and w+ modes?
✅ Correct Answer: 2
6 How do you get the name of a file from a file object (fp)?
✅ Correct Answer: 1
7 Which of the following is not a valid attribute of a file object (fp)?
✅ Correct Answer: 4
8 How do you close a file object (fp)?
✅ Correct Answer: 3
9 How do you get the current position within the file?
✅ Correct Answer: 2
10 How do you rename a file?
✅ Correct Answer: 2
11 How do you delete a file?
✅ Correct Answer: 3
12 How do you change the file position to an offset value from the start?
✅ Correct Answer: 1
13 What happens if no arguments are passed to the seek function?
✅ Correct Answer: 4
14 Which function is called when the following Python code is executed? f = foo() format(f)
✅ Correct Answer: 4
15 Which function overloads the + operator?
✅ Correct Answer: 1
16 Which operator is overloaded by __invert__()?
✅ Correct Answer: 2
17 Which function overloads the == operator?
✅ Correct Answer: 1
18 Which operator is overloaded by __lg__()?
✅ Correct Answer: 4
19 Which function overloads the >> operator?
✅ Correct Answer: 4
20 Let A and B be objects of class Foo. Which functions are called when print(A + B) is executed?
✅ Correct Answer: 1
21 Which operator is overloaded by the __or__() function?
✅ Correct Answer: 2
22 Which function overloads the // operator?
✅ Correct Answer: 3
23 _____ represents an entity in the real world with its identity and behaviour.
✅ Correct Answer: 2
24 ____ is used to create an object.
✅ Correct Answer: 2
25 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()
✅ Correct Answer: 3
26 What is setattr() used for?
✅ Correct Answer: 2
27 What is getattr() used for?
✅ Correct Answer: 1
28 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)
✅ Correct Answer: 2
29 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()
✅ Correct Answer: 3
30 What will be the output of the following Python code? class test: def __init__(self): self.variable = 'Old' self.Change(self.variable) def Change(self, var): var = 'New' obj=test() print(obj.variable)
✅ Correct Answer: 3
31 What is Instantiation in terms of OOP terminology?
✅ Correct Answer: 4
32 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__))
✅ Correct Answer: 3
33 What will be the output of the following Python code? class Demo: def __init__(self): pass def test(self): print(__name__) obj = Demo() obj.test()
✅ Correct Answer: 2
34 The assignment of more than one function to a particular operator is _____
✅ Correct Answer: 3
35 Which of the following is not a class method?
✅ Correct Answer: 1
36 Is the following Python code valid? class B(object): def first(self): print("First method called") def second(): print("Second method called") ob = B() B.first(ob)
✅ Correct Answer: 3
37 What are the methods which begin and end with two underscore characters called?
✅ Correct Answer: 1
38 What will be the output of the following Python 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)
✅ Correct Answer: 3
39 What will be the output of the following Python 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)
✅ Correct Answer: 1
40 What is hasattr(obj,name) used for?
✅ Correct Answer: 3
41 What will be the output of the following Python 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'))
✅ Correct Answer: 2
42 What is delattr(obj,name) used for?
✅ Correct Answer: 2
43 What will be the output of the following Python 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__)
✅ Correct Answer: 4
44 What does print(Test.__name__) display (assuming Test is the name of the class)?
✅ Correct Answer: 3
45 Which of the following best describes inheritance?
✅ Correct Answer: 1
46 Which of the following statements is wrong about inheritance?
✅ Correct Answer: 3
47 What will be the output of the following Python 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()
✅ Correct Answer: 3
48 What will be the output of the following Python code? class A(): def disp(self): print("A disp()") class B(A): pass obj = B() obj.disp()
✅ Correct Answer: 2
49 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?
✅ Correct Answer: 1
50 What will be the output of the following Python 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()
✅ Correct Answer: 3
Back to All Quizzes