aKidsZone
Home
MCQs
Python — Quiz
Python Mcq Set 19
50 questions · Test your knowledge
Home
/
MCQs
/
Python Mcq Set 19
1
Which function is used to close a file in python?
A
Close()
B
Stop()
C
End()
D
Closefile()
2
Is it possible to create a text file in python?
A
Yes
B
No
C
Machine dependent
D
All of the mentioned
3
Which of the following are the modes of both writing and reading in binary format in file?
A
wb+
B
w
C
wb
D
w+
4
Which of the following is not a valid mode to open a file?
A
ab
B
rw
C
r+
D
w+
5
What is the difference between r+ and w+ modes?
A
no difference
B
in r+ the pointer is initially placed at the beginning of the file and the pointer is at the end for w+
C
in w+ the pointer is initially placed at the beginning of the file and the pointer is at the end for r+
D
depends on the operating system
6
How do you get the name of a file from a file object (fp)?
A
fp.name
B
fp.file(name)
C
self.__name__(fp)
D
fp.__name__()
7
Which of the following is not a valid attribute of a file object (fp)?
A
fp.name
B
fp.closed
C
fp.mode
D
fp.size
8
How do you close a file object (fp)?
A
close(fp)
B
fclose(fp)
C
fp.close()
D
fp.__close__()
9
How do you get the current position within the file?
A
fp.seek()
B
fp.tell()
C
fp.loc
D
fp.pos
10
How do you rename a file?
A
fp.name = ‘new_name.txt’
B
os.rename(existing_name, new_name)
C
os.rename(fp, new_name)
D
os.set_name(existing_name, new_name)
11
How do you delete a file?
A
del(fp)
B
fp.delete()
C
os.remove(‘file’)
D
os.delete(‘file’)
12
How do you change the file position to an offset value from the start?
A
fp.seek(offset, 0)
B
fp.seek(offset, 1)
C
fp.seek(offset, 2)
D
none of the mentioned
13
What happens if no arguments are passed to the seek function?
A
file position is set to the start of file
B
file position is set to the end of file
C
file position remains unchanged
D
error
14
Which function is called when the following Python code is executed? f = foo() format(f)
A
format()
B
__format__()
C
str()
D
__str__()
15
Which function overloads the + operator?
A
__add__()
B
__plus__()
C
__sum__()
D
none of the mentioned
16
Which operator is overloaded by __invert__()?
A
!
B
~
C
^
D
–
17
Which function overloads the == operator?
A
__eq__()
B
__equ__()
C
__isequal__()
D
none of the mentioned
18
Which operator is overloaded by __lg__()?
A
<
B
>
C
!=
D
none of the mentioned
19
Which function overloads the >> operator?
A
__more__()
B
__gt__()
C
__ge__()
D
none of the mentioned
20
Let A and B be objects of class Foo. Which functions are called when print(A + B) is executed?
A
__add__(), __str__()
B
__str__(), __add__()
C
__sum__(), __str__()
D
__str__(), __sum__()
21
Which operator is overloaded by the __or__() function?
A
||
B
|
C
//
D
/
22
Which function overloads the // operator?
A
__div__()
B
_ceildiv__()
C
__floordiv__()
D
__truediv__()
23
_____ represents an entity in the real world with its identity and behaviour.
A
A method
B
An object
C
A class
D
An operator
24
____ is used to create an object.
A
class
B
constructor
C
User-defined functions
D
In-built functions
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()
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
26
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
27
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
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)
A
6
B
7
C
Error
D
0
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()
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
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)
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
31
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
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__))
A
12
B
52
C
13
D
60
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()
A
Exception is thrown
B
__main__
C
Demo
D
test
34
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
35
Which of the following is not a class method?
A
Non-static
B
Static
C
Bounded
D
unbounded solution
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)
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
37
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
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)
A
Error
B
Nothing is printed
C
__str__ called
D
__repr__ called
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)
A
__str__ called
B
__repr__ called
C
Error
D
Nothing is printed
40
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
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'))
A
Error as age isn’t defined
B
True
C
False
D
7
42
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
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__)
A
Exception is thrown
B
__main__
C
Nothing is displayed
D
Base class for all students
44
What does print(Test.__name__) display (assuming Test is the name of the class)?
A
()
B
Exception is thrown
C
Test
D
__main__
45
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
46
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
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()
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)
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()
A
invalid syntax for inheritance
B
Error because when object is created, argument must be passed
C
Nothing is printed
D
A 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?
A
A.__init__(self)
B
B.__init__(self)
C
A.__init__(B)
D
B.__init__(A)
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()
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
Submit Quiz
Back to All Quizzes