0% found this document useful (0 votes)
65 views6 pages

Python Model Question Paper

Uploaded by

no1sigmamale
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views6 pages

Python Model Question Paper

Uploaded by

no1sigmamale
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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([Link](), end=" ")

print('Boom!')

2.

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

while d > 3:

print([Link](), end=" ")

print('Boom!')

3.

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

while True:

print([Link](), end=" ")

print('Boom!')

4.

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

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

print([Link](), 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)[Link]([6,7])
b)[Link]([6,7])
c)A = A + [6,7]
d)[Link](-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 = [Link](dict2)

D) dict3 = [Link]()
[Link](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)[Link]()
c) [Link]()
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:
[Link](i)
b)for lst in lists:
for i in lst:
[Link](lst)
c)for lst in lists:
if keyword in lst:
[Link](lst)
d)for lst in lists:
if keyword in lst:
[Link](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

Common questions

Powered by AI

To merge two dictionaries without overwriting keys, you can use a method that allows keeping track of both sets of keys. The option in Source 1, 'dict3 = dict1.copy(); dict3.update(dict2)' merges dict2 into dict1 and handles overlapping keys by overwriting them in this case, but to prevent overwriting, each key can be handled manually or included in a list under the same key .

The XOR operation ('x^y') toggles bits that are different between x and y. In the loop, 'x = x^y' progressively accumulates the XOR result of the current x and y. For the termination condition 'while not x or not y', it ensures that the loop executes only while at least one of x or y is zero. The final printed results are obtained after specific bitwise manipulations leading to x being 3 and y reaching 0, exhibiting the properties of XOR in modifying binary states .

Python's memory management system uses reference counting, which tracks object references in memory. In the example with 'list1' and 'list2', since 'list2 = list1', both lists reference the same memory address. Thus, altering an element via 'list2[0] = 'Guava'' affects both, whereas 'list3 = list1[:]' creates a shallow copy with independent memory space, not affecting 'list1' when modified .

Using list comprehensions keeps with functional programming principles by minimizing side effects. '[ele for ele in reversed(lst)]' creates a new reversed list without modifying the original, adhering to functional programming ideologies by avoiding mutation of the original list .

If you try to pass a list object to a comparison method expecting individual elements, such as in 'if lst in b:', it will raise a 'TypeError' because a list is unhashable and cannot be directly checked for existence in a set which expects hashable and immutable objects .

The function can use a loop to iterate through each sublist, checking for the presence of a specific element using 'if keyword in lst'. Appending the original sublist when the keyword exists ensures correct filtering. It's critical to append the sublist not the element, to preserve the intended structure while meeting the filtering criteria .

Python allows list slicing 'lst[::-1]' to achieve a reversed list without altering the original, unlike 'lst.reverse()' which mutates it. Slicing respects functional programming paradigms by offering an efficient, clear syntax for obtaining modified forms, promoting immutability and thread safety, similar to slicing lists to add elements at specific indices .

The code snippet in option 1, 'd = {'me': 1, 'you': 2, 'we': 3} while len(d) > 3: print(d.popitem(), end=" ") print('Boom!')', will fail to execute the loop body because the condition 'len(d) > 3' is false from the start, given the initial length of dictionary 'd' is 3. Therefore, the loop is never entered, and the only output will be 'Boom!' .

Using assignment operations as part of the loop's condition can inadvertently alter loop behavior. For example, setting conditions like 'if x > 4' followed by an assignment 'x = 0' directly manipulates x, affecting subsequent conditional evaluations. This tight coupling between loop control variables and internal logic can lead to non-intuitive flows and termination paths that are difficult to debug .

The example shows a tuple, 'tup = ("Item_1", 0.5, name_lst)', containing a mutable list 'name_lst'. When 'name_lst.append("Vishal")' is called, the list within the tuple is modified because tuples don't encapsulate the immutability of their contained objects' contents. Only the structure of the tuple itself (the identity of the objects contained) is immutable .

You might also like