Programming for Noobs
PYTHON Quiz 8 Answers
Disclaimer-
This article only attempts to discover some questions that can be generated
in PYTHON for Engineering with the answer to all these questions. There can be
some errors to these answers. If you find any error then please
do write to us.
Q1. In python, a class is created by ____ keyword, and to create ____ of
the class, we will have to use constructor of class.
1.def, file
2.class, object
3.self, .py file
4.None of these
Reason: In python class is created by
keyword ‘class’ and to create’ object ‘of the class and we will use to
construct to class
Programming for Noobs
.
Programming for Noobs
Q2. What will be the output of the following python code?
class Roll:
def __init__(self, id):
self.id = id
id = 231
return id
val = Roll(321)
print (val.id)
1.TypeError
2.231
3.321
4.None of these
Reason: The program give the type
error, Because Roll takes no argument.
In this program constructor give the lines Val=Roll(321) is incorrect
Programming for Noobs
Programming for Noobs
Q3. What will be the output of following python code?
class X:
def __init__(self):
self.a = 10
self._b = 20
self.b = 20
def getB(self):
return self.b
x = X()
x._b = 60
print(x.getB())
1.20
2.60
3.Error
4.Program runs but does not print anything
Reason: The program has give attribute
error
Because ‘X’ object has no attribute ‘b’
Programming for Noobs
Programming for Noobs
Q4. Private method in python starts with ____ while protected methods starts
with ____ .
1.#, @
2.double underscore, single underscore
3.single underscore, double underscore
4.None of these
Reason: In a python private method start with double underscore and while start
protected with single underscore,
Programming for Noobs
Programming for Noobs
Q5. What will be the
output of the following code. Code is saved in a file named 'code.py' :
f =
open('file.txt')
f.readlines()
print(f.name)
print ( f.closed )
f.close()
print ( f.closed )
1.Error in code
2.true
false
true
3.code.py
False
True
4.file.txt
False
True
Reason: Reason.file.text is not found because the python
only open directory or file own libraries and files
Programming for Noobs
Please subscribe to our channel
0 Comments