Python Q&A

Python Q&A - Part6

214) What is JSON?

Answer: JSON is used to store and exchange data

215) What is full form of JSON?

Answer: JavaScript object notation

216) How can you use JSON in Python?

Answer: Python has a built-in package called json

217) What is the output of the following?
import json
x = ‘{ “1”:”a”, “2”:”b”, “3”:”c”}’
y = json.loads(x)

print(y[“2”])

Answer: b

218) What method is used to parse JSON string?

Answer: loads() method

219) What is the output of the following?

from datetime import datetime
dt = “Jan 01 2021”
dt1 = datetime.strptime(dt, ‘%b %d %Y’)
print(dt1)

Answer: 2021-01-01 00:00:00

220) What is PIP?

Answer: PIP is a package manager for Python packages

221) What is a package?

Answer: Package contains all files needed for module

222) What are modules?

Answer: Modules are Python code libraries

223) What does try block do?

Answer: Test a block of code for errors

224) What does except block do?

Answer: Handle the error

225) What does finally block do?

Answer: Executes the code irrespective of error

226) What is the output of the following code?
try:
x=1
y=0
print(x/y)
except:
print(“error”)
finally:
print(“End”)

Answer:

error
End

227) What is the output of the following?

def fn(str):
    if str.isalnum():
       print(“yes”)
   else:
     print(“no”)
fn(‘a12’)

Answer: yes

228) What is keyword to throw exception?

Answer: raise

229) What is the output of following code?

lst=[‘a’,’b’,’c’]
for x,y in enumerate(lst):
    print(x,y)

Answer:

0 a
1 b
2 c

230) What does enumerate() method do?

Answer: enumerate() returns element and adds index

231) What is frozenset()?

Answer: It is immutable version of Python “Set” object. It cannot be changed while “Set” can be changed

232) What is exec() method”?

Answer: It executes dynamically created code

233) What is the output of the following?

x=’print(“hello”)’
exec(x)

Answer: hello

234) What is the output of the following?

class myclass:
a=1
b=2
x=myclass()
print(hasattr(myclass,’c’))

Answer: false

235) What is the output of the following?

x=’hello’
print(list(x))

Answer: [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]

236) What is the output of the following?

str1 = [‘x’,1]
str2 = [‘y’,2]
x = zip(str1, str2)
y=set(x)
print(y)

Answer: {(1, 2), (‘x’, ‘y’)}

237) What is the output of the following?

lst=[ ]
print(any(lst))

Answer: false

238) What is the output of the following?

print(round(1))
print(round(1.4))
print(round(1.5))
print(round(1.648,2))
print(round(1.6468,3))

Answer:

1
1
2
1.65
1.647

239) What is output of the following code?

x=6789
y=0
while x>0:
y=y*10+x%10
x=x//10
print(str(y))

Answer: 9876

240) What is index -1 with respect to a list

Answer: It is the last index of the list

241) What is the output of the following?

lst=[1,2,3,4,5,6]
lst.remove(6)
print(lst)

Answer: [1,2,3,4,5]

Explanation: Remove takes the item value

lst=[1,2,3,4,5,6]
lst.pop(5)
print(lst)

Answer: [1,2,3,4,5]

Explanation: pop takes the item index

del lst

Answer: Deletes the whole list

242) What is the output of the following?

for x in range(4):
     for y in range(x):
        print(x,end=” “)
     print(“\n”)

Answer:

1
22
333

243) What is output of the following?

x=range(1,4)
for y in x:
    print(y)

Answer:

1
2
3