_________________________________________________________
Q.1. In python, a class is created by ____ keyword, and to create ____ of the class, we will have to use constructor of class.
1.
2.
3.
4.
ANSWER :- (2) A class is a code template for creating objects. Objects have member variable. In python a class is created by the keyword class and to create object it uses constructor of class.
_________________________________________________________
Q.2. 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.
2.
3.
4.
ANSWER :- (1) Here it will give an error when program is run. as here _init_ should return none.
_________________________________________________________
Q.3. 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.
2.
3.
4.
ANSWER :- (1) As given in code value assigned to self.b i.e 20 will be printed when program is run.
_________________________________________________________
Q.4. Private method in python starts with ____ while protected methods starts with ____ .
1.
2.
3.
4.
ANSWER :- (2) The double underscore __ prefixed to a variable makes it private. It gives a strong suggestion not to touch it from outside the class. while convention to make an instance variable protected is to add a prefix _ (single underscore) to it.
_________________________________________________________
Q.5. 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.
2.
3.
4.
ANSWER :- (1) Here it will return an error as command is given to open file.txt which is not save in directory or will not be fined when executed. while code is saved in file code.py as given.
_________________________________________________________
FOR ANY DOUBTS PLEASE COMMENT...
_________________________________________________________
Comments
Post a Comment