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

11 IP 2022 Lists in Python new

The document consists of objective type questions related to Python lists, including fill-in-the-blanks, true or false statements, and multiple choice questions. It also includes solved questions that explain lists, their characteristics, methods, and operations. Additionally, it provides examples and explanations of various list operations and methods in Python.

Uploaded by

Kartik Kansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

11 IP 2022 Lists in Python new

The document consists of objective type questions related to Python lists, including fill-in-the-blanks, true or false statements, and multiple choice questions. It also includes solved questions that explain lists, their characteristics, methods, and operations. Additionally, it provides examples and explanations of various list operations and methods in Python.

Uploaded by

Kartik Kansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

OBJECTIVE TYPE QUESTIONS

1. Fill in the blanks.


(a) List in Python is classified as a data type.
(b) A list consists of elements of type.
(c) Lists are indexed by a/an data type.
(d) Elements of a list are enclosed in brackets.
(e) Lists are , you can update or edit the list.
(f) method is used to delete elements from a list if index is not known.
(g) method is used to delete elements from a list if index is known.
(h) The method adds a single item to the existing list at the end.
(i) You can compare two lists by using operators.
(j) The operator replicates a list.
(k) The operator creates a reference of the original list that shares the same
memory location.
(I) The function returns the deleted element of the list.
(m) The function arranges the elements of the list either in ascending or descending
order.

2. State whether the following statements are True or False.


(a) Python lists are mutable.
(b) Lists are depicted through curly brackets only.
(c) A list can contain heterogeneous values.
(d) We can create an empty list by the code L=list()
(e) A list is stored in memory as integer.
(f) The command [1,2,3,4,]<[9,1] will give the output as True.
(g) List[a:b] will give the elements between a and b.
(h) The append() function is used to add a value in a list at any place.
(i) index() function will return the index of first matched item from the list.
(j) extend() function is used to add multiple list items in a list.
3. Multiple Choice Questions (MCQs):
(a) Consider the following statement:
List=['h', 'o', 'i']
The length of the above list is:
(i) 5 (ii) 3 (iii) 0 (iv) None of these
(b) The list 1.1 contains [3,4,5,6,6,8,0]
What will be the output of the following expression?
print(L1[-1])
(i) 0 (ii) 3 (iii) 5 (iv) 8
(c) Which of the following statements is correct about list?
(i) List can contain values of mixed data types. (ii) List can contain duplicate values.
(iii) A list with no elements is called an empty list. (iv) All of these
(d) Which operator can be used with list?
(i) in (ii) not in
(iii) both (i) & (ii) (iv) Arithmetic Operators only
(e) Which of the following commands will create a list?
(i) Listl=list() (ii) List1=[]
(iii) List1=[1,2,3,"a"] (iv) All of these
(f) What is the use of append() function in list?
(i) It adds an item to the end of the list. (ii) It adds an item at the beginning of the list.
(iii) It adds an item anywhere in the list. (iv) All of these
(g) Select the output of the following expression:
strl= "pen"
print (list (strl) )
(i) ['p','e','n'] (ii) [pen] (iii) [p/e/n] (iv) { "pen")
(h) The sequential accessing of each of the elements in a list is called:
(i) List Indexing (ii) List Traversal (iii) List Slicing (iv) List Accessing
(i) Consider the following lists:
L1=["this", "is", "a", "list"]
L2=["this", "is", "another list"]
Which of the following statements will result in an error?
(i) L1=L2 (ii) Ll.copy()
(iii) L2.append(1,2,3) (iv) L2.pop()
(i) Consider the following code:
>>>A=[10,20,30,40]
>>>B=A.copy()
>>>A[2]=50
>>>B
Which of the following will be the elements of list A and B?
(i) A= [10,20,50,40] (ii) A=[10,20,30, 40]
B=[10,20,30, 40] B=[10,20,50, 40]
(iii) A= [10,20,30,40,50] (iv) A= [10,20,40]
B=[10,20,50, 40] B= [10,20,50, 40]
(k) Consider the following lists:
A= [1,2]
B= [1,2]
What will be the output of the following?
print (A==B)
(i) True (ii) False (iii) Error (iv) No output
(I) Which of the following function will return the first occurrence of the specified element in a list?
(i) sort () (ii) value () (iii) index () (iv) Sorted()
Chapter-5
1. Fill in the blanks:
(a) Sequence (b) Heterogeneous (c) Integer (d) Square[) (e) Mutable
(f) remove() (g) pop() (h) append() (i) Comparison (j) replication(*)
(k) Assignment(=) (I) pop() (m) sort()

