Python Q&A

Python Q&A - Part5

179) What is encapsulation?

Answer: Encapsulation is packing of data and functions. It hides the implementation details of a class from other objects

180) What is polymorphism?

Answer: It is using an entity(operator, function or object) in different ways in different scenarios

181) What is method overriding?

Answer: The ability of child class to change the implementation of method provided by parent class

182) What is PYTHONHOME environment variable?

Answer: It is used to set the default location of the standard Python libraries

183) How will you convert a string to an object

Answer: eval(string)

184) What is the output of the following?

lst=[2,3,1]
lst.sort()
print(lst[2])

Answer: 3

185) What is the code to print the largest number of the following list

lst=[25,30,20]

Answer: print(max(lst))

186) What is monkey patching?

Answer: It is dynamic modification of a class or module at run-time

187) How do you create empty class?

Answer: 

class myclass:
pass

188) How do you create object of empty class?

Answer: x=myclass()

189) x=’abcd’

print(x[::-1])

Answer: dcba

190) Write code to check if ‘noon’ is palindrome

Answer:

x=’noon’
y=x[::-1]
if x==y:
print(“Palindrome”)
else:
print(“Not Palindrome”)

191) Write the code to find how many numbers are there in the string str = ‘Python1Test2’

Answer:

y=0
for x in str:
    if x.isnumeric():
        y=y+1
print(y)

192) What is a framework?

Answer: A framework is a code library that provides reusable code or extensions for common operations.

193) What is the output of following code?
def fn(x):
    if x == 0:
        return 1
   else:
      return x*fn(x-1)

print(fn(5))

Answer: 120

Explanation: (5*4*3*2*1)

194) Name some of the frameworks of Python

Answer: Flask, Django, Pyramid, Tornado

195) print(‘ Hello ‘.strip())

Answer: Hello

Explanation: Removes leading, trailing white spaces

196) What is the output of the following?

str=’hello’

print(str.swapcase())

Answer: HELLO

197) Add 3 before 4 in lst [1,2,4]

Answer: lst.insert(2,3)

198) How do you reverse a list

Answer: list.reverse()

199) Write code to sort alphabetically
str=’d e c a b’

Answer:

str=’d e c a b’
str1=[s.lower() for s in str.split()]
str1.sort()
for x in str1:
   print(x)

200) Are strings immutable?

Answer: Yes

201) What is Scikit-learn?

Answer: It is a library containing tools for machine learning

202) What is Pylint?

Answer: Pylint analyses code without running it. It checks for errors and tries to enforce a coding standard and style

203) With which version of Python is Pylint compatible?

Answer: Pylint is compatible with versions greater than 2.7.0

204) What is Pychecker?

Answer: It is a tool for finding bugs in python source code 

205) What is difference between Pylint and Pychecker?

Answer: Pylint checks for style issues while Pychecker don’t. Pylint does not import live modules while Pychecker does

206) What is a regular expression?

Answer: It is a special sequence of characters that help match or find other string or 

207) Which module support regular expression?

Answer: re module

208) What is the output of the following code?

import re
str=’Python books’
x=re.search(‘^P’,str)
if x:
   print(“Starts with P”)
else:
    print(“Don’t start with P”)

Answer: Starts with P

209) What is output of the following code?
import re
str=’Books on Python’
x=re.findall(‘Python$’,str)
if x:
   print(“Ends with Python”)
else:
   print(“Don’t end with Python”)

Answer: Ends with Python

210) What is the output of the following?

import re
str = “Office opens at AM”
#x=re.findall(‘AM’,str)
x=re.sub(“AM”, “9AM”, str)
print(x)

Answer: Office opens at 9AM

211) What does \b mean with respect to regular expression:

Answer: Returns a match where the specified characters are at the beginning or at the end of a word

212) What is the output of the following?

import re
str = “Python is the trend”
x = re.search(r”\btr”, str)
print(x.span())

Answer: (14, 16)
Explanation:

(This means that the letters “tr” exists at the beginning of the word in the string str between positions 14 and 16)
(\b – Returns a match where the specified characters are at the beginning or at the end of a word)

213) What is output of the following?

import re
str = “Python is the trend”
x = re.search(r”\btr\w+”, str)
print(x.group())

Answer: trend

Explanation: (This returns the word where the letters exist)