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

Python Practice Questions 2

The document contains multiple choice questions about Python concepts like lists, dictionaries, functions, loops, and lambda functions. It provides the questions, possible answers for each, and in some cases additional explanations. For example, question 1 asks about list slicing syntax, question 2 analyzes a list comprehension, and question 9 walks through a recursive function that returns the last digit of a number.

Uploaded by

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

Python Practice Questions 2

The document contains multiple choice questions about Python concepts like lists, dictionaries, functions, loops, and lambda functions. It provides the questions, possible answers for each, and in some cases additional explanations. For example, question 1 asks about list slicing syntax, question 2 analyzes a list comprehension, and question 9 walks through a recursive function that returns the last digit of a number.

Uploaded by

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

For more questions, search online for Python multiple choice questions

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. prints all characters of my_string that aren't vowels


B. prints only on executing print(k)
C. prints all the consonants in my_string
D. prints all the vowels in my_string
C isn't correct as there can be characters such as numbers, or symbols, which are not vowels but are not
consonants either.

3). What is the output of the following program:


print("{0:.2}".format(1.0/3))

4). What is the output of the following code:


>>>I= [1,2,6,7,8]
>>>I.insert(9)

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)

5) Suppose list1 is [2445,133,12454,123]. What is max(list1)?


A. 2445
B. 133
C. 12454
D. 123

6) What is the output of the following program:


i=0
while i <5:
print(i)
i+=1
if i==3:
break
else:
print(0)
A. None of the others
B. 0 1 2
C. 0 1 2 0
D. Error
The code would print out 0 0 1 0 2

7) What will be displayed by the following code?


a=True
b=False
c=False
if a or b and c:
print("GEEKSFORGEEKS")
else:
print("geeksforgeeks")

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;

8) What will be the output of the following Python code snippet?


>>>test= {1:'A', 2:'B', 3:'C'}
>>>del test[1]
>>>test[1]='D'
>>>del test[2]
>>>print(len(test))

A. Error as the key-value pair of 1:'A' is already deleted


B. 2
C. 0
D. 1

9) What does the following function accomplish when called as follows?


def _4midable(d):
if d<10:
return d
else:
return _4midable(d//10)

>>>_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

10) Using the following definition for a queue,

def makeQueue():
return ('queue',[])

What operation does the following function implement?

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]

(12) Which data structures are immutable?


A. Strings and Tuples
B. Lists and Dictionaries
C. Lists and Strings
D. Tuples and Lists

(13) What will be displayed by the following code?

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

(14) Which of these functional programming methods use lambda?


A. append()
B. lookup()
C. sort()
D. map()

(15) What will be the output of the following Python code?

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)

(17) What will be the output of the following Python code?


x='abcd'
print(list(map(list,x)))

A. [ ['a'], ['b'], ['c'], ['d'] ]


B. ['abcd']
C. ['a','b','c','d']
D. None of the mentioned

(18) What does the function mystery do?

def mystery(lst):
if lst==[]:
return True
else:
return (lst[0]%2==0) and mystery(lst[1:])

A. mystery checks if all the elements in the list are divisible by 2


B. mystery checks if all the elements excluding the first is even
C. mystery is a recursive function that checks if the number of elements is even
D. mystery is an iterative function that checks if the number of elements in the list is even

(19) What is returned from the stack expression below?


>>>s1 = makeStack()
>>>push(s1,3)
>>>push(s1,5)
>>>s2=s1
>>>push(s1,7)
>>>top(s2)

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.

(20) What is the output of the following program:


y=8
z=lambda x:x*y
print(z(6))

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.

(21) What is the result of running :


foo_bar(["cat","and","test"])
Given:

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 ]

A. ['c', 'a', 't']


B. Array expected but given element
C. ['ca', 'an', 'te']
D. Index out of range error

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.

(22) Given the following python code:


def find_phnumber(name,lst):
if name in lst:
return lst[name]
phone_list={"Anna":1376432, "Beth":4747183, "Char":3762155, "Dean":6678784, "Edge":9426731}

What would be the output of the following:


>>>find_phnumber("Dean", phone_list)

(23) Which of the following commands will create a list?

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

This is not valid Python syntax for a list comprehension.

(26) What will be the output of the following Python code snippet?
print([i+j for i in "abc" for j in "def"])

