Python Questions and Answers – Lists – 4

This set of Python Programming Questions & Answers focuses on “Lists”.

1. 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
View Answer

Answer: c
Explanation: In Python, default arguments are evaluated only once when the function is defined. Since the default values is a mutable list, it retains changes across function calls. Thus, each call to f() appends to the same list, resulting in [1, 2, 3].
advertisement

2. 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
View Answer

Answer: c
Explanation: In the given code, the string ‘amir’ is checked for membership in the list [‘Amir’, ‘Bala’, ‘Chales’]. Since Python is case-sensitive, ‘amir’ (lowercase ‘a’) does not match ‘Amir’ (uppercase ‘A’), so the condition fails. Hence, the output is 2.
Free 30-Day Java Certification Bootcamp is Live. Join Now!

3. 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
View Answer

Answer: d
Explanation: The code uses list comprehension to convert each name in names1 to lowercase, resulting in [‘amir’, ‘bala’, ‘charlie’]. names2[2] refers to ‘charlie’, and names2[2][0] gives the first character ‘c’. Therefore, the output is c.
advertisement

4. 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
View Answer

Answer: b
Explanation: The append() method adds its argument as a single element at the end of the list. Here, [5, 6, 7, 8] is appended as one sublist, not individual elements. So the list becomes [1, 2, 3, 4, [5, 6, 7, 8]], which has 5 elements.

5. 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
View Answer

Answer: d
Explanation: The in operator is used to check for membership and works with lists, dictionaries, and sets. In lists and sets, it checks for presence of an element; in dictionaries, it checks for presence of a key. Therefore, it applies to all the mentioned data structures.

6. 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
View Answer

Answer: d
Explanation: The + operator concatenates two lists by combining all their elements into a new list. So list1 + list2 results in [1, 2, 3, 4, 5, 6, 7, 8], which has 8 elements. Therefore, len(list1 + list2) returns 8.

7. 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
View Answer

Answer: c
Explanation: The function addItem uses += [1] to modify the list passed to it. Since lists are mutable and passed by reference, mylist is modified directly. After appending 1, it becomes [1, 2, 3, 4, 1], so its length is 5.

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

def increment_items(L, increment):
    i = 0
    while i < len(L):
        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
View Answer
Answer: a
Explanation: The function increment_items modifies the list in-place by adding the increment value to each element. However, since it doesn’t return anything, the first print() outputs None. The second print() shows the updated list [3, 4, 5].
 
 

9. 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
View Answer

Answer: a
Explanation: The function iterates over the list L, starting at index 0, and appends every third element (i = i + 3) to result. This means it collects items at indices 0, 3, 6, and so on, effectively returning every third item from the list starting at the first element.

10. 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’]
View Answer

Answer: a
Explanation: The insert() method inserts ‘celery’ at the index where ‘broccoli’ is found, which is position 1. This pushes ‘broccoli’ and the following elements one position to the right, resulting in the list: [‘carrot’, ‘celery’, ‘broccoli’, ‘potato’, ‘asparagus’].

Sanfoundry Global Education & Learning Series – Python.

To practice all programming questions on Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.