Python Practice Questions 2
Python Practice Questions 2
1) Suppose list1 is [4,2,2,4,5,2,1,0]. Which of the following is correct syntax for the slicing operation?
A. print(list1[:-2])
B. print(list1[0])
C. print(list1[:2])
D. All of the mentioned
2) Assuming that my_string is defined, what will be the output of the following Python code snippet?
k= [print(i) for i in my_string if i not in "aeiou"]
A. Type Error
B. I=[9,1,2,6,5,7,8]
C. I=[1,2,6,5,7,8,9]
D. I=[1,2,6,5,9,7,8] (insert randomly at any position)
A. Nothing
B. GEEKSFORGEEKS
C. None of the others
D. geeksforgeeks
The order of logical operators is not,and,or.
I.e, b and c will be evaluated first as False and False, which will return False.
Then a or False will be evaluated→ True or False-->True;
>>>_4midable(5162)
A. It returns 516
B. It returns 5
C. It returns 2
D. It returns 516.2
5162//10-->516 → 516//10 → 51 → 51///10 → 5 → 5 < 10→ return 5
def makeQueue():
return ('queue',[])
def mysteryQueue(que):
return ('queue', que[1][1:])
A. isQueueEmpty
B. enqueue
C. dequeue
D. front
(11) Given the code below, what does running magic([x for x in range(1,4)], [x for x in range(1,4]) do?
def magic(lst1, lst2):
if (lst1 == []):
return []
elif(lst2 == []):
return []
else:
return [lst1[0] + lst2[-1] ] + magic(lst1[1:], lst2[:-1])
A. returns [3,2,1]
B. returns [4,4,4]
C. returns [3,2,1]
D. returns [1,2,3]
the first element of lst1 is added to the last element of lst 2 to produce: [1 + 3, 2+2, 3 + 1]
def f(value,values):
v=1
values[0]=44
>>>t=3
>>>v=[1,2,3]
>>>f(t,v)
>>>print(t,v[0])
A. 3 44
B. 3 1
C. 1 44
D. 1 1
elements = [0,1,2]
def incr(x):
return x+1
print(list(map(incr,elements)))
A. [1,2,3]
B. error
C. [0,1,2]
D. None of the mentioned
(16) The maximum value from the given list, lst below can be obtained by which of the following python
statements?
Assume max() and foldr() are defined as follows:
def foldr(combiner,base,lst):
if lst==[]:
return base
else:
return combiner(lst[0],foldr(combiner,base,lst[1:]))
def max(a,b):
if a>=b:
return a
else:
return b
lst=[11,12,13,14,15,16,17,18,19,20]
A. foldr(max,0,lst)
B. foldr(max,21,lst)
C. foldr(max(lst))
D. foldr(max,[],lst)
def mystery(lst):
if lst==[]:
return True
else:
return (lst[0]%2==0) and mystery(lst[1:])
A. 7
B. 5
C. 0
D. 3
This demonstrates referencing: where if one list is defined in terms of another, both lists will change
whenever one list is modified. Recall the ADTs used consist of tuples and lists.
A. 48
B. 14
C. None of the others
D. 64
when you assign a variable to a lambda function, you can then call that function with arguments just as
you would call any other function. You can even do a lambda function that takes no parameters but
returns some output.
def foo_bar(foo_list):
def fun_helper(y):
return len(y)
return [ x[:1] for x in foo_list if fun_helper(x) > 0 ]
This is just adding the first element of each item in the foo list to the final list that is returned. C from cat,
A from and, T from test.
A. list1 = list()
B. list1 = []
C. list1= list([1,2,3])
D. all of the mentioned
(24) What will be the output of the following Python code snippet?
test = {1:'A', 2:'B', 3:'C'}
test = {}
print(len(test))
A. An exception is thrown
B. 3
C. None
D. 0
(25) What will be the output of the following Python code snippet?
print([if i%2==0: i; else: i+1; for i in range(4)])
A. Error
B. [1,1,3,3]
C. [0,2,2,4]
D. None of the mentioned
(26) What will be the output of the following Python code snippet?
print([i+j for i in "abc" for j in "def"])
A. 12
B. 8
C. 24
D. 4
(28) What is the equivalent representation of the following list comprehension expression?
lst1=[3,17,11,9,7,2,19,17]
[2*x for x in lst1 if x>10]
A. ('[0] [1]',)
B. ('01')
C. [0] [1]
D. 01
When you print a string, the string quotes are not printed with it.
Technically D won't have any errors either, but C is the answer they were looking for.
A. [-4, 8]
B. 8
C. Error
D. Address of m
m is a map object so printing it will return the address of said object. Compare this to printing list(m).
A. Error
B. "a"
C. "A"
D. "B"
Option A:
list_1=0
for i in list_0:
if expr(i):
list_1.append(func(i))
Option B:
list_1=0
for i in list_0:
if expr(i):
list_1.append(func(i))
Option C
list_1=[]
for i in list_0:
if func(i):
list_1.append(expr(i))
Option D
list1=[]
for i in list_0:
if func(i):
list_1.append(i)
(36)The reduce(function, lst) function is used to apply a particular function passed in its argument to all of
the list elements mentioned in the sequence passed along. The reduce() method reduces the array to a
single value. The reduce() method executes a provided function for each value of the array (from left-to-
right). What is the output of the code shown?
import functools
m=functools.reduce(lambda x: x-3 in range(4, 10))
print(list(m))
A.. No output
B. Error
C. [1, 2, 3, 4, 5, 6, 7]
D. [1, 2, 3, 4, 5, 6]
Reduce works like foldl; the syntax is functools.reduce(function, sequence); the reduce is missing an
argument, also the lambda function would not work as it would pass two arguments to the lambda, where
only one was expected. The function being used should be a "combiner"; it needs to combine a minimum
of two elements into one.
x = '1234'
print(list(map(list, x)))
x=2
y = 10
x *= y * x + 1
a. 210
b. 42
c. 30
d. 21
a = True
b = False
c = False
if not a or b:
print (1)
elif not a or not b and c:
print (2)
elif not a or b or not b and a:
print (3)
else:
print (4)
A. 2
B. 3
C. 4
D. 1
a = 4.5
b=2
print (a//b)
A. 2.0
B. 2.25
C2
D. 2.2
(41) In Python, upper() is a built-in method (not a function) used for string handling. The upper() methods
returns the uppercased string from the given string. It converts all lowercase characters to uppercase.
What will be the output of the following Python code?
x = ['ab', 'cd']
print(list(map(lambda x: x.upper(), x)))
a. ['ab', 'cd']
b. ['AB', 'CD']
c. None of the mentioned
d. Error
(42) What would be the output of the last statement if the following were entered in python?
' '.join(a)
a. ['he she we']
b. 'heshewe'
c. 'he she we'
d. ['heshewe']
(45) What will be the output of the following Python code snippet?
a. [0, 2, 4]
b. [0, 1, 4]
c. error, ';' is not allowed
d. error, **+ is not a valid operator
a. ['heshewe']
b. 'he she we'
c. ['he she we']
d. 'heshewe'
a. dlroW olleH
b. Hello Worl
c. d
d. Error
(49) What will be the output of the following Python code snippet?
print([if i%2==0: i; else: i+1; for i in range(4)])
a. [0, 2, 2, 4]
b. Error
c. [1, 1, 3, 3]
d. None of the mentioned
a. h
b. o
c. hello
d. olleh
(51) Suppose list1 is [2, 33, 222, 14, 25], What is list1[:-1]?
a. 25
b. [ 2 5, 1 4, 2 2 2, 3 3, 2 ]
c. [ 2, 3 3, 2 2 2, 1 4 ]
d. Error
a. 0 2 1 3 2 4
b. 0 1 2 3 4 5
c. Error
d. 1 0 2 4 3 5
a. 1
b. 5
c. 25
d. 3
return foldr(helper,0,q)
a. 0
b. SyntaxError
c. 15.0
d. 30.0
a. Error
b. 5
c. 4
d. 6
(57) Assuming the following tagged ADT, is a representation for an empty instance of an
ADT: ('library', [])
What does the following function implement which takes this ADT as an argument?
def someFunc(aDataSet):
return aDataSet[1] == []
a. A selector that returns the second element of its argument
b. A predicate that determines whether its argument is empty or not
c. A constructor that returns a new structure
d. A mutator that changes the second element of its argument
(60) What condition argument would I pass to the function map_foo to check if an element in
the list is even?
a. ( lambda x: x % 2 == 0 )
b. ( lambda z: z / 2 == 0 )
c. def is_even( x ): return x / 2 == 0
d. I don't think that's possible
a. ['olleh']
b. ['hello']
c. ['llo']
d. ['h', 'e', 'l', 'l', 'o']
(63) Which python statement, when applied to lst = [1,2,3,4,5,6,7,8] would return the
following result? Assume that you have access to a predicate isOdd() which checks if a
given number is odd.
The result is: [2,6,10,14]
a. filter(isOdd,(map(lambda f: f * 2,lst)))
b. map(lambda f : f + 2,lst)
c. filter(lambda f : f+2, lst)
d. map(lambda f: f*2,(filter(isOdd, lst)))
a. [1, 4, 5]
b. [1, -2, -3]
c. Error
d. [-2, -3
(66) What is the output from Python for the following statement?
>>> list(map(lambda x: x[0], ["let","me","know"]))
(67) Python interprets 0O7 as the Octal (base 8) value of 7. What is the output of the following?
i=1
while True:
if i%0O7 ==0:
break
print(i)
i+=1
A. 1 2 3 4 5 6
B. None of these
C. 1 2 3 4 5 6 7
D. Error
A. [3,4,5,6,7]
B. [2,3,4,5,6]
C. [1,2,3,4,5]
D. [0,1,2,3,4]
How will you translate the following function to an anonymous lambda function?
def func(x):
return ''.join(x)
print('abcefd'.replace('cd','12'))
A. abcefd
B. ab1ef2
C. ab12ed2
D. ab1efd
(75) What would be the output of the last statement if the following were entered in python?
>>>lst=[6,21,15,12]
>>>list.insert(len(lst),13)
>>>lst
A. abc
B. a
C. bca
D. bc
(81) What will be the output of the following Python code snippet?
my_string= "hello world"
k=[(i.upper(),len(i)) for i in my_string]
print(k)
A. geeksforgeeks a a a a a a a a a a a a
B. None
C. Error
D. gaaaaaaaaaaaa
(83) What does the following function accomplish given a dictionary D, and value V?
def loopy(D,V):
for K in D:
if D[K]==V:
return K
(84) The any() function returns True if any item in an iterable is true, otherwise it returns False. Which of
the following functions accepts only integers as arguments?
A. chr()
B. ord()
C. any()
D. min()
(85) Which data structure is appropriate to store customers in a clinic for taking flu shots?
A. Priority Queue
B. List
C. Queue
D. Stack
A. Class
B. Tuples
C. Lists
D. Dictionary
numbers, strings, lists, tuples, dictionaries, files-- core data types in python
(88) Python interprets 2e-04 as 2*10**-4. What is the output of the following program?
A. 11.001199999999999
B. 12
C. 11
D. TypeError
A. ['ab', 'cd']
B. ['AB', 'CD']
C. None of the mentioned
D. Error
A. A
B. An exception is thrown
C. I
D. C
x= False
y= False
z=True
if not x or y:
print(1)
elif not x or not y and z:
print(2)
elif not x or y or not y and x:
print(3)
else:
print(4)
A. 1
B. 2
C. 3
D. 4
(93) What can you do to fix the following function that should return the elements which are odd?
Line 1. def oddlist(lst):
Line 2: if lst==[]:
Line 3: return []
Line 4: elif (lst[0]%2==0):
Line 5: return [lst[0]] + oddlist(lst[1:])
Line 6: else:
Line 7: return oddlist(lst[1:])
(94) What will be the output of the following Python code snippet?
a={}
a['a']=1
a['b']=[2,3,4]
print(a)
A. An exception is thrown
B. 6
C. 8
D. 7
(97) What will be the output of the following Python code snippet?
>>> a={1:"A", 2:"B", 3:"C"}
>>>del a
A. Error
B. 2
C. None of the mentioned
D. 1
(101) Suppose the rule of the party is that the participants who arrive later will leave earlier. Which data
structure is appropriate to store the participants?
A. Queue
B. Stack
C. Linked List
D. list
x=[12,34]
print(len("".join(list(map(int,x ) ) ) ) )
A. 4
B. None of the mentioned
C. Error
D. 2
A> 01
B. ('[0] [1]',)
C. ('01',)
D. [0] [1]
When you print a string, it does not print with the quotes (in IDLE anyway).
(106) What would be the output of the last statement if the following were entered in Python?
>>>lst=[6,21,15,12]
>>>lst.insert(len(lst),13)
>>>lst
A. [6,12,13,15,21]
B. [6,21,15,12,13]
C. [6,21,13,15,12]
D. None of the mentioned
x=['ab', 'cd']
print(len(map(list,x)))
(108) Suppose there is a list such that I=[2,3,4]. If we want to print this list in reverse order, which of the
following methods should be used?
A. list(reverse[(I)])
B. reverse(I)
C. print(I.reverse())
D. I[-1::-1]
lst.reverse() is the syntax, and it does not return anything; to print the list require a second line of code
after reversing it with I.reverse(). But slicing also works.
r1 = make_rational(1,2)
r2 = make_rational(1,3)
Which of the following python statements will subtract the two rational numbers r1 and r2?
A. rat_algebra(lambda : x-y, r1,r2)
B. rat_algebra(r1-r2, r1, r2)
C. rat_algebra (lambda : r1-r2, r1, r2)
D. rat_algebra( lambda x,y : x-y, r1, r2)
This is the only option with valid lambda syntax
(110) What arguments would you pass to filter_foo() to generate the list of prime numbers from
lst1==[3,4,5,6,7,8,9,10,11,12] ? Assume that you have access to the predicate isPrime(), which returns
True if the argument is a prime number and False otherwise.
A. filter_foo(isPrime, lst1])
B. filter_foo(isPrime(x), lst1)
C. filter_foo(isPrime(x), map(isPrime, lst1) )
D. filter_foo(lambda x: x%2 and x%x, lst1)
A. An exception is thrown
B. R
C. A
D. C
(112) What is the output of the code shown below?
f= lambda x: bool(x%2)
print(f(20),f(21))
A. False True
B. True True
C. True False
D. False False
Bool converts an object to a boolean value-- True or False. It always return True, unless the object is
empty ([], (), {} ), False, 0, or None. --W3Schools
A. [10,25,30,45]
B. [10,30]
C. [10]
D. [25,45]