A. ["da", "ea", "fa", "db", "eb, "fb", "dc", "ec", "fc"]


B. [ ["ad", "bd", "cd"], ["ae", "be", "ce"], ["af, "bf", "cf"] ]
C. ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
D. [ ["da", "db", "dc"], ["ea", "eb", "ec"], ["fa", "fb", "fc"]
(27) What will be displayed by the following code?
r = lambda q: q*2
s = lambda q: q*3
x=2
x =r(x)
x=s(x)
x=r(x)
print(x)

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. map ( lambda x: 2*x , filter ( lambda x: x > 10, lst1 ) )


B. filter(lambda x: (2*x>10), lst1)
C. map(lambda x: (2*x)>10, lst1)
D. filter(lambda x: 2*x, map (lambda x: x>10, lst1))

(29) What will be the output of the following Python code?


x= [[0], [1]]
print(("".join(list(map(str,x))),))

A. ('[0] [1]',)
B. ('01')
C. [0] [1]
D. 01

When you print a string, the string quotes are not printed with it.

(30) What does running sumList([x for x in range(1,4)]) produce?


def sumList(lst):
if (lst == []):
return 0
else:
return lst[0] + sumList(lst[1:])

A. Error with syntax


B. returns the list [6,3,1]
C. returns 0
D. returns the number 6

(31) Consider the following list as an input:


numbers = [1,2,3]
map() allows you to apply a function to every item of an iterable. Which of the following programs won't
have an error?

A. map(lambda x: return x, numbers)


B. list(map(lambda x: [x]%2==0, numbers))
C. list(map(lambda x: x, numbers))
D. map(lambda x: x, numbers))

Technically D won't have any errors either, but C is the answer they were looking for.

(32) What will be the output of the following Python code?


x= ['ab', 'cd']
print(len(list(map(list,x))))

A. None of the mentioned


B. Error
C. 4
D. 2

(33) What is the output of the code shown below?


L = [-2,4]
m = map(lambda x: x*2, L)
print(m)

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).

(34) What is the output of the following segment:


chr(ord("A"))

A. Error
B. "a"
C. "A"
D. "B"

(35) Which of the following is the correct expansion of:


list_1 = [expr(i) for i in list_0 if func(i)] ?

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.

(37) What will be the output of the following Python code?

x = '1234'
print(list(map(list, x)))

a. [[1], [2], [3], [4]]


b. [1234]
c. [1, 2, 3, 4]
d. None of the mentioned

(38) What is output of following code

x=2
y = 10
x *= y * x + 1

a. 210
b. 42
c. 30
d. 21

(39)What will be displayed by the following code?

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

(40) What will be displayed by the following code?

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?

>>> lst = [2,6,10,4,12]


>>> lst = lst.sort()
>>> lst
a. Nothing
b. [2, 4, 6, 10, 12]
c. [2, 6, 10, 4, 12]
d. [12, 10, 6, 4, 2

(43) To shuffle the list(say list1) what function do we use?


a. random.shuffleList(list1)
b. shuffle(list1)
c. random.shuffle(list1)
d. list1.shuffle()

(44) What is output for a = ['he', 'she', 'we'] given

' '.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?

x = [i**+2 for i in range(3)]; print(x);

a. [0, 2, 4]
b. [0, 1, 4]
c. error, ';' is not allowed
d. error, **+ is not a valid operator

(46) What is output for


a = ['he', 'she', 'we']
''.join(a)

a. ['heshewe']
b. 'he she we'
c. ['he she we']
d. 'heshewe'

(47) What does the following code do?


return[(x,y) for x,y in zip(range(1,4),[4,5,6])]

a. returns [(1, 4), (1, 5), (1, 6), (2, 4),(2,5),(2,6),(3,4),(3,5),(3,6)]


b. Syntax Error!
c. returns [((1,2,3),(4,5,6))]
d. returns [(1,4), (2,5), (3,6)]

(48) What is the output of the following program :


print ("Hello World"[::-1])

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

(50) What will be the output of the following Python code?


>>> str1 = 'hello'
>>> str2 = ','
>>> str3 = 'world'
>>> str1[-1:]

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

(52) What is the output of the following program:


i=0
while i < 3:
print(i)
i+=1
print(i+1)

a. 0 2 1 3 2 4
b. 0 1 2 3 4 5
c. Error
d. 1 0 2 4 3 5

(53) Suppose list1 is [3, 5, 25, 1, 3], what is min(list1)?

a. 1
b. 5
c. 25
d. 3

(54) What does this mystery function do to a list?


def mystery(q):
def helper(a,b):
return a + b

return foldr(helper,0,q)

a. inserts an element to a list


b. returns 0
c. does nothing
d. sums a list

(55) What would be the output of the following code snippet?


(lambda x: (x + 3) * 5 / 2)(3)

a. 0
b. SyntaxError
c. 15.0
d. 30.0

(56) What will be the output of the following Python code?


x = [12, 34]
print(len(' '.join(list(map(int, x)))))

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

(58) What will be the output of the following Python code?


x = ['ab', 'cd']
print(len(map(list, x)))

a. None of the mentioned


b. [2, 2]
c. 2
d. 4

(59) The components of an ADT are

a. strings and integers as defined in the parameters for the ADT


b. a representation and a set of functions that use the representation
c. lists and tuples for the constructors and selectors/accessors of the ADT
d. for and while loops as defined for the selectors/accessors of the ADT

(60) What condition argument would I pass to the function map_foo to check if an element in
the list is even?

def map_foo(f, foo_list, condition):


return [ f(x) for x in foo_list if condition(x) ]

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

(61) Given the following python code


def sum(start, stop, term, nex):
if start > stop:
return 0
else:
return term(start) + sum(nex(start), stop, term, nex)
Which of the following will produce a sum of the series 2^3 + 5^3 + 8^3 + 11^3

a. sum(2, 11, lambda n: n*3, lambda n: n+3)


b. sum(lambda n: n*3, 2, lambda n: n+3, 11)
c. sum(lambda n: n**3, 2, lambda n: n+3, 11)
d. sum(2, 11, lambda n: n**3, lambda n: n+3)

(62) What is the output when we execute list("hello")?

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)))

