Python_lilst_questions_answers
MCQs (Multiple Choice Questions)
1. Which of the following creates a blank list in Python?
a) l = ()
b) l = {}
c) l = []
d) l = ''
Answer: c) l = []
2. What will be the output of the following code?
l = list(range(5))
print(l)
a) [0,1,2,3,4]
b) [1,2,3,4,5]
c) (0,1,2,3,4)
d) Error
Answer: a) [0,1,2,3,4]
3. Which function is used to split a string into a list in Python?
a) break()
b) split()
c) list()
d) partition()
Answer: b) split()
4. If the input is "[10,20,30]" and the following code is executed:
l = eval(input("Enter List:"))
print(type(l))
What will be the output?
a) <class 'tuple'>
b) <class 'list'>
c) <class 'str'>
d) <class 'dict'>
Answer: b) <class 'list'>
5. Which of the following correctly converts the string "python is very easy" into a list of words?
a) list("python is very easy")
b) "python is very easy".split()
c) split("python is very easy")
d) ["python","is","very","easy"]
Answer: b) "python is very easy".split()
Short Questions and Answers
Q1. How do you create an empty list in Python?
A1. By using square brackets:
l = []
Q2. Write a statement to create a list with elements 10, 20, 30, 40.
A2.
l = [10, 20, 30, 40]
Q3. How can you create a list dynamically from user input?
A3. Using eval(input()):
l = eval(input("Enter List:"))
Q4. What is the difference between list(range(5)) and range(5)?
A4.
-
range(5)creates a range object:range(0,5) -
list(range(5))converts it into a list:[0, 1, 2, 3, 4]
Q5. Explain how split() works with an example.
A5. split() breaks a string into a list of words based on spaces (or a given delimiter).
s = "python is very easy"
l = s.split()
print(l) # ['python', 'is', 'very', 'easy']
___________________________________________________________________________________
1. What will be the output of the following code?
l = [10, 20, 30, 40]
print(l[0])
a) 10
b) 20
c) 30
d) IndexError
Answer: a) 10
2. Which index refers to the last element of a list?
a) 0
b) len(l)
c) -1
d) 1
Answer: c) -1
3. What will n[2:7:2] return if n = [1,2,3,4,5,6,7,8,9,10]?
a) [2,4,6]
b) [3,5,7]
c) [2,3,4,5,6]
d) Error
Answer: b) [3,5,7]
4. What will be the result of n[8:2:-2] for n = [1,2,3,4,5,6,7,8,9,10]?
a) [9,7,5]
b) [8,6,4]
c) [9,8,7,6,5]
d) Error
Answer: a) [9,7,5]
5. Which of the following will cause an IndexError?
a) l[0]
b) l[-1]
c) l[10] when l = [10,20,30,40]
d) l[1:3]
Answer: c) l[10]
Short Questions and Answers
Q1. How can you access the first element of a list l = [10,20,30,40]?
A1. Using index 0: l[0] → 10
Q2. What happens if you try to access l[10] when l = [10,20,30,40]?
A2. It raises an IndexError because index 10 is out of range.
Q3. Write the output of n[4::2] for n = [1,2,3,4,5,6,7,8,9,10].
A3. Output: [5,7,9]
Q4. What is the difference between n[3:7] and n[3:7:1]?
A4. Both return the same result [4,5,6,7], since the default step is 1.
Q5. If n = [1,2,3,4,5,6,7,8,9,10], what is the output of n[4:100]?
A5. [5,6,7,8,9,10] (Python allows slicing beyond the list length without error).
True / False
-
Negative indexing in Python lists starts from
-1which refers to the last element. → True -
l[10]will return the 11th element if available, otherwiseNone. → False (it raisesIndexError) -
Slicing a list never raises an
IndexError, even if the end index is out of range. → True -
n[2:7:2]selects every 2nd element between index 2 and 6. → True -
n[8:2:-2]will produce[9,7,5]forn = [1,2,3,4,5,6,7,8,9,10]. → True
Fill in the blanks
-
The first element of a list can be accessed using index
___.
Answer:0 -
The last element of a list can be accessed using index
___.
Answer:-1 -
Accessing an index beyond the list size raises an
___.
Answer:IndexError -
In slicing, the third value represents the
___.
Answer:step -
n[4:100]returns[5,6,7,8,9,10]because slicing beyond the list size does not raise an___.
Answer:error
Perfect 🚀 Thanks for sharing the Traversing the elements of list section.
Here’s the complete set:
MCQs (Multiple Choice Questions)
1. What is traversal in a list?
a) Updating elements of a list
b) Sequential access of each element in the list
c) Deleting elements from a list
d) Adding elements to a list
Answer: b) Sequential access of each element in the list
2. Which function is commonly used to determine the number of elements in a list?
a) count()
b) len()
c) size()
d) length()
Answer: b) len()
3. If list_numbers = [0,1,2,3,4,5], what will len(list_numbers) return?
a) 4
b) 5
c) 6
d) IndexError
Answer: c) 6
4. In the given code:
a = 0
number = len(list_numbers)
while (a <= number):
print(list_numbers[a])
a = a + 1
What will happen?
a) It will print all elements of the list correctly
b) It will give an IndexError after printing all elements
c) It will run infinitely
d) It will print only the first element
Answer: b) It will give an IndexError (since a goes up to len(list_numbers))
5. Which of the following is the correct way to traverse all elements of a list without error?
a) while a <= len(list_numbers)
b) while a < len(list_numbers)
c) while a != len(list_numbers)
d) Both b and c
Answer: d) Both b and c
Short Questions and Answers
Q1. Define traversal of a list.
A1. Traversal is the sequential access of each element in the list.
Q2. What will the following code print?
list_numbers = [0,1,2,3,4,5]
print(list_numbers[2])
A2. Output: 2
Q3. Why does the code with while (a <= len(list_numbers)) raise an error?
A3. Because indexing in Python starts at 0, the last valid index is len(list) - 1. When a = len(list), it exceeds the valid index range → IndexError.
Q4. How can you avoid the error in the given while loop traversal?
A4. Use < instead of <=:
while a < len(list_numbers):
print(list_numbers[a])
a += 1
Q5. Write a simple Python code to traverse a list using a for loop.
A5.
list_numbers = [0,1,2,3,4,5]
for num in list_numbers:
print(num)
True / False
-
Traversal means accessing all elements of a list one by one. → True
-
The function
len(list)returns the last element of the list. → False (it returns number of elements). -
Using
while a <= len(list_numbers)is safe for traversal. → False (causesIndexError). -
The last valid index in a list of size
nisn-1. → True -
A
forloop is often more convenient than awhileloop for traversing lists. → True
Fill in the blanks
-
Traversal of a list means accessing each element in the list
___.
Answer: sequentially -
The function
___(list)returns the number of elements in the list.
Answer:len -
If a list has 10 elements, the last valid index is
___.
Answer:9 -
The error raised when accessing an index out of range is
___.
Answer:IndexError -
To safely traverse a list with a
whileloop, the condition must bea ___ len(list_numbers).
Answer:<
Perfect 🙌 Thanks for sharing the next section. I’ll now prepare MCQs, Short Q&A, True/False, and Fill in the blanks for:
👉 Traversing a list using while, for, even filtering, and index-wise display.
MCQs (Multiple Choice Questions)
1. Which loop condition correctly traverses all elements in a list using a while loop?
a) i <= len(l)
b) i < len(l)
c) i != len(l)
d) i == len(l)
Answer: b) i < len(l)
2. Which of the following is the output of the code?
l = [0,1,2,3,4,5,6,7,8,9,10]
for i in l:
if i % 2 == 0:
print(i)
a) 1 3 5 7 9
b) 0 2 4 6 8 10
c) 2 4 6 8
d) Error
Answer: b) 0 2 4 6 8 10
3. In the list l = ['A','B','C'], what is the negative index of element 'B'?
a) 0
b) 1
c) -1
d) -2
Answer: d) -2
4. What will the following code display?
l = ['A','B','C']
x = len(l) # 3
for i in range(x):
print(l[i], 'is available at +ve index:', i, 'and at -ve index:', i-x)
a) Each element with only positive index
b) Each element with both positive and negative index
c) Only the last element
d) Error in indexing
Answer: b) Each element with both positive and negative index
5. Which of the following is the simplest way to traverse a list in Python?
a) While loop
b) For loop (direct element access)
c) Index-based access with while
d) Range with while
Answer: b) For loop (direct element access)
Short Questions and Answers
Q1. Write code to traverse a list using a while loop.
A1.
l = [0,1,2,3,4,5]
i = 0
while i < len(l):
print(l[i])
i += 1
Q2. How does a for loop simplify list traversal compared to a while loop?
A2. In a for loop, Python automatically handles iteration without needing an explicit counter variable or increment.
Q3. Write Python code to display only even numbers from the list [0,1,2,3,4,5,6].
A3.
for i in [0,1,2,3,4,5,6]:
if i % 2 == 0:
print(i)
Q4. What are the positive and negative indexes of element 'C' in ['A','B','C']?
A4. Positive index = 2, Negative index = -1.
Q5. Why is range(len(l)) useful in list traversal?
A5. It allows iteration with access to both elements and their index positions.
True / False
-
Traversing with a while loop requires manual increment of the index variable. → True
-
A for loop can directly iterate over elements of the list without using indexes. → True
-
Negative indexing starts from
-0. → False (it starts from-1). -
The element at positive index
1and negative index-2is the same in['A','B','C']. → True -
Using
if i % 2 == 0inside a loop helps to filter odd numbers. → False (it filters even numbers).
Fill in the blanks
-
In a
whileloop, the condition must bei ___ len(l)to avoid errors.
Answer:< -
The simplest way to traverse a list is by using a
___loop.
Answer:for -
Negative index
-1always refers to the___element in the list.
Answer: last -
In the list
['A','B','C'],'A'has positive index0and negative index___.
Answer:-3 -
The function
___(l)gives the number of elements in a list.
Answer:len
Excellent ✨ — you’ve now entered the Functions of List section, which is rich in exam-worthy questions. I’ll prepare for you:
✅ MCQs
✅ Short Q&A
✅ True/False
✅ Fill in the blanks
MCQs (Multiple Choice Questions)
1. Which function returns the number of elements in a list?
a) count()
b) len()
c) size()
d) length()
Answer: b) len()
2. What will be the output of the following code?
n = [1,2,3,4]
n.insert(1,333)
print(n)
a) [333,1,2,3,4]
b) [1,333,2,3,4]
c) [1,2,3,333,4]
d) Error
Answer: b) [1,333,2,3,4]
3. What is the difference between append() and insert()?
a) Both add an item at a specific index.
b) Both add an item only at the end.
c) append() always adds at the end, insert() can add at a specific position.
d) append() adds multiple items, insert() adds one item.
Answer: c) append() always adds at the end, insert() can add at a specific position.
4. What happens if you try to remove an element not present in the list using remove()?
a) Nothing happens
b) It removes the last element
c) It returns None
d) It raises ValueError
Answer: d) It raises ValueError
5. Which of the following statements is TRUE about pop()?
a) It only removes elements but doesn’t return them.
b) It removes and returns an element from a list.
c) It works only when the list is empty.
d) It is identical to remove().
Answer: b) It removes and returns an element from a list.
Short Questions and Answers
Q1. What is the purpose of the count() function in lists?
A1. It returns the number of occurrences of a specified item in the list.
Q2. Explain the difference between append() and insert().
A2.
-
append(x): Addsxonly at the end of the list. -
insert(i,x): Insertsxat indexi(ifiis out of range, adds at start or end).
Q3. What is the use of extend()? Give an example.
A3. It adds all elements of another list (or iterable) to the current list.
order1 = ['A','B']
order2 = ['C','D']
order1.extend(order2) # ['A','B','C','D']
Q4. How is pop() different from remove()?
A4.
-
remove(x)→ Removes the first occurrence ofx. -
pop([i])→ Removes and returns the element at indexi. If index is not given, removes the last element.
Q5. How can append() and pop() be used to implement a stack?
A5.
-
Use
append(x)to push elements. -
Use
pop()to pop elements.
This follows LIFO (Last In First Out) order.
True / False
-
len()returns the number of elements in a list. → True -
append()can insert an element at any index. → False (always adds at the end). -
If
n.remove(50)is executed when50is not in list, it raisesValueError. → True -
pop()is the only list function that both modifies the list and returns a value. → True -
extend()is used to add a single element to the end of the list. → False (adds multiple items).
Fill in the blanks
-
The function
___()returns the index of the first occurrence of an element in a list.
Answer:index -
The
append()function always adds an element at the ___ of the list.
Answer: end -
The
insert()function takes ___ arguments: index and element.
Answer: two -
___()function adds all items of one list to another.
Answer: extend -
A stack can be implemented using lists with
___()for push and___()for pop.
Answer:append,pop
Superb 🙌 — now you’ve shared the biggest section:
✅ reverse() and sort()
✅ Aliasing vs Cloning
✅ Mathematical operations (+, *)
✅ Comparing lists
✅ Nested lists and matrix
✅ List comprehensions
This part is perfect for exams & interviews. I’ll give you:
-
MCQs
-
Short Q&A
-
True/False
-
Fill in the blanks
MCQs (Multiple Choice Questions)
1. What will be the output of:
n = [10,30,20,40]
n.reverse()
print(n)
a) [10,30,20,40]
b) [40,20,30,10]
c) [20,40,10,30]
d) Error
Answer: b) [40,20,30,10]
2. Which of the following raises TypeError when calling sort()?
a) [10,20,30,40]
b) ['sunny','bunny']
c) [20,'B',10,'A']
d) [True,False,True]
Answer: c) [20,'B',10,'A']
3. In aliasing, if x = [10,20,30] and y = x, what happens if y[1] = 999?
a) Only y changes
b) Only x changes
c) Both x and y change
d) Error
Answer: c) Both x and y change
4. Which operator is used for concatenating two lists?
a) *
b) +
c) %
d) &
Answer: b) +
5. What will be the output of:
x = [10,20,30]
y = x * 2
print(y)
a) [10,20,30,10,20,30]
b) [20,40,60]
c) Error
d) [10,20,30]*2
Answer: a) [10,20,30,10,20,30]
6. How can you represent a 3x3 matrix in Python?
a) With list of lists
b) With dict
c) With tuple
d) With set
Answer: a) With list of lists
7. What will the list comprehension [i*i for i in range(1,6) if i%2==0] return?
a) [1,4,9,16,25]
b) [4,16]
c) [2,4]
d) Error
Answer: b) [4,16]
Short Questions and Answers
Q1. Difference between reverse() and sort().
A1.
-
reverse()→ Reverses elements of list in place. -
sort()→ Arranges elements in ascending order (default) or descending order (withreverse=True).
Q2. Explain aliasing vs cloning in lists.
A2.
-
Aliasing (
y=x) → Both variables refer to the same list object. Changes reflect in both. -
Cloning (
y = x[:]ory = x.copy()) → Creates a new independent copy of the list.
Q3. What is the difference between a + b and a.extend(b)?
A3.
-
a + b→ Creates a new list combining both. -
a.extend(b)→ Adds all elements ofbintoa(modifiesain place, returnsNone).
Q4. Write a list comprehension to extract the first letter of each word in ['Chiranjeevi','Nag','Venkatesh','Balaiah'].
A4.
l = [x[0] for x in ['Chiranjeevi','Nag','Venkatesh','Balaiah']]
# ['C','N','V','B']
Q5. What is a nested list? Give an example.
A5. A list containing another list as its element.
n = [10,20,[30,40]]
True / False
-
sort()can sort a list containing both integers and strings. → False -
Aliasing means both variables point to the same list object. → True
-
Cloning with
copy()makes an independent duplicate of the list. → True -
a.extend(b)returns a new list containing elements of bothaandb. → False (it modifiesaand returns None). -
Nested lists can be used to represent a matrix. → True
Fill in the blanks
-
reverse()changes the order of elements in the list to___.
Answer: reverse order -
The function
___()sorts a list in ascending order by default.
Answer: sort -
The process of giving another reference variable to the same list is called
___.
Answer: aliasing -
The operator
___is used to concatenate two lists.
Answer:+ -
A
___is a list inside another list.
Answer: nested list -
List comprehension syntax is:
[expression for item in sequence if ___]
Answer: condition
Perfect 👌 You’ve now given me a hands-on example problem for Single List (names list) with all the key operations. I’ll prepare:
✅ MCQs
✅ Short Q&A
✅ True/False
✅ Fill in the blanks
based on this example.
MCQs (Multiple Choice Questions)
1. Which function is used to search for the index of an element in a list?
a) find()
b) index()
c) search()
d) locate()
Answer: b) index()
2. Which statement correctly sorts the list names in reverse alphabetical order (Z–A)?
a) names.sort(reverse=True)
b) names.reverse()
c) sorted(names)
d) names[::-1]
Answer: a) names.sort(reverse=True)
3. Which method is used to remove a specific element (by value) from a list?
a) delete()
b) pop()
c) remove()
d) discard()
Answer: c) remove()
4. What will be the result of the following code?
names = ["Alice", "Bob", "Charlie"]
names.append("David")
print(names)
a) ["Alice", "Bob"]
b) ["Alice", "Bob", "Charlie"]
c) ["Alice", "Bob", "Charlie", "David"]
d) Error
Answer: c) ["Alice", "Bob", "Charlie", "David"]
5. Which of the following is the correct way to print a list backward without modifying it?
a) for name in names[::-1]
b) for name in reversed(names)
c) Both a and b
d) None of the above
Answer: c) Both a and b
Short Questions and Answers
Q1. How do you check if "Charlie" exists in a list of names?
A1.
if "Charlie" in names:
print("Found")
Q2. What is the difference between sort() and reverse()?
A2.
-
sort()arranges elements in ascending (default) or descending (reverse=True) order. -
reverse()just reverses the current order without sorting.
Q3. How can you update "Bob" to "Bobby" in the list?
A3.
idx = names.index("Bob")
names[idx] = "Bobby"
Q4. Which method is used to add a new name at the end of the list?
A4. append()
Q5. What happens if you try to remove a name not present in the list using remove()?
A5. Python raises a ValueError.
True / False
-
names.sort()modifies the list in place. → True -
reversed(names)returns a new reversed list. → False (returns an iterator, not a list). -
append()adds an element at the end of the list. → True -
index("name")returns the last occurrence of the element. → False (returns first occurrence). -
Removing
"Eve"from the list when"Eve"is not present will silently do nothing. → False (raisesValueError).
Fill in the blanks
-
The
___()method returns the position of the first occurrence of an element in a list.
Answer:index -
The function
___()is used to add a new element at the end of a list.
Answer:append -
To remove a specific element by value, we use the
___()method.
Answer:remove -
Traversing forward is done using a
forloop, but reverse traversal can be done using___()or slicing with___.
Answer:reversed(),[::-1] -
names.sort(reverse=True)sorts the list in ___ order.
Answer: descending (Z–A)
Perfect example again 👍 – this Double List (students & marks) covers searching, sorting, traversing, updating, deleting, adding, and ranking. Just like we did with the Single List, let’s now create:
✅ MCQs
✅ Short Q&A
✅ True/False
✅ Fill in the blanks
MCQs (Multiple Choice Questions)
1. If we have two parallel lists (students, marks), why do we swap both while sorting?
a) To keep student and marks consistent
b) To save memory
c) To reduce time complexity
d) Not necessary
Answer: a) To keep student and marks consistent
2. Which loop construct is used in the code to traverse the lists in reverse order?
a) for loop with range()
b) while loop
c) do-while loop
d) for each loop
Answer: b) while loop
3. What will be the output of this snippet?
students = ["Alice", "Bob", "Charlie"]
marks = [85, 90, 78]
for i in range(len(students)):
print(students[i], "->", marks[i])
a) 85 -> Alice etc.
b) Alice -> 85, Bob -> 90, Charlie -> 78
c) Only student names
d) Only marks
Answer: b) Alice -> 85, Bob -> 90, Charlie -> 78
4. Which method adds "Frank" with marks to the list?
a) insert()
b) append()
c) add()
d) extend()
Answer: b) append()
5. When sorting students by marks in descending order, what condition is checked?
a) if marks[i] > marks[j]:
b) if marks[i] < marks[j]:
c) if students[i] > students[j]:
d) if students[i] < students[j]:
Answer: b) if marks[i] < marks[j]:
Short Questions & Answers
Q1. How do you find Charlie’s marks from the list?
A1. Loop through students, find index of "Charlie", and print marks[i].
Q2. Why do we need parallel lists instead of a single list here?
A2. Because we need to maintain mapping student → marks consistently.
Q3. How do you delete a student and their marks?
A3.
del students[i]
del marks[i]
Q4. What happens if you add a student to the list but forget to add marks?
A4. The two lists will be misaligned, leading to incorrect mapping.
Q5. How is the Rank List created?
A5. By sorting both lists in descending order of marks and printing with rank numbers.
True / False
-
students.append("Frank")automatically assigns marks to Frank. → False -
Sorting students alphabetically also changes the order of marks. → True
-
Deleting
"Eve"fromstudentsrequires deleting her mark frommarks. → True -
The program uses
breakafter finding a student to stop searching. → True -
Rank List always sorts by student name. → False (it sorts by marks).
Fill in the blanks
-
To maintain mapping between student and marks, both lists must be updated ___.
Answer: together -
The
___()method is used to add a new student at the end of the list.
Answer: append -
To traverse both lists forward, we loop from
0to___.
Answer:len(students)-1 -
Sorting by student name uses the condition
if students[i] ___ students[j]:.
Answer: > -
The rank list is displayed after sorting in ___ order of marks.
Answer: descending
Got it ✅ — I’ll summarize everything we discussed (Single List + Double List operations) into two sections only:
MCQs (Multiple Choice Questions)
1. What does names.sort() do?
a) Sorts numbers only
b) Sorts the list in ascending (A–Z) order
c) Sorts list in descending (Z–A) order
d) Reverses the list
Answer: b) Sorts the list in ascending (A–Z) order
2. Which operator is used to join two lists into one?
a) *
b) +
c) &
d) append()
Answer: b) +
3. What is aliasing in Python lists?
a) Making a copy of the list
b) Two variables referring to the same list
c) Sorting a list
d) Removing duplicates
Answer: b) Two variables referring to the same list
4. In the code:
l = [10, 20, 30]
y = l.copy()
What happens?
a) Aliasing occurs
b) Cloning occurs (independent copy)
c) Error occurs
d) Nothing
Answer: b) Cloning occurs
5. Which loop construct is used to traverse a list backward?
a) for with range(len(l))
b) while loop with decrement
c) for each loop
d) None
Answer: b) while loop with decrement
6. Why do we swap both students and marks while sorting in double lists?
a) To save memory
b) To keep student–marks consistent
c) To reduce execution time
d) It is optional
Answer: b) To keep student–marks consistent
7. Which method is used to add a new element at the end of a list?
a) insert()
b) append()
c) add()
d) extend()
Answer: b) append()
8. To sort students by marks (highest → lowest), which condition is correct?
a) if marks[i] > marks[j]
b) if marks[i] < marks[j]
c) if students[i] > students[j]
d) if students[i] < students[j]
Answer: b) if marks[i] < marks[j]
9. Which of these is not a valid list operation?
a) Traversal
b) Aliasing
c) Cloning
d) Compiling
Answer: d) Compiling
10. A nested list can be used to represent:
a) A string
b) A dictionary
c) A matrix
d) A set
Answer: c) A matrix
Q & A (Short Answer Questions)
Q1. What is list traversal?
A1. Sequentially accessing each element of the list.
Q2. What is the difference between = operator and copy() function in lists?
A2. = creates an alias (both variables refer to the same object), while copy() creates a new independent duplicate (cloning).
Q3. What is the difference between shallow cloning and deep cloning?
A3.
-
Shallow cloning: Copies only the outer list, nested objects still refer to the same memory.
-
Deep cloning: Creates a complete copy, including all nested objects.
Q4. How do you delete an element "Eve" from a list?
A4. Use list.remove("Eve") or del list[index].
Q5. Why must we delete from both students and marks lists in double list programs?
A5. To maintain mapping consistency between student names and their marks.
Q6. How do you represent a matrix using lists?
A6. Using nested lists, e.g., [[10,20,30],[40,50,60],[70,80,90]].
Q7. What is list comprehension? Give an example.
A7. A concise way to create lists using expressions.
Example: [i*i for i in range(1,6)] → [1,4,9,16,25].
Q8. How is a Rank List generated from two lists?
A8. Sort both students and marks in descending order of marks and display with ranks.
Q9. How do you update a student’s marks in a double list?
A9. Find the student’s index using a loop, then assign a new value to marks[index].
Q10. What happens if you add a new student without adding marks?
A10. The lists become misaligned, breaking the student–marks mapping.
Perfect! ✅ I’ve created a 20-question practice test combining MCQs and Q&A based on all the list concepts we discussed (single list, double list, operations, traversals, functions, nested lists, and comprehensions).
Python Lists Practice Test (20 Questions)
MCQs (Choose the correct answer)
1. Which function returns the number of elements in a list?
a) count()
b) len()
c) size()
d) length()
Answer: b) len()
2. What will l[::-1] do to a list l?
a) Sort ascending
b) Reverse the list
c) Copy the list
d) Remove duplicates
Answer: b) Reverse the list
3. How do you add an element "Frank" at the end of a list names?
a) names.add("Frank")
b) names.append("Frank")
c) names.insert(-1, "Frank")
d) names.extend("Frank")
Answer: b) names.append("Frank")
4. Which method removes the first occurrence of an element from a list?
a) pop()
b) delete()
c) remove()
d) discard()
Answer: c) remove()
5. What is the last valid index in a list with 10 elements?
a) 9
b) 10
c) 0
d) -10
Answer: a) 9
6. Which of the following is aliasing?
a) y = x.copy()
b) y = x
c) y = x[:]
d) y = list(x)
Answer: b) y = x
7. To create a rank list of students based on marks, which sorting order is correct?
a) Marks ascending
b) Marks descending
c) Names ascending
d) Names descending
Answer: b) Marks descending
8. What will [i*i for i in range(1,6) if i%2==0] return?
a) [1,4,9,16,25]
b) [4,16]
c) [2,4]
d) [1,9,25]
Answer: b) [4,16]
9. Which of the following operations is invalid for a list containing both integers and strings?
a) append()
b) reverse()
c) sort()
d) pop()
Answer: c) sort()
10. How do you traverse a list in Python?
a) Using a for loop
b) Using a while loop
c) Using list comprehension
d) All of the above
Answer: d) All of the above
Short Answer / Q&A
11. What is the difference between append() and insert() in a list?
Answer: append() adds an element at the end; insert(index, value) adds it at a specific position.
12. Explain cloning vs aliasing.
Answer:
-
Aliasing (
y = x) → both variables refer to the same object; changes affect both. -
Cloning (
y = x.copy()ory = x[:]) → creates a new independent object.
13. How do you reverse a list without using reverse()?
Answer: Using slicing: l[::-1]
14. How do you traverse two parallel lists students and marks together?
Answer:
for i in range(len(students)):
print(students[i], "->", marks[i])
15. How do you update "Bob"’s marks to 95 in a double list?
Answer:
idx = students.index("Bob")
marks[idx] = 95
16. How can a nested list be used to represent a matrix?
Answer: Each sublist represents a row, e.g., [[1,2,3],[4,5,6],[7,8,9]].
17. Write a list comprehension to get the first letter of each name in ["Alice","Bob","Charlie"].
Answer:
[x[0] for x in ["Alice","Bob","Charlie"]] # ['A','B','C']
18. What happens if you try to remove an element not in the list?
Answer: Python raises a ValueError.
19. Why must we update both students and marks lists while sorting or deleting in a double list?
Answer: To maintain correct mapping between students and their marks.
20. Give an example of concatenating two lists [1,2,3] and [4,5,6].
Answer:
[1,2,3] + [4,5,6] # [1,2,3,4,5,6]
Absolutely! Let's create a comprehensive Q&A style guide for List Operations: update, add, remove, and related functions. I’ll cover the key operations, their usage, and examples in a structured way for learning or revision.
List Operations Q&A
Q1. How do you add an element at the end of a list?
A1. Use the append() method.
l = [10, 20, 30]
l.append(40)
print(l) # [10, 20, 30, 40]
Q2. How do you insert an element at a specific index?
A2. Use the insert(index, value) method.
l = [10, 20, 30]
l.insert(1, 15)
print(l) # [10, 15, 20, 30]
Q3. What is the difference between append() and insert()?
A3.
-
append()adds an element only at the end. -
insert()adds an element at a specific index.
Q4. How do you add all elements of one list to another list?
A4. Use the extend() method.
l1 = [1, 2]
l2 = [3, 4]
l1.extend(l2)
print(l1) # [1, 2, 3, 4]
Q5. How do you remove a specific element by value?
A5. Use the remove() method.
l = [10, 20, 30, 20]
l.remove(20)
print(l) # [10, 30, 20] (removes first occurrence)
Q6. How do you remove and return the last element of a list?
A6. Use the pop() method.
l = [10, 20, 30]
x = l.pop()
print(x) # 30
print(l) # [10, 20]
Q7. How do you remove and return an element at a specific index?
A7. Pass the index to pop(index).
l = [10, 20, 30]
x = l.pop(1)
print(x) # 20
print(l) # [10, 30]
Q8. How do you find the index of the first occurrence of an element?
A8. Use the index() method.
l = [10, 20, 30, 20]
print(l.index(20)) # 1
Q9. How do you count the occurrences of a specific element?
A9. Use the count() method.
l = [10, 20, 20, 30]
print(l.count(20)) # 2
Q10. How do you update a value at a specific index?
A10. Assign the new value using the index.
l = [10, 20, 30]
l[1] = 25
print(l) # [10, 25, 30]
Q11. How do you reverse a list in place?
A11. Use the reverse() method.
l = [10, 20, 30]
l.reverse()
print(l) # [30, 20, 10]
Q12. How do you sort a list in ascending or descending order?
A12. Use the sort() method.
l = [30, 10, 20]
l.sort() # Ascending
print(l) # [10, 20, 30]
l.sort(reverse=True) # Descending
print(l) # [30, 20, 10]
Q13. What happens if you try to remove an element not present in the list?
A13. remove() raises a ValueError.
Q14. How do you traverse a list?
A14. Using for loop, while loop, or list comprehension.
l = [10, 20, 30]
for x in l:
print(x)
Q15. How can you implement a stack (LIFO) using a list?
A15.
-
Use
append()to push elements. -
Use
pop()to pop elements.
stack = []
stack.append(10)
stack.append(20)
print(stack.pop()) # 20
Q16. How do you implement shallow cloning of a list?
A16. Using slice or copy() method.
l = [10, 20, 30]
clone1 = l[:] # Using slice
clone2 = l.copy() # Using copy()
Q17. What is aliasing in lists?
A17. When two variables refer to the same list object, changes through one affect the other.
Q18. How do you concatenate two lists?
A18. Using + operator or extend().
a = [1, 2]; b = [3, 4]
c = a + b # [1, 2, 3, 4]
a.extend(b) # a becomes [1, 2, 3, 4]
Q19. How do you repeat elements of a list multiple times?
A19. Using * operator.
l = [1, 2]
print(l * 3) # [1, 2, 1, 2, 1, 2]
Q20. How do you create a nested list?
A20. A list containing other lists.
matrix = [[1,2,3],[4,5,6],[7,8,9]]
print(matrix[1][2]) # 6
Comments
Post a Comment