You are on page 1of 31

CHAPTER 2 –REVISION TOUR – II

[STRINGS, LISTS, TUPLES, DICTIONARY]

PRACTICE QUESTIONS

STATE TRUE OR FALSE


1. String is mutable data type.
2. Python considered the character enclosed in triple quotes
as String.
3. String traversal can be done using „for‟ loop only.
4. Strings have both positive and negative indexes.
5. The find() and index() are similar functions.
6. Indexing of string in python starts from 1.
7. The Python strip() method removes any spaces or specified
characters at the start and end of a string.
8. The startswith() string method checks whether a string
starts with a particular substring.
9. "The characters of string will have two-way indexing."
ASSERTION & REASONING
1. A: Strings are immutable.
R: Individual elements cannot be changed in string in
place.
2. A: String is sequence datatype.
R: A python sequence is an ordered collections of
items where each item is indexed by an integer.
3. A: a=‟3‟
b=‟2‟
c=a+b
The value of c will be 5
R: „+‟ operator adds integers but concatenates strings
4. A:
a = "Hello"
b = "llo"
c=a-b
print(c)
This will lead to output: "He"
R: Python string does not support - operator
5. A:
str1="Hello" and str1="World" then print(str1*3) will give
error.
R : * replicates the string hence correct output will be
HelloHelloHello
6. A: str1=”Hello” and str1=”World” then
print(„wo‟ not in str) will print false
R : not in returns true if a particular substring is not
present in the specified string.
7. A: The following command >>>"USA12#'.upper()
will return False.
R: All cased characters in the string are uppercase and
requires that there be at least one cased character
8. A: Function isalnum() is used to check whether characters
in the string are alphabets or numbers.
R: Function isalnum() returns false if it either contains
alphabets or numbers
9. A: The indexes of a string in python is both forward and
backward.
R: Forward index is from 0 to (length-1) and backward
index
is from -1 to (-length)
10. A: The title() function in python is used to convert the first
character in each word to Uppercase and remaining
characters to Lowercase in the string and returns an
updated string.
R: Strings are immutable in python.
OBJECTIVE TYPE QUESTIONS (MCQ)
1. What is the output of 'hello'+1+2+3?
(a) hello123 (b) hello6 (c) Error (d) Hello+6
2. If n=”Hello” and user wants to assign n[0]=‟F‟ what will be
the result?
(a) It will replace the first character
(b It‟s not allowed in Python to assign a value to an
individual character using index
(c) It will replace the entire word Hello into F
(d) It will remove H and keep rest of the characters
3. In list slicing, the start and stop can be given beyond
limits. If it is then:
(a) Raise exception IndexError
(b) Raise exception ValueError
(c) Return elements falling between specified start and stop
values
(d) Return the entire list
4. Select the correct output of the code:
Str=“Computer”
Str=Str[-4:]
print(Str*2)
a. uter b. uterretu c. uteruter d. None of these
5. Given the Python declaration s1=”Hello”. Which of the
following statements will give an error?
(a) print(s1[4]) (b) s2=s1 (c) s1=s1[4] (d) s1[4]=”Y”
6. Predict the output of the following code:
RS=''
S="important"
for i in S:
RS=i+RS
print(RS)
7. What will the following code print?
>>> print(max("zoo 145 com"))
(a) 145 (b) 122 (c) z (d) zoo
8. Ms. Hetvee is working on a string program. She wants to
display last four characters of a string object named s.
Which of the following is statement is true?
a. s[4:] b. s[:4] c. s[-4:] d. s[:-4]
9. What will be the output of following code snippet:
msg = “Hello Friends”
msg [ : : -1]
a)Hello b) Hello Friend c) 'sdneirF olleH' d) Friend
10. Suppose str1= „welcome‟. All the following expression
produce the same result except one.Which one?
(a) str[ : : -6] (b) str[ : : -1][ : : -6] (c) str[ : :6] (d) str[0] + str[-
1]
11. Consider the string state = "Jharkhand". Identify the
appropriate statement that will display the lastfive
characters of the string state?
(a) state [-5:] (b) state [4:] (c) state [:4] (d) state [:-4]
12. Which of the following statement(s) would give an error
during execution?
S=["CBSE"] # Statement 1
S+="Delhi" # Statement 2
S[0]= '@' # Statement 3
S=S+"Thank you" # Statement 4
(a) Statement 1 (b) Statement 2
(c) Statement 3 (d) Statement 4
13. Select the correct output of the following string operations
mystring = "native"
stringList = ["abc","native","xyz"]
print(stringList[1] == mystring)
print(stringList[1] is mystring)
a) True b) False c) False d) Ture
False True Flase Ture
14. Select the correct output of the code :
n = "Post on Twitter is trending"
q = n.split('t')
print( q[0]+q[2]+q[3]+q[4][1:] )
(a)Pos on witter is rending (b)Pos on Twitter is ending
(c) Poser witter is ending (d) Poser is ending
15. What will be the result of the following command:-
>>>"i love python".capitalize()
(a) 'I Love Python' (b) 'I love python'
(c) 'I LOVE PYTHON' (d) None of the above
16. The number of element return from the partition function
is: >>>"I love to study Java".partition("study")
(a) 1 (b) 3 (c) 4 (d) 5
17. Predict the output of the following:
S='INDIAN'
L=S.partition('N')
G=L[0] + '-' + L[1] + '-' + L[2]
print(G)
(a) I-N-DIA-N (b) I-DIA- (c) I-DIAN (d) I-N-DIAN
18. What will be the output of the following command:
>>>string= "A quick fox jump over a lazy fox"
>>>string.find("fox",9,34)
(a) 28 (b) 29 (c) 8 (d) 7
19. Select the correct option for output
X="abcdef"
i='a'
while i in X:
print(i, end=" ")
(a) Error (b) a (c) infinite loop (d) No output
20. What is the output of the following code?
S1="Computer2023"
S2="2023"
print(S1.isdigit(), S2.isdigit())
a)False True b) False False
c) True False d) True True
21. What will be the output of the following statement given:
txt="cbse. sample paper 2022"
print(txt.capitalize())
a) CBSE. sample paper 2022 b) CBSE. SAMPLE SAPER
2022
c) cbse. sample paper 2022 d) Cbse. Sample Paper 2022
22. Select the correct output of the code:
a = "Year 2022 at all the best"
a = a.split('a')
b = a[0] + "-" + a[1] + "-" + a[3]
print (b)
a) Year – 0- at All the best b) Ye-r 2022 -ll the best
c) Year – 022- at All the best d) Year – 0- at all the best
23. Select the correct output of the code:
mystr = „Python programming is fun!‟
mystr = mystr.partition('pro')
mystr='$'.join(mystr)
(a) Python $programming is fun!
(b) Python $pro$gramming is fun!
(c) Python$ pro$ gramming is fun!$
(d) P$ython $p$ro$gramming is $fun!
24. Identify the output of the following Python statement:
s="Good Morning"
print(s.capitalize( ),s.title( ), end="!")
(a) GOOD MORNING!Good Morning
(b) Good Morning! Good Morning
(c) Good morning! Good Morning!
(d) Good morning Good Morning!
25. What is the output of the following code?
Str1="Hello World"
Str1.replace('o','*')
Str1.replace('o','*')
(a) Hello World (b) Hell* W*rld
(c) Hello W*rld (d) Error
26. State whether the statement is True or False?
No matter the underlying data type, if values are equal
returns true.
27. Predict the output of the following:
S="Hello World!"
print(S.find("Hello"))
(a) 0 (b) 3 (c) [2,3,7] (d) (2,3,7)
28. Which of the following is not a valid Python String
operation?
(a) 'Welcome'+'10' (b)'Welcome'*10
(c) 'Welcome'*10.0 (d) '10'+'Welcome'
29. Which of the following statement(s) would give an error
after executing the following code?
EM = "Blue Tick will cost $8" # Statement 1
print( EM) # Statement 2
ME = "Nothing costs more than Truth" # Statement 3
EM *= 2 # Statement 4
EM [-1: -3 : -1] += ME[ : 7] # Statement 5
(a) Statement 3 (b) Statement 4
(c) Statement 5 (d) Statement 4 and 5
30. Select the correct output of the following code :
s="I#N#F#O#R#M#A#T#I#C#S"
L=list(s.split("#"))
print(L)
(a) [I#N#F#O#R#M#A#T#I#C#S]
(b) [„I‟, „N‟, „F‟, „O‟, „R‟, „M‟, „A‟, „T‟, „I‟, „C‟, „S‟]
(c) ['I N F O R M A T I C S']
(d) ['INFORMATICS']
31. STRING="WELCOME" #Line1
NOTE= " " #Line2
for S in range[0,8] #Line3
print(STRING[S]) #Line4
print(S STRING) #Line5
Which statement is wrong in above code:
a) Line3 and Line4 b) Line4 and Line5
c) Line2 and Line3 d) Line3 and Line5
32. Consider the following code that inputs a string and
removes all special characters from it after converting it to
lowercase.
s = input("Enter a string")
s = s.lower()
for c in ',.;:-?!()\'"':
_________________
print(s)
For eg: if the input is 'I AM , WHAT I AM", it should print
i am what i am
Choose the correct option to complete the code .
a. s = s.replace(c, ' ') b. s = s.replace(c, '\0')
c. s = s.replace(c, None) d. s = s.remove(c)
33. Which of the following statement(s) would give an error
after executing the following code?
str= "Python Programming" #S1
x= '2' #S2
print(str*2) #S3
print(str*x) #S4
(a) S1 (b) S2 (c) S3 (d) S4
2 –MARKS / 3 - MARKS
1. If S="python language" is a Python string, which method
will display the following output 'P' in uppercase and
remaining in lower case?
2. Rewrite the following code :
for Name in [Aakash, Sathya, Tarushi]
IF Name[0]= 'S':
print(Name)
3. Consider the given Python string declaration:
>>> mystring = 'Programming is Fun'
>>> printmystring[-50:10:2].swapcase())
4. Rewrite the following:
STRING=""WELCOME
NOTE = " "
for S in range(0,8):
if STRING[S]= ‟E‟:
print(STRING(S))
Else:
print "NO"
5. Given is a Python string declaration:
>>>Wish = "## Wishing All Happy Diwali @#$"
Write the output of >>>Wish[ -6 : : -6 ]
6. Predict the output of the following:
Name = "cBsE@2051"
R=" "
for x in range (len(Name)):
if Name[x].isupper ( ):
R = R + Name[x].lower()
elif Name[x].islower():
R = R + Name[x].upper()
elif Name[x].isdigit():
R = R + Name[x-1]
else:
R = R + "#"
print(R)
7. Predict the output of the following:
>>> 'he likes tea very much'.rstrip('crunch')
>>> 'There is a mango and an apple on the table'.rstrip('ea')
7. Find the output
Msg1="WeLcOME"
Msg2="GUeSTs"
Msg3=""
for I in range(0,len(Msg2)+1):
if Msg1[I]>="A" and Msg1[I]<="M":
Msg3=Msg3+Msg1[I]
elif Msg1[I]>="N" and Msg1[I]<="Z":
Msg3=Msg3+Msg2[I]
else:
Msg3=Msg3+"*"
print(Msg3)
8. Predict the output of the following:
s="3 & Four"
n = len(s)
m=""
for i in range(0, n):
if (s[i] >= 'A' and s[i] <= 'Z'):
m = m +s[i].upper()
elif (s[i] >= 'a' and s[i] <= 'z'):
m = m +s[i-1]
if (s[i].isdigit()):
m = m + s[i].lower()
else:
m = m +'-'
print(m)
9. Name="PythoN3.1"
R=" "
for x in range(len(Name)):
if Name[x].isupper():
R=R+Name[x].lower()
elif Name[x].islower():
R=R+Name[x].upper()
elif Name[x].isdigit():
R=R+Name[x-1]
else:
R=R+"#"
print(R)
10. Find output generated by the following code:
string="aabbcc"
count=3
while True:
if string[0]=='a':
string=string[2:]
elif string[-1]=='b':
string=string[:2]
else:
count+=1
break
print(string)
print(count)
11. Write the output of the code snippet.
txt = "Time, will it come back?"
x = txt.find(",")
y = txt.find("?")
print(txt[x+2:y])
12. Predict the output of the following:
S='Python Programming'
L=S.split()
S=','.join(L)
print(S)
13. Predict the output of the following:
S="Good Morning Madam"
L=S.split()
for W in L:
if W.lower()==W[::-1].lower()
print(W)
*********************************************************
TOPIC - LIST
STATE TRUE OR FALSE
1. List immutable data type.
2. List hold key and values.
3. Lists once created cannot be changed.
4. The pop() and remove() are similar functions.
5. The append() can add an element in the middle of the list.
6. Del statement deletes sub list from a list.
7. Sort() and sorted() both are similar function.
8. l1=[5,7,9,4,12] 5 not in l1
9. List can be extend
10. Mutable means only we can insert elements
11. Index of last element in list is n-1, where n is total number
of elements.
12. We can concatenate only two list at one time.
13. L.pop() method will return deleted element immediately
14. L.insert() method insert a new element at the beginning of
the list only.
15. If index is not found in insert() I.e. L.insert(102,89) then it
will add 89 at the end of the list.
16. L.reverse() will accept the parameters to reverse the
elements of the list.
17. L.sort(reverse=1) will reverse the list in descending order
18. L.pop() used to delete the element based on the element of
the list.
19. L.clear() used to clear the elements of the list but memory
will exists after clear.
20. L.append() append more than one element at the end of the
list.
21. L=[10,20,30]
L.extend([90,100])
print(L)
output of the above code is:
[10,20,30,[90,100]]
22. After sorting using L.sort() method, the index number of
the list element will change.
23. L.remove() used to delete the element of the list based on
its value, if the specified value is not there it will return
Error.
ASSERTION & REASONING
1. A: list is mutable data Type
R: We can insert and delete as many elements from list
2. A: Elements can be added or removed from a list
R: List is an immutable datatype.
3. A: List can be changed after creation
R: List is mutable datatype.
4. A: Following code will result into output : True
a=[10,20]
b=a[:]
print( a is b)
R: "is" is used for checking whether or not both the
operands is using same memory location
5. A: list() is used to convert string in to list element
R: string will be passed into list() it will be converted into
list elements
6. A: reverse() is used to reverse the list elements.
Reason: it can reverse immutable data type
elements also
7. A: pop() can delete elements from the last index of list
R: this function can remove elements from the last index
only.
8. A: len() displays the list length
R: it is applicable for both mutable and immutable data
type.
9. A: Usha wants to sort the list having name of students of
her class in descending order using sorted() function
R: sorted() function will sort the list in ascending order
10. A: Raju is working on a list of numbers. He wants to print
the list values using traversing method. He insists that
list can be traversed in forward direction and backward
direction i.e. from beginning to last and last to
beginning.
R: List can be traversed from first element to last element
only.
11. A: Anu wants to delete the elements from the list using
pop() function.
R: Her friend Pari suggested her to use del () function to
delete more than one elements from the list.
12. A: Students in the Lab practical were asked to store
the elements in a List data type in which we can perform
all the operations like addition , deletion , deletion ,
traversal.
R: List is mutable data type
OBJECTIVE TYPE QUESTIONS(MCQ)
1. Which point can be considered as difference between string
and list?
(a) Length (b) Indexing and Slicing
(c) Mutability (d) Accessing individual elements
2. What is the output when we execute list(“hello”)?
(a) [„h‟, „e‟, „l‟, „l‟, „o‟] (b) [„hello‟] (c) [„llo‟] (d) [„olleh‟].
3. Given a List L=[7,18,9,6,1], what will be the output of
L[-2::-1]?
(a) [6, 9, 18, 7] (b) [6,1] (c) [6,9,18] (d) [6]
4. If a List L=['Hello','World!'] Identify the correct output of the
following Python command: >>> print(*L)
(a) Hello World! (b) Hello,World!
(c)'Hello','World!' (d) 'Hello','World!'
5. Consider the list aList=[ "SIPO", [1,3,5,7]]. What would the
following code print?
(a) S, 3 (b) S, 1 (c) I, 3 (d) I, 1
6. Suppose list1 is [1, 3, 2], What is list1 * 2 ?
a) [2, 6, 4] (b) [1, 3, 2, 1, 3]
(c) [1, 3, 2, 1, 3, 2] (d) [1, 3, 2, 3, 2, 1]
7. Suppose list1 = [0.5 * x for x in range(0,4)], list1 is
a) [0, 1, 2, 3] b) [0, 1, 2, 3, 4]
c) [0.0, 0.5, 1.0, 1.5] d) [0.0, 0.5, 1.0, 1.5, 2.0]
8. >>> L1=[6,4,2,9,7]
>>> print(L1[3:]= "100"
(a) [6,4,2,9,7,100] (b) [6,4,2,100]
(c) [6,4,2,1,0,0] (d) [6,4,2, „1‟,‟0‟,‟0‟]
9. Which statement is not correct
a)The statement x = x + 10 is a valid statement
b)List slice is a list itself.
c)Lists are immutable while strings are mutable.
d)Lists and strings in pythons support two way indexing
10. Which one is the correct output for the following code?
a=[1,2,3,4]
b=[sum(a[0:x+1])
for x in range(0,len(a)):
print (b)
(a) 10 (b) [1,3,5,7] (c) 4 (d) [1,3,6,10]
11. What will be the output for the following python code:
L=[10,20,30,40,50]
L=L+5
print(L)
(a) [10,20,30,40,50,5] (b) [15,25,35,45,55]
(c) [5,10,20,30,40,50] (d) Error
12. Select the correct output of the code:
L1, L2 = [10, 23, 36, 45, 78], [ ]
for i in range :
L2.insert(i, L1.pop( ) )
print( L1, L2, sep=‟@‟)
(a) [78, 45, 36, 23, 10] @ [ ]
(b) [10, 23, 36, 45, 78] @ [78, 45, 36, 23, 10]
(c) [10, 23, 36, 45, 78] @ [10, 23, 36, 45, 78]
(d) [ ] @ [78, 45, 36, 23, 10]
13. The append() method adds an element at
(a) First (b) Last (c) Specified index (d) At any
location
14. The return type of x in the below code is
txt = "I could eat bananas all day"
x = txt.partition("bananas")
(a) string (b) List (c) Tuple (d) Dictionary
15. S1: Operator + concatenates one list to the end of another
list.
S2: Operator * is to multiply the elements inside the list.
Which option is correct?
(a) S1 is correct, S2 is incorrect
(b) S1 is incorrect, S2 is incorrect
(c) S1 is incorrect, S2 is incorrect
(d) S1 is incorrect, S2 is correct
16. Which of the following statement is true for extend() list
method?
(a) Adds element at last
(b) Adds multiple elements at last
(c) Adds element at specified index
(d) Adds elements at random index
17. What is the output of the following code?
a=[1,2,3,4,5]
for i in range(1,5): (a) 5 5 1 2 3 (b) 5 1 2 3 4
a[i-1]=a[i]
for i in range(0,5): (c) 2 3 4 5 1 (d) 2 3 4 5 5
print(a[i],end=" ")

18. What is the output of the functions shown below?


min(max(False,-3,-4),2,7)
(a) -3 (b) -4 (c) True (d) False
19. If we have 2 Python Lists as follows:
L1=[10,20,30]
L2=[40,50,60]
If we want to generate a list L3 as:
then best Python command out of the following is:
(a) L3=L1+L2 (b) L3=L1.append(L2)
(c) L3=L1.update(L2) (d) L3=L1.extend(L2)
20. Identify the correct output of the following Python code:
L=[3,3,2,2,1,1]
L.append(L.pop(L.pop()))
print(L)
(a) [3,2,2,1,3] (b)[3,3,2,2,3] (c)[1,3,2,2,1] (d)[1,1,2,2,3,3]
21. Predict the output of the following:
L1=[1,4,3,2]
L2=L1
L1.sort()
print(L2)
(a) [1,4,3,2] (b)[1,2,3,4] (c) 1 4 3 2 (d) 1 2 3 4
22. If PL is Python list as follows:
PL=['Basic','C','C++']
Then what will be the status of the list PL after
PL.sort(reverse=True) ?
(a) ['Basic','C','C++'] (b) ['C++','C','Basic']
(c) ['C','C++','Basic'] (d) PL=['Basic','C++','C']
23. Given list1 = [34,66,12,89,28,99]
Statement 1: list1.reverse()
Statement 2: list1[::-1]
Which statement modifies the contents of original list1.
(a) Statement 1 (b) Statement 2
(c) Both Statement 1 and 2. (d) None of the mentioned
24. which command we use can use To remove string "hello"
from list1, Given, list1=["hello"]
(a) list1.remove("hello") (b) list1.pop(list1.index('hello'))
(c) Both A and B (d) None of these
25. How would you create a loop to iterate over the contents of
the list given as Days = [31, 28, 31, 30, 31, 30, 31, 31,30]
and print out each element?
(a) for days in range(Days):
print(days)
(b) for days in Days:
print(days)
(c) for days in range(len(Days)):
print(days)
(d) for days in Days:
print(monthDays)
25. Predict the output of the following:
S=[['A','B'],['C','D'],['E','F']]
print(S[1][1])
(a)'A' (b) 'B' (c) 'C' (d) 'D'
26. Predict the output of the following:
L1=[1,2,3]
L2=[4,5]
L3=[6,7]
L1.extend(L2)
L1.append(L3)
print(L1)
(a) [1,2,3,4,5,6,7] (b)[1,2,3,[4,5],6,7]
(c) [1,2,3,4,5,[6,7]] (d)[1,2,3,[4,5],[6,7]]
27. State True or False "There is no conceptual limit to the size
of a list."
28. The statement del l[1:3] do which of the following task?
a. deletes elements 2 to 4 elements from the list
b. deletes 2nd and 3rd element from the list
c. deletes 1st and 3rd element from the list
d. deletes 1st, 2nd and 3rd element from the list
29. Which of the following will give output as: [23, 2, 9, 75]?
If list1=[6,23,3,2,0,9,8,75]
(a) print(list1[1:7:2]) (b) print(list1[0:7:2])
(c) print(list1[1:8:2]) (c) print(list1[0:8:2])
30. Find the output
values = [[3, 4, 5, 1 ], [33, 6, 1, 2]]
for row in values:
row.sort()
for element in row:
print(element, end = " ")
print()
(a). The program prints on row 3 4 5 1 33 6 1 2
(b). The program prints two rows 3 4 5 1
followed by 33 6 1 2
(c). The program prints two rows 3 4 5 1
followed by 33 6 1 2
(d). The program prints two rows 1 3 4 5
followed by 1 2 6 33
2 MARKS / 3 MARKS
1. (a) >>>L=[1,"Computer",2,"Science",10,"PRE",30,"BOARD"]
>>>print(L[3:])
(b) Identify the output of the following Python statements.
>>>x = [[10.0, 11.0, 12.0],[13.0, 14.0, 15.0]]
>>>y = x[1][2]
>>>print(y)
2. Write the output of following code:
lst1=[10,15,20,25,30]
lst1.insert(3,4)
lst1.insert(2,3)
print(lis[-5])
3. Write output for the following code:
list1=[x+2 for x in range(5)]
print(list1)
4. What will be the output of the following code?
a=[1,2,3,4]
s=0
for a[-1] in a:
print(a[-1])
s+=a[-1]
print(„sum=‟,s)
5. Predict the output of the following:
(i) L=[10,20]
L1=[30,40]
L2=[50,60]
L.append(L1)
L.extend(L2)
print(L)
5. Predict the output of the following:
(ii) M=[11, 22, 33, 44]
Q=M
M[2]+=22
L=len(M)
for i in range (L):
print("Now@", Q[L-i-1], "#", M[i])
6. Predict the output of the Python code given below:
l=[ ]
for i in range(4):
l.append(2*i+1)
print(l[::-1])
7. Predict the output of the following:
TXT = ["10","20","30","5"]
CNT = 3
TOTAL = 0
for C in [7,5,4,6]:
T = TXT[CNT]
TOTAL = float (T) + C
print (TOTAL)
CNT-=1
8. Write the output of the following:
x = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
result = []
for items in x:
for item in items:
if item % 2 == 0:
result.append(item)
print(result)
9. Write the output of the following python code:
Numbers =[9,18,27,36]
for num in Numbers:
for N in range(1,num%8):
print(N,"$",end="")
print()
10. Predict the output of the following:
List1 = list("Examination")
List2 =List1[1:-1]
new_list = []
for i in List2:
j=List2.index(i)
if j%2==0:
List1.remove(i)
print(List1)
11. Predict the output of the following:
fruit_list1 = ['Apple', 'Berry', 'Cherry', 'Papaya']
fruit_list2 = fruit_list1
fruit_list3 = fruit_list1[:]
fruit_list2[0] = 'Guava'
fruit_list3[1] = 'Kiwi'
sum = 0
for ls in (fruit_list1, fruit_list2, fruit_list3):
if ls[0] == 'Guava':
sum += 1
if ls[1] == 'Kiwi':
sum += 20
print(sum)
12. Predict the output of the following:
L1,L2=[10,15,20,25],[ ]
for i in range(len(L1)):
L2.insert(i,L1.pop())
print(L1,L2,sep='&')
14. Predict the output of the following:
L1=[10,20,30,20,10]
L2=[ ]
for i in L1:
if i not in L2:
L2.append(i)
print(L1,L2,sep='$')
15. Predict the output of the following:
num=[10,20,30,40,50,60]
for x in range(0,len(num),2):
num[x], num[x+1]=num[x+1], num[x]
print(num)
16. Predict the output of the following:
L=[1,2,3,4,5]
Lst=[]
for i in range(len(L)):
if i%2==1:
t=(L[i],L[i]**2)
Lst.append(t)
print(Lst)
17. Predict the output of the following:
sub=["CS", "PHY", "CHEM", "MATHS","ENG"]
del sub[2]
sub.remove("ENG")
sub.pop(1)
print(sub)
18. Predict the output of the following:
Predict the output:
list1 =['Red', 'Green', 'Blue', 'Cyan','Magenta','Yellow',]
print(list1[-4:0:-1])
19. Predict the output of the following:
L=[10,20,40,50,60,67]
L[0:3]="100"
print(L)
20. Predict the output of the following:
lst = [40, 15, 20, 25, 30]
lst.sort()
lst.insert(3, 4)
lst.append(23)
print(lst)
21. Predict the output of the following:
L=[10,2,5,-1,90,23,45]
L.sort(reverse=True)
S=L.pop(2)
L.insert(4,89)
L.sort()
L.remove(90)
print(L)
22. Predict the output of the following:
L=[10,2,5,-1,90,23,45]
L.sort(reverse=True)
G=L.append(L.pop(L.pop()))
print(G)
23. Predict the output of the following:
L=['30','40','20','50']
count=3
Sum=0
P=[]
for i in[6,4,5,8]:
T=L[count]
Sum=float(T)+i
P.append(Sum)
count-=1
print(P)
print(P[3:0:-1])
for i in[0,1,2,3]:
P[i]=int(P[i])+10
print(P)
24. Predict the output of the following:
L=['FORTRAN','C++','Java','Python']
L.insert(2,"HTML")
del L[4]
L.remove('Java')
L.pop()
L.insert(1,"PHP")
L.sort()
L.pop()
print(L)
*********************************************************
TOPIC - TUPLES
STATE TRUE OR FALSE
1. Tuple data structure is mutable
2. tuples allowed to have mixed lengths.
3. tuple have one element for Ex: S=(10)
4. We Can concatenate tuples.
5. The elements of the of a tuple can be deleted
6. A tuple storing other tuples is called as nested tuple
7. The + Operator adds one tuple to the end of another tuple
8. A tuple can only have positive indexing.
ASSERTION & REASONING
1. A: Tuples are ordered
R: Items in a tuple has an defined order that will not be
changed.
2. A: A tuple can be concatenated to a list, but a list cannot
be concatenated to a tuple.
R: Lists are mutable and tuples are immutable in Python
3. A: Tuple is an in inbuilt data structure
R: Tuple data structure is created by the programmers and
not by the creator of the python
4. a=(1,2,3)
a[0]=4
A: The above code will result in error
R: Tuples are immutable. So we cant change them.
5. A: A tuple cannot be sorted inline in python and therefore
sorted () returns a sorted list after sorting a tuple.
R: Tuples are non-mutable data type
6. >>> tuple1 = (10,20,30,40,50)
>> tuple1.index(90)
A: Above statements gives ValueError in python
R: ValueError: tuple.index(x): x not in tuple
7. T1 = (10, 20, (x, y), 30)
print(T1.index(x))
A: Above program code displays the index of the element „x‟
R: Returns the index of the first occurrence of the
element in the given tuple
8. >>>tuple1 = (10,20,30,40,50,60,70,80)
>>>tuple1[2:]
Output: (30, 40, 50, 60, 70, 80)
A: Output given is incorrect
R: Slicing in above case ends at last index
9. A:
t=(10,20,30)
t[1]=45
the above mentioned question will throws an error.
R: Tuple are used to store sequential data in a program
10. A: Forming a tuple from individual values is called packing
R: Many individual data can be packed by assigning them
to one single variable, where data is separated by
comma.
OBJECTIVE TYPE QUESTIONS (MCQ)
1. Which of the following statement creates a tuple?
a) t=[1,2,3,4] b) t={1,2,3,4} c) t=<1,2,3,4> d)
t=(1,2,3,4)
2. Which of the following operation is supported in python
with respect to tuple t?
a) t[1]=33 b) t.append(33) c) t=t+t d) t.sum()
3. Which of the following is not a supported operation in
Python?
(a) “xyz”+ “abc” (b) (2)+(3,) (c) 2+3 (d) [2,4]+[1,2]
4. Predict the output:
T=('1')
print(T*3)
(a) 3 (b) ('1','1','1') (c) 111 (d) ('3')
5. Identify the errors in the following code:
MyTuple1=(1, 2, 3) #Statement1
MyTuple2=(4) #Statement2
MyTuple1.append(4) #Statement3
print(MyTuple1, MyTuple2) #Statement4
(a) Statement 1 (b) Statement 2
(c) Statement 3 (d) Statement 2 &3
6. Suppose a tuple Tup is declared as Tup = (12, 15, 63, 80)
which ofthe following is incorrect?
(a) print(Tup[1]) (b) Tup[2] = 90
(c) print(min(Tup)) (d) print(len(Tup))
7. Predict the output of the following code:
t1=(2,3,4,5,6)
print(t1.index(4))
(a) 4 (b) 5 (c) 6 (d) 2
8. Given tp = (1,2,3,4,5,6). Which of the following two
statements will give the same output?
1. print(tp[:-1]) # Statement 1
2. print(tp[0:5]) # Statement 2
3. print(tp[0:4]) # Statement 3
4. print(tp[-4:]) # Statement 4
(a) Statement 1 and 2 (b) Statement 2 and 4
(c) Statement 1 and 4 (d) Statement 1 and 3
9. What will be the output for the following Python
statement?
T=(10,20,[30,40,50],60,70)
T[2][1]=100
print(T)
(a) (10,20,100,60,70) (b) 10,20,[30,100,50],60,70)
(c) (10,20,[100,40,50],60,70) (d) None of these
10. Which of the following is an unordered collection of
elements
(a) List (b) Tuple (c) Dictionary (d) String
11. Consider the following statements in Python and the
question that follows:
i. Tuple is an ordered list
ii. Tuple is a mutable data type
From the above statements we can say that
(a) Only (i) is correct
(b) Only (ii) is correct
(c) Both (i) and (ii) are correct
(d) None of (i) and (ii) are correct
12. Which of the following is not a tuple in Python?
(a) (1,2,3) (b) (“One”,”Two”,”Three”) (c) (10,) (d) (“One”)
13. Suppose a tuple T is declared as T=(10,20,30) and a list
L=["mon", "tue", "wed","thu", "fri", "sat", "sun"], which of the
following is incorrect ?
a) min(L) b) L[2] = 40 c) T[3] = “thurs” d) print(
min(T))
14. Suppose a Python tuple T is declared as:
T=('A','B','C')
Which of the following command is invalid?
(a) T=('D') (b) T=('D',) (c) T+=('D') (d) T+=('D',)
15. Predict the output:
tup1 = (2,4,[6,2],3)
tup1[2][1]=7
print(tup1)
(a)Error (b) (2,4,[6,2],3) (c)(2,4,[6,7],3) (d)(2,4,6,7,3)
16. Consider a tuple t=(2,4,5), which of the following will give
error?
(a) list(t)[-1]=2 (b) print(t[-1]) (c) list(t).append(6) (d) t[-1]=7
17. Suppose a tuple T1 is declared as T1 = (10, 20, 30, 40, 50)
which of the following is incorrect?
a)print(T[1]) b) T[2] = -29 c) print(max(T)) d) print(len(T))
18. What will be the output of the following Python code ?
T1= ( )
T2=T1 * 2
print(len(T2))
(a) 0 (b) 2 (c) 1 (d) Error
19. Predict the output:
Tup=(3,1,2,4)
sorted(Tup)
print(Tup)
(a) (3,1,2,4) (b) (1,2,3,4) (c) [3,1,2,4] (d) [1,2,3,4]
20. Predict the output of the following:
S=([1,2],[3,4],[5,6])
S[2][1]=8
print(T)
(a) ([1,2],[8,4],[5,6])
(b) ([1,2],[3,4],[8,6])
(c) ([1,2],[3,4],[5,8])
(d) Will generate an Error as a Tuple is immutable
2 MARKS / 3 MARKS
1. Predict the output of the following:
test_list = [5, 6, 7]
test_tup = (9, 10)
res = tuple(list(test_tup) + test_list)
print(str(res))
2. tuple1 = (5, 12, 7, 4, 9 ,6)
list1 =list(tuple1)
list1.insert(2,8)
list1.pop()
tuple1=tuple(list1)
print(tuple1)
3. Predict the output of the following code:
tuple1 = ( [7,6], [4,4], [5,9] , [3,4] , [5,5] , [6,2] , [8,4])
listy = list( tuple1)
new_list = list()
for elem in listy :
tot = 0
for value in elem:
tot += value
if elem.count(value) == 2:
new_list.append(value)
tot = 0
else:
print( tuple(new_list) )
4. Write the output of the code given below:
a,b,c,d = (1,2,3,4)
mytuple = (a,b,c,d)*2+(5**2,)
print(len(mytuple)+2)
5. Predict the output
******************************************************
TOPIC - DICTIONARY
STATE TRUE OR FALSE
1. Dictionary values are immutable
2. List can be converted in Dictionary
3. It is always not necessary to enclose dictionary in { }.
4. Dictionary keys are unique and always of string type
5. D.popitem() is used to delete the last items of the
dictionary.
6. D.get() method is used to insert new key:value pair inside
of the dictionary.
ASSERTION & REASONING
1. A: Dictionaries are mutable.
R: Individual elements can be changed in dictionary in
place.
2. A: Dictionary in Python holds data items in key-value pairs
R: Immutable means they cannot be changed after
creation.
3. A: Key of dictionary can‟t be changed.
R: Dictionary Key‟s are immutable.
4. A: Key of dictionary must be single element.
R: Key of dictionary is always string type.
5. A: Dictionary and set both are same because both enclosed
in { }.
R: Dictionary is used to store the data in a key-value pair
format.
6. A: The pop() method accepts the key as an argument and
remove the associated value
R: pop() only work with list because it is mutable.
7. A: The items of the dictionary can be deleted by using the
del keyword.
R: del is predefined keyword in python.
OBJECTIVE TYPE QUESTIONS (MCQ)
1. Which statement is correct for dictionary?
(a) A dictionary is a ordered set of key: value pair
(b) each of the keys within a dictionary must be unique
(c) each of the values in the dictionary must be unique
(d) values in the dictionary are immutable
2. Which of the following is the correct statement for checking
the presence of a key in the dictionary?
a) <key> in <dictionary_obj>
b) <key> not in <dictionary_obj>
c) <key> found in <dictionary_obj>
d) a) <key> exists in <dictionary_obj>
3. What will be the output for the following Python statements?
D= {“Amit”:90, “Reshma”:96, “Suhail”:92, “John”:95}
print(“John” in D, 90 in D, sep= “#”)
(a) True#False (b)True#True (c) False#True (d) False#False
4. Choose the most correct statement among the following –
(a) a dictionary is a sequential set of elements
(b) a dictionary is a set of key-value pairs
(c) a dictionary is a sequential collection of elements key-
value pairs
(d) a dictionary is a non-sequential collection of elements
5. What will be output of the following code:
d1={1:2,3:4,5:6}
d2=d1.popitem()
print(d2)
(a) {1:2} (b) {5:6} (c) (1,2) (d) (5,6)
6. Which of the following statements create a dictionary?
a) d = { } b) d = {"john":40, "peter":45}
c) d = {40: "john", 45: "peter"} d) All of the mentioned above
7. What will be output of following:
d = {1 : "SUM", 2 : "DIFF", 3 : "PROD"}
for i in d:
print (i)
8. Predict the output of the following:
D={1:"One",2:"Two",3:"Three"}
L=[ ]
for K,V in D.items():
if V[0]=="T":
L.append(K)
print(L)
(a) [1,2,3] (b) ["One","Two","Three"]
(d) [2,3] (d) ["Two","Three"]
9. Given the following dictionary
employee={'salary':10000,'age':22,'name':'Mahesh'}
employee.pop('age')
print(employee)
What is output?
a. {„age‟:22}
b. {'salary': 10000, 'name': 'Mahesh'}
c. {'salary':10000,'age':22,'name':'Mahesh'}
d. None of the above
10. D={'A':1, 'B':2, 'C':3}
Then, which of the following command will remove the
entire dictionary from the memory?
(a) del(D) (b) D.del() (c) D.clear() (d) D.remove()
11. Predict the output of the following:
D1={'A':5,'B':5,'C':9,'D':10}
D2={'B':5,'D':10}
D1.update(D2)
print(D1)
(a) {'A':5,'B':5,'C':9,'D':10} (b) {'A':5,'B':5,'C':9,'B':5,'D':10}
(c) {'A':5,'C':9,'D':10} (d) {'B':7,'D':10,'A':5,'C':9}
12. Predict the output of the following:
D1={1:2,2:3,3:4}
D2=D1.get(1,2)
print(D2)
(a) 2 (b) 3 (c) [2,3] (d) {1:2,2:3}
13. Which of the following operation(s) supported in
Dictionary?
(a) Comparison(only = and !=) (b) Membership
(c) Concatenation (d) Replication
14. Predict the output of the following:
D1={1:10,4:56,100:45}
D2={1:10,4:56,45:100}
print(D1==D2)
(a) True (b) False
(c) Cannot Compare the dictionaries (d) Error
15. Predict the output of the following:
D1={1:10,4:56,100:45}
D2={1:10,4:56,45:100}
print(D1<=D2)
(a) True (b) False
(c) Cannot Compare the dictionaries (d) Error
16. Write a python command to create list of keys from a
dictionary.
dict={'a':'A', 'b':'B','c':'C', 'd':'D'}
(a) dict.keys() (b) keys() (c) key.dict() (d) None of these
17. What will the following code do?
dict={“Phy”:94,”Che”:70,”Bio”:82,”Eng”:95}
dict.update({“Che”:72,”Bio”:80})
(a) It will create new dictionary as dict={“Che”:72,”Bio”:80}
and old dict will be deleted.
(b) It will throw an error as dictionary cannot be updated.
(c) It will simply update the dictionary as
dict={“Phy”:94,”Che”:72,”Bio”:80, “Eng”:95}
(d) It will not throw any error but it will not do any changes
in dict.
18. Consider the coding given below and fill in the blanks:
Dict_d={„BookName‟:‟Python‟,‟Author‟:”Arundhati Roy”}
Dict_d.pop() # Statement 1
List_L=[“java”,”SQL”,”Python”]
List_L.pop() # Statement 2
(i) In the above snippet both statement 1 and 2 deletes the
last element from the object Dict_d and
List_L________(True/False).
(ii) Statement_______(1/2) alone deletes the last element in
its object
19. Identify the output of following code
d={'a':'Delhi','b':'Mumbai','c':'Kolkata'}
for i in d:
if i in d[i]:
x=len(d[i])
print(x)
(a) 6 (b) 0 (c) 5 (d) 7
20. What will be the output of the following code?
D1={1: "One",2: "Two", 3: "C"}
D2={4: "Four",5: "Five"}
D1.update(D2)
print (D1)
a) {4: "Four",5: "Five"}
b) Method update() doesn‟t exist for dictionary
c) {1:"One",2:"Two", 3: "C"}
d) {1:"One",2: "Two",3: "C",4:"Four",5: "Five"}
21. Given the following dictionaries
Dict1={'Tuple': 'immutable', 'List': 'mutable', 'Dictionary':
'mutable', 'String': 'immutable'}
Which of the following will delete key:value pair for
key=„Dictionary‟ in the above mentioned dictionary?
(a) del Dict1['Dictinary']
(b) Dict1[„Dictionary‟].delete( )
(c) delete (Dict1.["Dictionary"])
(d) del(Dict1.[„Dictionary‟])
2 MARKS / 3 -MARKS
1. Write a statement in Python to declare a dictionary whose
keys are Sub1, Sub2, Sub3 and values are Physics,
Chemistry, Math respectively.
2. Debug the following code and underline the correction
made:
d=dict{ }
n=input("enter number of terms")
for i in range(n):
a=input("enter a character")
d[i]=a
3. Write the output of the code given below:
>>>squares = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
>>>print(squares.pop(4))
4. Write the output of the code given below :
Emp = { "SMITH":45000 , "CLARK":20000 }
Emp["HAPPY"] = 23000
Emp["SMITH"] = 14000
print( Emp.values())
5. What will be the output of the following Python code?
d1={"a":10,"b":2,"c":3}
str1=""
for i in d1:
str1=str1+str(d1[i])+" "
str2=str1[ : -1]
print(str2[: : -1])
6. Predict the output of the Python Code given below:
d={"Rohan":67,"Ahasham":78,"naman":89,"pranav":79}
print(d)
sum=0
for i in d:
sum+=d[i]
print(sum)
print("sum of values of dictionaries",sum)
7. Predict the output of
d={2:'b',4:'c'}
d1={1:'a'}
d.update(d1)
d[1]='v'
print(list(d.values()))
8. Mydict={ }
Mydict[(1,2,3)]=12
Mydict[(4,5,6)]=20
Mydict[(1,2)]=25
total=0
for i in Mydict:
total=total+Mydict[i]
print(total)
print(Mydict)
9. Write the output of the code given below:
d1 = {"name": "Aman", "age": 26}
d2 = {27:'age','age':28}
d1.update(d2)
print(d1.values())
10. Write the output of the following code given below:
Marks = {'Sidhi':65, 'Prakul':62, 'Suchitra':64, 'Prakash':50}
newMarks = {'Suchitra':66,'Arun':55,'Prkash':49}
Marks.update(newMarks)
for key,value in Marks.items():
print(key,'scored',value,'marks in Pre Board',end=' ')
if(value<55):
print(„and needs imporovement‟end=‟.‟)
print()
11. Write the output of the following Python program code:
my_dict = { }
my_dict[(1,2,4)] = 8
my_dict[(4,2,1)] = 10
my_dict[(1,2)] = 12
sum = 0
for k in my_dict:
sum += my_dict[k]
print (sum)
print(my_dict)
12. Predict the output of the following:
D={ }
T=("Raj","Anu","Nisha","Lisa")
for i in range(1,5):
D[i]=T[i-1]
print(D)
13. Predict the output of the following:
S="UVW"
L=[10,20,30]
D={ }
N=len(S)
for i in range(N):
D[L[i]]=S[i]
for K,V in D.items():
print(K,V,sep="*",end=",")
14. What will be the output, given by the following piece
of code:
D1= {"A":12, "B": 6, "C" : 50, "D" : 70}
print (50 in D1,"C" in D1,sep="#")
15. Predict the output of the following:
P=1400
D={'KTM':45,'RoyalEnfield':75,'Jawa':33,'TVS':89,
'Yamaha':111}
for j in D:
if(len(j)>5):
P=P-D[j]
print(j)
X={'Suzuki':1000,'Bajaja':21}
D.update(X)
print(X)
print(D)
print(D.get('Jawa','KTM'))

*****************************************************

You might also like