2. True or False:
(a) True (b) False (c) True (d) True (e) False
(f) True (g) False (h) False (i) True (j) True

3. Multiple Choice Questions (MCQs):


SOLVED QUESTIONS

1. What is a list?
Ans. A list is a mutable sequence of values which can be of any type. Lists are indexed by an integer. Elements
of a list are enclosed in square brackets [ ], separated by commas. Lists allow duplicate values as well. The
elements of a list can be accessed by its index.
2. What are the differences between lists and strings?
Ans. The differences between lists and strings are:
(a) Strings cannot be changed but lists are mutable. They can be changed.
(b) Lists use square brackets unlike strings which are enclosed in single or double quotes.
(c) Strings store single type of elements but lists can store elements of different types.
3. Can lists be used as keys in Python dictionaries? Justify your answer.
Ans. No, lists cannot be used as keys in a dictionary because lists are mutable while keys of the dictionary are
immutable by default.
4. What is the difference between pop() and remove() functions?
Ans. pop() function removes the elementfrom the specified index and also returns the element which was removed.
if no index value is provided in pop(), then the last element is deleted. remove() function is used when we
know the element to be deleted but not the index of the element. It does not return the deleted element.
5. Given below are statements for creating lists. Find errors, if any, and rewrite the correct statements.
(a) L1= 1,6,a,8
(b) L2=(10)
(c) L1=[[0,1,2,3] ['my', 'book']]
(d) L1=[0,1,2,3], [4,5,6]
(e) L1=(['a','b', 'c' [1,2,3,A] )
(f) L1=[Aman,Lakshay,Aushim,Nishant]
Ans. (a) L1= [1,6, 'a',8]
(b) L2=[10]
(c) L1=[[0,1,2,3], ['my', 'book']]
(d) L1=[[0,1,2,3], [4,5,6]]
(e) L1=['a','b','c',[1,2,3,'A']]
(f) L1=[ 'Aman', 'Lakshay', 'Aushim', 'Nishant' ]
6. Suppose L=[ "abc", [6,7,8], 3, 'mouse']
Consider the above list and answer the following:
(a) L[3:] (b) L[::2]
(c) L[1:2] (d) L[1] [1]
Ans. (a) ['mouse'] (b) ['abc', 3]
(c) [[6, 7, 8]] (d) 7
7. Write the most appropriate list method to perform the following tasks:
(a) Delete a given element from the list.
(b) Get the position of an item in the list.
(c) Delete the 3rd element from the list.
(d) Add single element at the end of the list.
(e) Add an element in the beginning of the list.
(f) Add elements in the list at the end of a list.
Ans. (a) remove()
(b) index()
(c) pop()/del
(d) append()
(e) insert()
(f) extend()
8. Give the output of the following:
(a) a=[2,7,9,13,28]
print(a [2:1+a [:2])
(b) values = [ [3, 4, 5, 1 ], [33, 6, 1, 2] ]
for row in values:
row. sort()
for element in row:
print (element, end = " ")
print()
Ans. (a) [9, 13, 28, 2, 7]
(b) 1 3 4 5
1 2 6 33
9. Suppose listl is [3, 4, 5, 20, 5, 25, 1, 3]. What is listl after listl.pop(1)?
Ans. [3, 5, 20, 5, 25, 1, 3]
10. What will the output of the following statements?
(a) listl = [12,32,65,26,80,10]
listl.sort()
print (listl)
(b) listl = [12,32,65,26,80,10]
sorted(listl)
print (listl)
(c) listl = [1,2,3,4,5,6,7,8,9,10]
listl [: :-2]
listl [:3] + listl [3: ]
(d) listl = [1,2,3,4,5]
listl [len (list1)-1]
Ans. (a) [10,12,26,32,65,80]
Explanation:
Line 1: Creation of list with the list object listl and values -12, 32, 65, 26, 80, 10.
Line 2: listl.sort() function sorts all values in ascending order.
Line 3: Finally the list is printed.
(b) [10,12,26,32,65,80]
[12,32,65,26,80,10]
Explanation:
Line 1: Creation of list with the list object listl and values -12, 32, 65, 26, 80, 10.
Line 2: sorted(listl) function sorts all values and displayed the sorted list.
Line 3: Print the listl as it is without modifying the list.
(c) [10,8,6,4,2]
[1,2,3,4,5,6,7,8,9,10]
Explanation:
Line 1: Creation of list with list object listl and values- 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
Line 2: List1 [: : -2] reverses the list with skip value -2.
Line 3: As the first slice returns the first three elements and the next slice returns all elements except
the first three elements.
(d) 5
Explanation:
Line 1: Creation of list with list object listl and values -1, 2, 3, 4, 5.
Line 2: The len() function returns total number of elements from the list. So, the len(listl) = 5.
Therefore, the element at list [5 - 1] = list [4] is 5.
11. Differentiate between append() and extend() methods of list.
Ans.
append() extend()
append() function adds one element to a list at a extend() function adds multiple elements to a list
time. at a time.
The multiple values can be added using a list as a If user adds multiple values in the form of list as a
parameter but append adds them as a list only. parameter, it will add as separate values.
Example: Example:
t1=[1,2,3] t1=[1,2,3]
tl . append ( [4,5] ) tl . extend ( [4,5] )
print(tl) print(tl)
The output will be: [1,2,3, [4,5] ] The output will be: [1,2,3,4,5]

12. Consider a list:


listl = [6,7,8,9]
What is the difference between the following operations on listl:
(a) listl*2
(b) listl*=2
(c) listl=listl*2
Ans. (a) The * operator is treated as a replication operator when the list object is referred to with any numeric
value. So, here it will replicate the list values. Therefore, it returns [6,7,8,9,6,7,8,9].
(a) It is a shorthand operator and will also produce a similar result like question (a).
(c) It also produces a similar result as the statement is also similar.
13. The record of a student (Name, Roll No, Marks in five subjects and percentage of marks) is stored in the
following list:
stRecord = [ 'Raman' , 'A-36' , [56,98,99,72,69] , 78.8]
Write Python statements to retrieve the following information from the list stRecord.
(a) Percentage of the student.
(b) Marks in the fifth subject.
(c) Maximum marks of the student.
(d) Roll No. of the student.
(e) Change the name of the student from 'Raman' to 'Raghav'.
(f) Access the index directly which is given at 0th index in the list.
Ans. (a) As percentage is given at the end of the list which is at the 3rd index, the statement will be written in this
manner:
perc=stRecord [3]
print (perc)
(b) As marks are given at 2nd index and it is a nested list with five subjects, the marks list index will be 4th.
So, the statement will be:
marks sub5 = stRecord [2] [4]
print (marks sub5)
(c) We need to access marks here and return the maximum value. To do this, use max() function:
marks_max = max (stRecord [2 ] )
print (marks_max)
(d) Access the index directly, which is given at ist index in the list.
rno = stRecord [1]
print (rno)
(e) Change the name of the student from 'Raman' to `Raghav'.
stRecord [0] =' Raghav'
print (stRecord)
(f) sname = stRecord [0]
print (sname)
14. Given a Python list, find value 20 in the list, and if it is present, replace it with 200. Only update the first
occurrence of a value.
listl = [5,10,15,20,25,50,20]
Expected output:
listl = [5,10,15,200,25,50,20]
Ans. listl = [5, 10, 15, 20, 25, 50, 20]
indexl = listl.index(20)
listl[indexl] = 200
print(listl)
15. Write a Python program to count the number of strings where the string length is 2 or more and the first
and last characters are same from a given list of strings.
Sample List: [ ' abc ' , 'xyz', 'cbc', '121' ]
Expected Result: 2
Ans. 1= [ ' abc ' , 'xyz', 'cdc', '121' ]
c=0
for word in 1:
if len (word) > 1 and word [0] == word [-1] :
c=c+1
print (c)
16. Write the output of the following:
L=[]
Ll=[]
L2=[]
for i in range (6,10) :
L.append(i)
for i in range (10,4,-2) :
Ll.append(i)
for i in range (len (L1) ) :
L2 .append (L [i]+Ll [i] )
L2. append (len (L) -len (L1) )
print (L2)
Ans. [16, 15, 14, 1]
17. Write the output of the following:
(a) str1='book'
print (list (strl) )
(b) L = [2, 4, 5, 6, 2, 3, 4, 4, 7 ]
count = L. count (4)
print(count)
(c) L = [10,45,2,20,30,40]
L.sort()
print (L)
L.reverse()
print (L)
(d) L = ['p', 'w', 'r', 'd', 'a']
L.remove('r')
print(L)
print (L .pop () )
del L[1]
print(L)
(e) Ll = [10, 20, 30, 40]
Ll.clear()
print(L1)
Ans. (a) ['b', 'o', 'o', 'k']
(b) 3
(c) [2, 10, 20, 30, 40, 45]
[45, 40, 30, 20, 10, 2]
(d) ['p', 'w', 'd', 'a']
a
,
[ 'p ' 'd']

(e) []
18. Write the output of the following:
Ll= [500, 600]
L2= [35, 45]
Ll . append (700)
Ll . extend (L2)
Ll . insert (5,2)
print (L1)
print (Ll+L2)
print(L1)
print (L1 . index (35) )
print(L2*2)
Ans. [500, 600, 700, 35, 45, 2]
[500, 600, 700, 35, 45, 2, 35, 45]
[500, 600, 700, 35, 45, 2]
3
[35, 45, 35, 45]
19. Write the output of the following code:
List1=[2,4,6,8,10]
List2=List1
List3=Listl.copy()
Listl [4] =12
List2[3]=14
Listl . remove (2)
print ("Listl",Listl)
print ("List2",List2)
print ("List3",List3)
Ans. List1[4, 6, 14, 12]
List2 [4, 6, 14, 12]
List3 [2, 4, 6, 8, 10]
20. Write a program to perform linear search on the given list:
list = [10,51,2,18,4,31,13,5,23,64,29]
Ans. #Linear Search Implementation
num = [10,51,2,18,4,31,13,5,23,64,29]
pos = 0
print("List elements are: ",end=")
for i in num:
print(i,end=")
print()
find = int(input("Enter the element to search: "))
flag = 0
for i in num:
if (i == find):
flag = 1
pos = num.index(i)
break
if flag == 1:
print("Element found at index:",pos)
else:
print("Element not found")
Output
List elements are: 10 51 2 18 4 31 13 5 23 64 29
Enter the element to search: 18
Element found at index: 3
21. Write the output of the following:
(a) L=[1,2,3,4,5,6,7,8,9,10]
S= [i for i in L if i%2==0]
print(S)
(b) A=[1,2,3,4]
B=[value*3 for value in A]
print(B)
Ans. (a) [2,4,6,8,10]
(b) [3,6,9,12]
22. Write a program to shift every element of a list to circularly right, e.g.,
Input: 1,2,3,4,5,6,7
Output: 4 561237
Ans. 1=[1,2,3,4,5,6,7]
mid=int (len (1) /2)
for i in range (mid) :
1 [i] , 1 [mid+i] =1 [mid+i] , [i]
print (1)
23. Write a program which accepts a number from the user and prints the frequency of the number in the list
LST given as under. If the number is not in the LST, it should print "Number not found".
LST = [3, 21, 5, 6, 3, 8, 21, 6]
Output:
Enter Element: 6
The frequency of number 6 is 2.
Ans. LST = [3, 21, 5, 6, 3, 8, 21, 6]
length = len (LST)
element = int (input ("Enter element: ") )
c=0
for i in range (0, length):
if element == LST[i]:
c=c+1
if c==0:
print ("Number Not found")
else:
print ("The frequency of number", element, "is", c)
24. Write a program in Python to find the maximum and minimum elements in the list entered by the user.
Ans. 1st = []
num = int (input ( 'How many numbers: ' ) )
for n in range (num) :
numbers = int (input ( 'Enter number: 'H
1st. append (numbers)
print ("Maximum element in the list is :", max(lst))
print ("Minimum element in the list is :", min(lst))
Output:
How many numbers: 5
Enter number: 1
Enter number: 88
Enter number: 45
Enter number: 27
Enter number: 10
Maximum element in the list is : 88
Minimum element in the list is : 1
25. Write the output of the following Python program code:
STR1= list ("SNo4")
for i in range (len (STR1) ) :
if i==3:
x=int (i)
x+=x-3
STR1 [i] =x
elif (STR1 [i] .islower () ) :
STR1 [i] = STR1 [i] . upper ()
else:
STR1 [i]=STR1 [i] *2
print(STR1)
Ans. ['SS', 'NN', '0', '3']
26. Write the output of the following Python program code:
L = "neat"
X = 1,11

Ll = []
count=l
for i in L:
if i in ['a','e','i','o','u']:
X = X + i.swapcase()
else:
if((count%2)!=0):
X = X + str((len(L[:count])))
else:
X=X+i
count = count + 1
print(X)
Ans. lEAt
27. WAP to create a list L with integer elements as given and search a particular value in the list. If it exists,
then the function should display "exists", otherwise it should display "Does not exist".
Input: [10,20,3,100,65,87,2]
Output: >>> Enter the number to be searched :75
Does not exist
>>> Enter the number to be searched :100
Exists at position :4
Ans. L=[10,20,3,100,65,87,2]
N=int(input("Enter the number to be searched:"))
for i in range(len(L)):
if type(L[i])==int and L[i]==N:
print("Exists at position:",i+1)
break
else:
print("Does not exist")
32. Consider a list list1= [1, 2,3,4,5].
What would be displayed for the following code:
(a) listl+2
(b) listl*3
(c) list1+ [10]
Ans. (a) TypeError: can only concatenate list (not "int") to list
(b) [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
(c) [1, 2, 3, 4, 5, 10]
33. WAP to display square of an element if it is an integer and change the case if the element is a string.
L = [10, "FUN", 40, "FEW", 50, "FULL"] .
List L contains both numbers and strings.
Ans. L= [ 10 , "FUN",40,"FEW",50,"FULL"]
for i in range(len(L)):
if type (L[i] )==int:
L[i]= L[i]**2
elif type (L [i]==str) :
L[i] = (L[i]).swapcase()
print (L)
Output:
[100, 'fun', 1600, 'few', 2500, 'full']
34. WAP in Python to display those strings which are starting with character 'A' or 'a' from the given list L.
L= [ "RINKU" , "AUSHIM", "VIJAYA", "AKHTAR", "LEENA", "TARUN", "AMAR"]
Ans. L=["RINKU","AUSHIM","VIJAYA","AKHTAR","LEENA","TARUN","AMAR"]
count =0
for i in L:
if i[0] in ('aA'):
count+=1
print(i)
print("Appearing",count,"times")
Output:
AUSHIM
AKHTAR
AMAR
Appearing 3 times
35. WAP to find and display the sum of all the values which are ending with 3 from a list.
Sample list is: [33,13,92,99,3,12]
Output will be 49.
Ans. L=[33,13,92,99,3,12]
sum=0
x=len (L)
for i in range (0,x) :
if type (L [i] ) ==int:
if L[i]%10==3:
sum+=L [i]
print(sum)
36. A list num contains the following elements:
3,21,5,6,14,8,14,3
WAP to swap the content with the next value divisible by 7 so that the resultant list looks like:
[3,5,21,6,8,14,3,14]
Ans. num=[3,21,5,6,14,8,14,3]
L=len (num)
i=0
while i<L:
if num[i]%7==0:
num[i], num[i+1]= num[i+1], num[i]
i=i+2
else:
i=i+1
print(num)
37. WAP to shift the negative numbers to the right and the positive numbers to the left so that the resultant
list looks like:
Original list: [-12,11, -13, -5,6, -7,5,-3,-6]
Output should be: [11,6,5,-6,-3,-7,-5,-13,-12]
Ans. 1=[-12, 11, -13, -5, 6, -7, 5, -3, -6]
n=len (1)
print("Original list:",1)
for i in range (n-1) :
for j in range (n-i-1) :
if l[j]<0:
l[j],l[j+1]=1[j+1],l[j]
print ("After shifting list is :",1)
38. Consider List1=[1,2,3,4,5,6,7,8,9,10]
Write code in Python to:
(a) Remove last three elements from the list.
(b) Add element 23 at index value 10.
(c) Display the list in sorted order.
Ans. (a) del Listl [-3:]
(b) Listl . insert (10,23)
(c) print (Listl.sort())
39. Write a program to display all the state names from a list of states which start with the alphabet M.
Output should be:
Enter the list = ["MP", "UP", "WB", "TN", "MH","MZ","DL", "BH", "RJ", "HR"]
The states with letter M:
MP
MH
MZ
Ans. a=eval (input ( 'Enter the list = '))
L=len (a)
print ( 'The states with letter M : ')
for i in range (L) :
if a[i] [0] in 'Mmt:
print (a [i] )
else:
pass

You might also like