Q. 1

What is the output of the following ?

x = True
y = False
z = True
if z or y and x :
    print ("PythonEasy")
else:
    print ("Not in PythonEasy")

Options:

Explanation

Correct answer is : A

and has higher precedence than or so y and x will be evaluated first which results in False.
Then z (value False ) or False results in True.
So PythonEasy will be the output

Discuss It

Q. 2

What is the output of the following code ?

for i in range(0):
    print(i)

 

Options:

Explanation

Correct answer is : None

range(0) results in empty object

print(list(range(0))) -> []
 

Discuss It

Q. 3

What is the output of the following code ?

p = {2, -1, 5}
for a in p:
    print(a)

 

Options:

Explanation

Correct answer is : D

As p is set it will print the elements, but it can be in any order

Discuss It

Q. 4

What is the output of the following code ? 

p = {2, -1, 5}
for a in p.values():
    print(a)

 

Options:

Explanation

Correct answer is : D

AttributeError: 'set' object has no attribute 'values'

Discuss It

Q. 5

What is the output of the following code ? 

p = {0: 'x', 1: 'y', 2: 'z'}
for i in p.values():
    print(p[i])

 

Options:

Explanation

Correct answer is : D

p.values() results in dict_values(['x', 'y', 'z']) 

For the first iteration p[i] will be same as p['x].

As there is no key named 'x' for p, it will raise a KeyError: 'x'

Discuss It

Q. 6

What is the output of the following code ? 

p = {0: 'x', 1: 'y', 2: 'z'}
for i in p.items():
    print(i)

 

Options:

Explanation

Correct answer is : C

p.items() results in dict_items([(0, 'x'), (1, 'y'), (2, 'z')])

For each iteration i will be assigned to tuples (0, 'x') (1, 'y') and (2, 'z')

Discuss It

Q. 7

What is the output of the following code ?

p = {0: 'x', 1: 'y', 2: 'z'}
for i in p:
    print(i)

 

Options:

Explanation

Correct answer is : C

When dictionary used as iterator, for loop iterate over keys

Discuss It

Q. 8

What is the output of the following code ? 

p = 5678
for i in p:
    print(i)

 

Options:

Explanation

Correct answer is : D

TypeError: 'int' object is not iterable

Discuss It

Q. 9

What is the output of the following code ? 

p = "5678"
for i in p:
    print(i)

Options:

Explanation

Correct answer is : B

p = "5678" is string object so iterable

Discuss It

Q. 10

What is the output of the following code ?

set1 = {5, 6, 7}
for i in set1:
    print(set1.add(i))

 

Options:

Explanation

Correct answer is : D

set1.add(i) returns None, so the output will be None 3 times 

None
None
None

Discuss It
Submit Your Answers

Interview Questions

on
Control Flow

Tutorial

on
Control Flow

Programs

on
Control Flow
Click any Link
to navigate to certain page easily
Write a line to us
Your Email
Title
Description