Python Q&A
Python Q&A - Part7
244) What is constructor used for?
Answer: Constructor is used to create object of class
245) What is the output of the following?
class myclass:
x=1
def fn(self):
print(“in function”)
p=myclass()
print(p.x)
p.fn()
Answer:
1
In function
246) What is the output of the following?
class myclass:
x=1
def __init__(self,x):
self.x=x
def fn(self):
print(self.x)
p=myclass(5)
p.fn()
Answer: 5
247) Which function is called automatically when
an object of a class is being created
Answer: __init__()
248) What is the “self” parameter?
Answer: self parameter is reference to the current instance of the class. It is used to access the members of the class. It should be first parameter of any function in the class
249) What is the output of the following code?
class myclass:
x=1
def fn(self):
print(self.x)
p=myclass()
p.x=5
p.fn()
Answer: 5
250) What is the output of following code?
class parentclass:
x=1
def __init__(self,x):
return x
class childclass(parentclass):
x=3
def __init__(self,x):
self.x=super().x
c=childclass(2)
print(c.x)
Answer: 1
Explanation: Child class object should get the x value from child class, but the super() is accessing the parentclass x value and assigning it to the child class variable x. Therefore print(c.x) prints 1
251) How to modify the following code to execute it?
class myclass:
Answer:
class myclass:
pass
252) What is super() function?
Answer: Super() function returns object of parent class. Parents properties and methods can be accessed through super() function
253) What are special functions?
Answer: The class functions that begin with double underscore are called special functions
254) What is an object?
Answer: An object is an instance of a class
255) What is encapsulation?
Answer: Wrapping data and methods that work on data within one unit. This ensures the object’s variables can be changed only through methods
256) What is hasattr()?
Answer: hasattr() returns true if the object has the attribute otherwise false
257) What is getattr()?
Answer: getattr() returns the value of the attribute
258) What is the output of the following code?
class myclass:
x=1
p=myclass()
print(getattr(myclass,’x’))
Answer: 1
259) What is the output of the following code?
class myclass:
x=1
p=myclass()
print(hasattr(myclass,’x’))
print(hasattr(myclass,’y’))
Answer:
True
False
260) What is the a name space?
Answer: A name space is a mapping from names to objects
261) What is the output of the following?
Answer:
4
2
2
3
262) What is output of the following code?
class myclass:
def __init__(self):
self._x=1
p=myclass()
pirnt(p._x)
Answer: Error. Protected members can be accessed inside the class only
263) What is operator overloading?
Answer: Assignment of more than one function to a particular operator. e.g.’+’ can add numbers and concatenate strings
264) What are Special methods?
Answer: Methods that begin and end with double underscore
265) What is an abstract method?
Answer: A method that has a declaration but no implementation
266) What is an abstract class?
Answer: A class which contains one or more abstract methods is called abstract class
267) What does re.purge() do?
Answer: It clears the cache
268) What is the output of the following code?
import re
print(re.split(‘\s’,’This is Python test’,1))
Answer: [‘This’, ‘is Python test’]
print(re.split(‘\s’,’This is Python test’,2))
Answer: [‘This’, ‘is’, ‘Python test’]
269) What is the output of the following?
import re
print(int(re.search(r’\d+’, ‘Retrieve only number 1234567890’)[0]))
Answer: 1234567890
270) What is output of the following?
import re
print(re.findall(‘\d+’,’Retrieve only number 1234567890’))
Answer: [‘1234567890’]
271) What is the output of the following?
import re
print(int(re.search(r’\d+’, ‘Retrieve only number 1234567890’).group(0)))
Answer: 1234567890