Python — Quiz

Python Mcq Set 19

50 questions · Test your knowledge

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