0% found this document useful (0 votes)
16 views5 pages

Tuple MCQs

Uploaded by

Himanshu Tonk
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)
16 views5 pages

Tuple MCQs

Uploaded by

Himanshu Tonk
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/ 5

TUPLES IN PYTHON

Objective Types Questions


Question 1: Which of the following statements will create a tuple:
1. tp1=("a", "b")
2. tp1[2]=("a", "b")
3. tp1=(3)*3
4. None of these
Reason — A tuple is created by placing all the items (elements)
inside parentheses () , separated by commas.
Question 2: Choose the correct statement(s).
1. Both tuples and lists are immutable.
2. Tuples are immutable while lists are mutable.
3. Both tuples and lists are mutable.
4. Tuples are mutable while lists are immutable.
Reason — Tuples cannot be modified whereas List can be modified.
Question 3: Choose the correct statement(s).
1. In Python, a tuple can contain only integers as its elements.
2. In Python, a tuple can contain only strings as its elements.
3. In Python, a tuple can contain elements of different types.
4. In Python, a tuple can contain either string or integer but not
both at a time
Reason — A tuple can have any number of items and they may be of
different types (integer, float, list, string, etc).
Question 4: Which of the following is/are correctly declared
tuple(s) ?
1. a = ("Hina", "Mina", "Tina", "Nina")
2. a = "Hina", "Mina", "Tina", "Nina")
3. a = ["Hina", "Mina", "Tina", "Nina"]
4. a = (["Hina", "Mina", "Tina", "Nina"])
Reason — A tuple is created by placing elements inside parentheses
() , separated by commas.
Question 5: Which of the following will create a single element
tuple?
1. (1,)
2. (1)
3. ( [1] )
4. tuple([1])
Reason — (1,) To create a tuple with only one element, comma
needs to be added after the element, otherwise Python will not
recognize the variable as a tuple and will consider it as an integer.
tuple([1]) tuple() function takes any iterable as an argument and
hence when u pass a list it makes the list elements as its values.
Here, 1 is enclosed in square brackets which signifies list. When [1] is
being passed as an argument in a tuple, it will generate a single
element tuple with element is equal "1".
Question 6: What will be the output of following Python code?
tp1 = (2,4,3)
tp3 = tp1*2
print(tp3)
1. (4,8,6)
2. (2,4,3,2,4,3)
3. (2,2,4,4,3,3)
4. Error
Reason — The "*" operator repeats a tuple specified number of
times and creates a new tuple.
Question 7: What will be the output of following Python code?
tp1 = (15,11,17,16,12)
tp1.pop(12)
print(tp1)
1. (15,11,16,12)
2. (15,11,17,16)
3. (15,11,17,16,12)
4. Error
Reason — As tuples are immutable so they don't support pop
operation.
Question 8: Which of the following options will not result in an
error when performed on types in Python where tp = (5,2,7,0,3)?
1. tp[1] = 2
2. tp.append(2)
3. tp1=tp+tp
4. tp.sum()
Reason — The "+" operator concatenates two tuples and creates a
new tuple. First option will throw an error since tuples are
immutable, item assignment not supported in tuples. Second and
Fourth option will also throw an error since tuple object has no
attribute 'append' and 'sum'.
Question 9: What will be the output of the following Python code?
tp = ()
tp1 = tp * 2
print(len(tp1))
1. 0
2. 2
3. 1
4. Error
Reason — Empty tuples multiplied with any number yield empty
tuples only.
Question 10: What will be the output of the following Python code?
tp = (5)
tp1 = tp * 2
print(len(tp1))
1. 0
2. 2
3. 1
4. Error
Reason — tp is not a tuple and holds an integer value hence object
of type 'int' has no len()
Question 11: What will be the output of the following Python code?
tp = (5,)
tp1 = tp * 2
print(len(tp1))
1. 0
2. 2
3. 1
4. Error
Reason — The "*" operator performs repetition in tuples. tp1 = tp *
2 will result in tp1 as (5, 5) so its length will be 2.
Question 12: Given tp = (5,3,1,9,0). Which of the following two
statements will give the same output?
(i) print( tp[:-1] )
(ii) print( tp[0:5] )
(iii) print( tp[0:4] )
(iv) print( tp[-4:] )
1. (i), (ii)
2. (ii), (iv)
3. (i), (iv)
4. (i), (iii)
Reason — Both will yield (5, 3, 1, 9). We can use indexes of tuple
elements to create tuple slices as per following format : seq =
T[start:stop]
Question 13: What is the output of the following code?
t = (10, 20, 30, 40, 50, 50, 70)
print(t[5:-1])
1. Blank output( )
2. (50,70)
3. (50,50,70)
4. (50,)
Reason — Length of tuple t is 7. t[5 : -1] represents tuple slice t[5 :
(7-1)] = t[5 : 6] i.e., the element at index 5. So output is (50,).
Question 14: What is the output of the following code?
t = (10, 20, 30, 40, 50, 60, 70)
print(t[5:-1])
1. (60)
2. (60,)
3. (10, 30, 50, 70)
4. (10, 20, 30, 40, 50, 60, 70)
Reason — Length of tuple t is 7. t[5 : -1] represents tuple slice t[5 :
(7-1)] = t[5 : 6] i.e., the element at index 5. So output is (60,).
Note: There is a misprint in the options provided in the book.
Question 15: Which of the below given functions cannot be used
with nested tuples?
1. index( )
2. count( )
3. max( )
4. sum( )
Reason — For sum( ) function to work, the tuple must have numeric
elements. Since nested tuples will have at least one element as tuple
so the sum( ) function will raise a TypeError and will not work.

You might also like