Python MCQs on List: SET 4

31. What will be the output of the following Python code?

def f(i, values = []):

values.append(i)

return values

f(1)

f(2)

v = f(3)

print(v)

a) [1] [2] [3]

b) [1] [1, 2] [1, 2, 3]

c) [1, 2, 3]

d) 1 2 3

Check Answer

Answer: c
Execute in the shell to verify

32. What will be the output of the following Python code?

names1 = [‘Amir’, ‘Bala’, ‘Chales’]

if ‘amir’ in names1:

print(1)

else:

print(2)

a) None

b) 1

c) 2

d) Error

Check Answer

Answer: c
Execute in the shell to verify.

33. What will be the output of the following Python code?

names1 = [‘Amir’, ‘Bala’, ‘Charlie’]

names2 = [name.lower() for name in names1]

print(names2[2][0])

a) None

b) a

c) b

d) c

Check Answer

Answer: d
List Comprehension are a shorthand for creating new lists.

34. What will be the output of the following Python code?

numbers = [1, 2, 3, 4]

numbers.append([5,6,7,8])

print(len(numbers))

a) 4

b) 5

c) 8

d) 12

Check Answer

Answer: b
A list is passed in append so the length is 5.

35. To which of the following the “in” operator can be used to check if an item is in it?

a) Lists

b) Dictionary

c) Set

d) All of the mentioned

Check Answer

Answer: d
In can be used in all data structures.

36. What will be the output of the following Python code?

list1 = [1, 2, 3, 4]

list2 = [5, 6, 7, 8]

print(len(list1 + list2))

a) 2

b) 4

c) 5

d) 8

Check Answer

Answer: d
+ appends all the elements individually into a new list.

37. What will be the output of the following Python code?

def addItem(listParam):

listParam += [1]

mylist = [1, 2, 3, 4]

addItem(mylist)

print(len(mylist))

a) 1

b) 4

c) 5

d) 8

Check Answer

Answer: c
+ will append the element to the list.

38. What will be the output of the following Python code?

def increment_items(L, increment):

i = 0
while i < len(L):

[i] = L[i] + increment
i = i + 1

values = [1, 2, 3]

print(increment_items(values, 2))
print(values)

a)

None
[3, 4, 5]

b)

None
[1, 2, 3]

c)

[3, 4, 5] [1, 2, 3]

d)

[3, 4, 5] None

Check Answer

Answer: a
Execute in the shell to verify.

39. What will be the output of the following Python code?

def example(L):
”’ (list) -> list
”’
i = 0
result = []
while i < len(L):
result.append(L[i])
i = i + 3
return result

a) Return a list containing every third item from L starting at index 0

b) Return an empty list

c) Return a list containing every third index from L starting at index 0

d) Return a list containing the items from L starting from index 0, omitting every third item.

Check Answer

Answer: a
Run the code to get a better understanding with many arguments.

40. What will be the output of the following Python code?

veggies = [‘carrot’, ‘broccoli’, ‘potato’, ‘asparagus’]

veggies.insert(veggies.index(‘broccoli’), ‘celery’)

print(veggies)

a) [‘carrot’, ‘celery’, ‘broccoli’, ‘potato’, ‘asparagus’]

b) [‘carrot’, ‘celery’, ‘potato’, ‘asparagus’]

c) [‘carrot’, ‘broccoli’, ‘celery’, ‘potato’, ‘asparagus’]

d) [‘celery’, ‘carrot’, ‘broccoli’, ‘potato’, ‘asparagus’]

Check Answer

Answer: a
Execute in the shell to verify.

Pages  1  2  3  4  5  6  7

Leave a Comment