0% found this document useful (0 votes)
49 views

Test Python

Uploaded by

no1sigmamale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Test Python

Uploaded by

no1sigmamale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1) Which of the following code snippets will have its only output as “Boom!

” after
running?

1.
d = {'me': 1, 'you': 2, 'we': 3}

while len(d) > 3:

print(d.popitem(), end=" ")

print('Boom!')

2.

d = {'me': 1, 'you': 2, 'we': 3}

while d > 3:

print(d.popitem(), end=" ")

print('Boom!')

3.

d = {'me': 1, 'you': 2, 'we': 3}

while True:

print(d.popitem(), end=" ")

print('Boom!')

4.

d = {'me': 1, 'you': 2, 'we': 3}

while sum([d[e] for e in d])<len(d):

print(d.popitem(), end=" ")

print('Boom!')

a)1
b)2
c)3
d)4

answer- a d

2)We have a list A = [1,2,3,4,5] we want to add 6 and 7 to the list.

Which of the following will give the desired output.

A = [1,2,3,4,5,6,7]

a)A.append([6,7])
b)A.extend([6,7])
c)A = A + [6,7]
d)A.insert(-1,[6,7])

answer- b c

3)What will be the output of the following code?

a = [50,50,60,70,70,70]
b = set(a)
C = [50,70,50,60,40,10,30,30]

def test(lst):
if lst in b:
return 1
else:
return 0

for i in filter(test, c):


print(i,end=" ")

a)50 60 70
b)50 70 50 60
c)50 50 60 70 70 70
d)50 60 70 70 70

answer- b

4)What would be the values of x, y, and z after the following code is run?

x = 0
y = 3
z = 0
while not x or not y:
x = x^y
y -= 1
z &= x
print(x, end=" ")
print(y, end=" ")
print(z, end=" ")

a)2 1 0
b)3 2 0
c)3 0 1
d)1 2 0

answer- b

5)Predict the output of the following code as we are trying to append to an


immutable tuple:

name_lst = ["Vijay", "Vickey"]


tup = ("Item_1", 0.5, name_lst)
name_lst.append("Vishal")
print(tup)
a)(“Item_1”, 0.5, name_lst)
b) (“Item_1”, 0.5, [“Vijay”, “Vickey”])
c) (“Item_1”, 0.5, [“Vijay”, “Vickey”, “Vishal”])
d)Error

answer- c

6)What will be the value of x at the end of each iteration in the given program if
executed?

x = 2
for i in range(0, 4):
if x%2:
x = x*2
else:
x = x+1

a) 3 -> 4 -> 7 -> 14


b)11 -> 12 -> 13 -> 14
c)4 -> 3 -> 6 -> 7 -> 14
d)3 -> 6 -> 7 -> 14

answer- d

7)Consider the following dictionaries:

dict1 = {'ten': 10, 'twenty':20, 'thirty': 30}


dict2 = {'thirty': 30, 'fifty': 50, 'sixty': 60}
Which of the following is the correct way of merging both dictionaries together?

A) dict3 = {dict1, dict2}

B) dict3 = dict1 + dict2

C) dict3 = dict1.merge(dict2)

D) dict3 = dict1.copy()
dict3.update(dict2)

answer- d

8) Which of the options would separate a string input_string on the first 2


occurrences of the letter “e”?

a)'e'.split(input_string, 2)
b) input_string.split('e', 2)
c)'e'.split(input_string, maxsplit=2)
d)input_string.split('e', maxsplit=2)

answer- b d

9)The physical strength of a candidate is given in the form of an integer.


According to observations in the past, it's observed that Male candidates generally
have strength greater than 70, and Female candidates have strength lesser than 70.
Complete the following code snippet which predicts whether the candidate is Male or
Female.

strength = 20
if (???):
print("Male")
else:
print("Female")

Which of the options can be placed at ??? to get the required output if the
strength has no limit of value (no upper limit)?

a)strength<70
b)strength>70
c)strength>100
d)strength<100

answer- b

10)Which of the options are correct to get a new_list having elements of lst in
reversed order.

def get_reverse(lst):
new_lst = _____________
return new_lst

a) [ele for ele in reversed(lst)]


b)lst.reverse()
c) lst.reversed()
d)lst[::-1]

answer- a d

11)Which of the following code snippets is the correct way to access the value of
the "history" subject from the dictionary given below?
sampleDict = { "class":{ "student":{ "name":"Mike", "marks":{ "physics":70,
"history":80 } } } }

a)sampleDict['class']['student']['marks']['history']
b) sampleDict['class'][0]['marks']['history']
c)sampleDict['class']['student']['marks'][1]
d)None of these

answer- a

12)Which of the options adds the elements 'Dhoni' and 'Virat' to the end of the
given list?

players = ['Jadeja','Rahul','Rohit'].

So, the resultant list will be players = ['Jadeja','Rahul','Rohit','Dhoni','Virat']

a)players+='Dhoni Virat'
b)players[len(players):] = ['Dhoni', 'Virat']
c)players[-1:] = ['Dhoni', 'Virat']
d)players+=['Dhoni', 'Virat']
answer- b d

13)In the given code snippet, the code should return each sublist in the output
only if the element "Data" is present in the sublist. What will be the appropriate
code block from the options to complete the function get_sublist() such that it
returns the output as expected.

Input: lists = [["Data", 10, "Science"],["Python", 12, "Data"],["Machine", 60,


"Learning"],["Python", 5, "machine"]]
def get_sublist(lists, keyword):

sublists = [ ]

__________________

return sublists

lists = get_sublist(lists, 'Data')


print(lists)

Expected Output: [['Data', 10, 'Science'], ['Python', 12, 'Data']]

Options are:
a)for lst in lists:
for i in lst:
sublists.append(i)
b)for lst in lists:
for i in lst:
sublists.append(lst)
c)for lst in lists:
if keyword in lst:
sublists.append(lst)
d)for lst in lists:
if keyword in lst:
sublists.append(keyword)

answer- c

14)You have been given the following piece of code. Assume that x has already been
declared.

if x > 2:
x = x*2
if x > 4:
x = 0
print(x)

Select the correct statement regarding this code.

a)Output will always be equal to 0


b)If x > 2 and x < 4, final value of x will be double the initial value
c)For x in 0 < x < 4 the output will always be 0
d) For x > 2, output will be equal to 0

answer- d

15)What is the output of the following code snippet?

list1 = ['Apple', 'Berry', 'Cherry', 'Papaya']


list2 = list1
list3 = list1[:]

list2[0] = 'Guava'
list3[1] = 'Kiwi'

total = 0
for ls in (list1, list2, list3):
if ls[0] == 'Guava':
total += 1

if ls[1] == 'Kiwi':
total += 20

print (total, list3[0])

a)63 Guava
b)22 Guava
c)22 Apple
d)63 Apple

answer- c

You might also like