(64) What is the result of this expression?


list(map(lambda x : (x**2) - (x/2), [1,2,3,4,5]))

a. [1.5, 3.0, 4.5, 6.0, 7.5]


b. [0.5, 3.0, 7.5, 14.0, 22.5]
c. [2, 3, 5, 6, 8]
d. Error

(65) What is the output of the code shown?

l = [1, -2, -3, 4, 5]


def f1(x):
return x<2
m1 = filter(f1, l)
print(list(m1))

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"]))

a. ['l', 'm', 'k']


b. Error
c. "let"
d. "lmk

(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

(68) What does the following expression return?


map(lambda x: x+1, [x-1 for x in [1,2,3,4,5] ] )

A. [3,4,5,6,7]
B. [2,3,4,5,6]
C. [1,2,3,4,5]
D. [0,1,2,3,4]

(69) What will be the output of the following Python expression?


round(4.5676,2)
A. 4.56
B. 4.6
C. 4.5
D. 4.57

(70) Consider the following list as an input:


x=["A","M","A","Z","I',"N","G"]

How will you translate the following function to an anonymous lambda function?

def func(x):
return ''.join(x)

A. lambda x: return ','.join(x)


B. lambda x: ''.join(x)
C. lambda x: ','.join(x)
D. lambda x: ''.join('x')

(71) Consider the following list as an input:


numbers = [1,2,3]

Which of the following would produce the result: [2]

A. list(filter(lambda x: x>1, numbers))


B. list(filter(lambda x: (x+2) *3/3%3 ==0, numbers))
C. list(filter(lambda x: 2,numbers))
D. list(filter(lambda x: x%2 ==0, numbers))

(72) What is the output of the following program?


T= tuple('geeks')
a,b,c,d,e=T
b=c='*'
T=(a,b,c,d,e)
print(T)

A. ('geeks', '*', '*')


B. ('g", 'e', 'e', 'k', 's')
C. KeyError
D. ('g', '*', '*', 'k', 's')
(73) What is the output of the following progam:

print('abcefd'.replace('cd','12'))

A. abcefd
B. ab1ef2
C. ab12ed2
D. ab1efd

(74) Which of the following functions is a built-in function in python?


A. print()
B. factorial()
C. seed()
D. sqrt()

(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. [6,12, 13, 15, 21]


B. [6, 21, 15, 12, 13]
C. [6, 21, 13, 15, 12]
D. [6, 21, 15, 13, 12]

(76) What would be the output of the following statement?


>>> [x for x in [10,25,30,45] if x %6==0]

A. [10, 25, 30, 45]


B. [10, 30]
C. [10]
D. [25, 45]

(77) What arithmetic operators cannot be used with strings?


A. -
B. *
C. All of the mentioned
D. +
(78) What will be the output of the following code: print( type( type( () ) ) )
A. 0
B. < class 'type' >
C. < class 'tuple' >
D. None

(79) What will be the output of the following Python statement?


>>> "a" + "bc"

A. abc
B. a
C. bca
D. bc

(80) The output of executing string.ascii_letters() can also be achieved by:


Assume that the string module has already been imported
Hint: string.ascii_letters returns the alphabets in upper and lowercase
A. string.lowercase_string.uppercase
B. string.ascii_lowercase_string.digits
C. string.letters
D. string.ascii_lowercase +string.ascii_uppercase

(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. None of the mentioned


B. [('H', 1), ('E', 1), ('L', 1), ('L', 1), ('O', 1), (' ', 1), ('W', 1), ('O', 1), ('R', 1), ('L', 1), ('D', 1)]
C. [ ('HELLO',5), ('WORLD',5)]
D. [ (' HELLO WORLD', 11)]

(82) What is the output of the following?


my_string = 'geeksforgeeks'
for i in range(len(my_string)):
print(my_string)
my_string='a'

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

A. Sorts the dictionary by the K values


B. returns the value V, associate with key K
C. returns the key K, associated with value V
D. sorts the dictionary by K, then V values

(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

(86) Which of these is not a core data type?

A. Class
B. Tuples
C. Lists
D. Dictionary

numbers, strings, lists, tuples, dictionaries, files-- core data types in python

(87) Suppose listExample is ['h','e','l','l','o'], what is len(listExample)?


A. Error
B. None
C. 4
D. 5

(88) Python interprets 2e-04 as 2*10**-4. What is the output of the following program?

T=(2e-04, True, False, 8, 1.001, True)


val=0
for x in T:
val+=int(x)
print(val)

A. 11.001199999999999
B. 12
C. 11
D. TypeError

(89) What will be the output of the following Python code?


def to_upper(k):
k.upper()
x=['ab','cd']
print(list(map(to_upper,x)))

A. ['ab', 'cd']
B. ['AB', 'CD']
C. None of the mentioned
D. Error

(90) Given L = [1,23,'hello',1], what data type is L?


A. List
B. Array
C. Dictionary
D. Tuple

(91) Given the following, what gets printed?

names1=['Amir', 'Barry', 'Chales', 'Dao']


names2=[name.lower() for name in names1]
print(names2[2][0].upper())

A. A
B. An exception is thrown
C. I
D. C

(92) Given the following, state what gets printed?

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:])

A. Change line 6 to return [lst[0]] + oddlist(lst[1:])


B. Change line 5 to return [lst[1]] + oddlist(lst[1:])
C. Switch line 7 with line 5
D. Change line 4 to elif (lst[0]%1==0:)

(94) What will be the output of the following Python code snippet?
a={}
a['a']=1
a['b']=[2,3,4]
print(a)

A. {'b': [2], 'a': 1}


B. Exception is thrown
C. {'b': [2,3,4], 'a':1}
D. {'b': [2], 'a': [3] }

(95) What will be the output of the following Python code?


a={}
a[1]=1
a['1']=2
a[1.0]=4
count=0
for i in a:
count+=a[i]
print(count)

A. An exception is thrown
B. 6
C. 8
D. 7

(96) What will be the output of the following Python code?


def to_upper(k):
return k.upper()
x=['ab', 'cd']
print(list(map(to_upper,x)))

A. ['AB', ' CD ']


B. Error
C.['ab', 'cd']
D. ['AB', 'CD']

(97) What will be the output of the following Python code snippet?
>>> a={1:"A", 2:"B", 3:"C"}
>>>del a

A. method del doesn't exist for the dictionary


B. del deletes the values in the dictionary
C. del deletes the keys in the dictionary
D. del deletes the entire dictionary

(98) Which one of these functional programming methods usees lambda?


A. sort()
B. append()
C. filter()
D. lookup()

(99) What is the output of the code shown?


I=[1,2,3,4,5]
m=map(lambda x : 2**x, I)
pprint(list(m))

(100) What will be the output of the following Python code?


x=[12,34]
print(len(list(map(int,x))))

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

(102) Suppose list1 is [2,33,222,14,25]. What is list1[ : - 1] ?


A. [25,14,222,33,2]
B. Error
C. 25
D. [2, 33, 222, 14]

(103) What will be the output of the following Python code?

x=[12,34]
print(len("".join(list(map(int,x ) ) ) ) )

A. 4
B. None of the mentioned
C. Error
D. 2

(104) What is the output of the following code snippet?


func = lambda x: return x
print(func(2))
A. x
B. 2
C. SyntaxError
D. 0

(105) What will be the output of the following Python code?


x=[[0],[1]]
print(" ".join(list(map(str,x))))

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

(107) What will be the output of the following Python code?

x=['ab', 'cd']
print(len(map(list,x)))

A. None of the mentioned


B. 4
C. 2
D. [2,2]

(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.

(109) A higher order function rat_algebra has been defined as following:


def rat_algebra(fn,rat1,rat2):
numer=fn( get_numer(rat1)*get_denom(rat2) , get_numer(rat2)*get_denom(rat1 ))
denom = get_denom(rat1) * get_denom(rat2)
return make_rational(numer,denom)

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.

def filter_foo(p, foo_list):


return [x for x in foo_list if p(x)]

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)

(111) Given the following, what gets printed?


names1= ['Amir', 'Barry', 'Chales', 'Dao']
names2=[name.lower().upper() for name in names1]
print(names2[1][2])

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

(113) What would be the output of the following statement?


>>> [x for x in [10, 25, 30, 45] if x%5==0]

A. [10,25,30,45]
B. [10,30]
C. [10]
D. [25,45]

You might also like