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
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].
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
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.
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
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.
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
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
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
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
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] NoneView Answer
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
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
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.
- Apply for Programming Internship
- Check Python Books
- Practice Programming MCQs
- Check Information Technology Books
- Apply for Python Internship