Prepared and compiled by Lipi Gupta
TUPLES– OUTPUT/ERRORS BASED QUESTIONS
1. Find the output generated by
following code fragments:
(a)
plane = ("Passengers", "Luggage")
plane[1] = "Snakes"
Ans. TypeError: 'tuple' object does not
support item assignment
(b)
t2 = ('a')
type(t2)
Ans. <class 'str'>
(c)
t3 = ('a',)
type(t3)
Ans. <class 'tuple'>
Prepared and compiled by Lipi Gupta
(d)
T4 = (17)
type(T4)
Ans. <class 'int'>
(e)
T5 = (17,)
type(T5)
Ans. <class 'tuple'>
(f)
tuple = ('a', 'b', 'c', 'd', 'e')
tuple = ('A',) + tuple [1:]
print (tuple)
Ans. ('A', 'b', 'c', 'd', 'e')
(g)
t2 = (4, 5, 6)
t3 = (6, 7)
t4 = t3 + t2
t5 = t2 + t3
Prepared and compiled by Lipi Gupta
print(t4)
print(t5)
Ans.
(6, 7, 4, 5, 6)
(4, 5, 6, 6, 7)
(h)
t3 = (6, 7)
t4 = t3 * 3
t5 = t3 * (3)
print(t4)
print(t5)
Ans.
(6, 7, 6, 7, 6, 7)
(6, 7, 6, 7, 6, 7)
(i)
t1 = (3, 4)
Prepared and compiled by Lipi Gupta
t2 = ('3', '4')
print(t1 + t2)
Ans. (3, 4, '3', '4')
(j) What will be stored in variables a, b,
c, d, e, f, g, h after following
statements?
perc = (88, 85, 80, 88, 83, 86)
a = perc[2:2]
b = perc[2:]
c = perc[:2]
d = perc[:-2]
e = perc[-2]
f = perc[2:-2]
g = perc[-2:2]
h = perc[:]
Ans.
()
(80, 88, 83, 86)
(88, 85)
(88, 85, 80, 88)
83
Prepared and compiled by Lipi Gupta
(80, 88)
()
(88, 85, 80, 88, 83, 86)
Q2. What does each of the following
expressions evaluate to? Suppose that T is
the tuple
("These", ("are", "a", "few", "words"),
"that", "we", "will", "use")
(a)
T[1][0: : 2]
Ans. ('are', 'few')
(b)
"a" in T [1] [ 0 ]
Ans.True
(c)
T [ : 1 ] + T[ 1 ]
Ans. ('These', 'are', 'a', 'few', 'words')
Prepared and compiled by Lipi Gupta
(d)
T[ 2 : : 2 ]
Ans. ('that', 'will')
(e)
T[2][2] in T[1]
Ans. True
Q3. Carefully read the given code
fragments and figure out the errors that
the code may produce.
(a)
t = ('a', 'b', 'c', 'd', 'e')
print(t[5])
Ans. IndexError: tuple index out of range
(b)
t = ('a', 'b', 'c', 'd', 'e')
t[0] = 'A'
Prepared and compiled by Lipi Gupta
Ans. TypeError: 'tuple' object does not
support item assignment
(c)
t1 = (3)
t2 = (4, 5, 6)
t3 = t1 + t2
print(t3)
Ans. TypeError: unsupported operand
type(s) for +: 'int' and 'tuple'
(d)
t2 = (4, 5, 6)
t3 = (6, 7)
print(t3 - t2)
Ans. TypeError: unsupported operand
type(s) for -: 'tuple' and 'tuple'
(e)
Prepared and compiled by Lipi Gupta
t3 = (6, 7)
t4 = t3 * 3
t5 = t3 * (3)
t6 = t3 * (3,)
print(t4)
print(t5)
print(16)
Ans. t6 = t3 * (3,)
TypeError: can't multiply sequence by
non-int of type 'tuple'
(f)
t = ('a', 'b', 'c', 'd', 'e')
1, 2, 3, 4, 5, = t
Ans. SyntaxError: can't assign to literal
(g)
t = ('a', 'b', 'c, d', 'e')
1n, 2n, 3n, 4n, 5n = t
Ans. SyntaxError: invalid syntax
Prepared and compiled by Lipi Gupta
Q4. What would be the output of
following code if :
ntpl = ("Hello", "Nita", "How's", "life?")
(a, b, c, d) = ntpl
print ( "a is:", a)
print("b is:", b)
print("c is:", c)
print("d is:", d)
ntpl = (a, b, c, d)
print (ntpl[0][0] + ntpl[1][1], ntpl[1])
Ans4.
a is: Hello
b is: Nita
c is: How's
d is: life?
Hi Nita
Q5. Predict the output:
Prepared and compiled by Lipi Gupta
tuple_a = 'a', 'b'
tuple_b = ('a', 'b')
print (tuple_a == tuple_b)
Ans. True
Q6. Find the error. Following code intends
to create a tuple with three identical
strings. But even after successfully
executing following code (No error
reported by Python). The len() returns a
value different from 3 Why ?
tup1 = ('Mega') * 3
print(len(tup1))
Ans. Resultant Tuple will be :
'MegaMegaMega'
Hence Length will be 12 and not 3.
Q7. Predict the output:
tuple1 = ('Python') * 3
print(type(tuple1))
Ans. <class 'str'>
Prepared and compiled by Lipi Gupta
Q8. What will the following code
produce?
Tup1 = (1,) * 3
Tup1 [0] = 2
print (Tup1)
Ans.
Tup1 [0] = 2
TypeError: 'tuple' object does not support
item assignment
Q9. What will be the output of the
following code snippet?
Tup1 = ((1, 2),) * 7
print (len(Tup1 [3: 8 ]))
Ans. 4