Python Tuple MCQ : Set 4

Python Tuple MCQ

1). What is the output of the following code?

l = [(” “,), (‘a’, ‘b’), (‘a’, ‘b’, ‘c’), (‘d’,), ()]
print(“Original list of tuples: “, l)
for tup in l:
    if len(tup) == 0:
        l.remove(tup)
print(“After removing empty tuples: “, l)

a). Original list of tuples: [(” “,), (‘a’, ‘b’), (‘a’, ‘b’, ‘c’), (‘d’,), ()]
After removing empty tuples: [(” “,), (‘a’, ‘b’), (‘a’, ‘b’, ‘c’), (‘d’,)]
b). Original list of tuples: [(‘ ‘,), (‘a’, ‘b’), (‘a’, ‘b’, ‘c’), (‘d’,), ()]
After removing empty tuples: [(‘ ‘,), (‘a’, ‘b’), (‘a’, ‘b’, ‘c’), (‘d’,)]
c). Original list of tuples: [(” “,), (‘a’, ‘b’), (‘a’, ‘b’, ‘c’), (‘d’,), ()]
After removing empty tuples: [(” “,), (‘a’, ‘b’, ‘c’), (‘d’,)]
d). Original list of tuples: [(” “,), (‘a’, ‘b’), (‘a’, ‘b’, ‘c’), (‘d’,), ()]
After removing empty tuples: [(‘a’, ‘b’, ‘c’), (‘d’,)]

Corresct answer is: b). Original list of tuples: [(‘ ‘,), (‘a’, ‘b’), (‘a’, ‘b’, ‘c’), (‘d’,), ()]
After removing empty tuples: [(‘ ‘,), (‘a’, ‘b’), (‘a’, ‘b’, ‘c’), (‘d’,)]
Explanation: The code initializes a list `l` with five tuples. It then iterates over the elements of `l` and removes any tuple with a length of 0 (an empty tuple).

2). What is the output of the following code?

l = [(3, 5.6), (6, 2.3), (1, 1.8)]
print(“Original list of tuples: “, l)
l1 = sorted(l, key=lambda x: float(x[1]))
print(“After sorting: “, l1)

a). Original list of tuples: [(3, 5.6), (6, 2.3), (1, 1.8)]
After sorting: [(1, 1.8), (6, 2.3), (3, 5.6)]
b). Original list of tuples: [(3, 5.6), (6, 2.3), (1, 1.8)]
After sorting: [(3, 5.6), (6, 2.3), (1, 1.8)]
c). Original list of tuples: [(3, 5.6), (6, 2.3), (1, 1.8)]
After sorting: [(6, 2.3), (3, 5.6), (1, 1.8)]
d). Error: name ‘tup’ is not defined

Corresct answer is: a). Original list of tuples: [(3, 5.6), (6, 2.3), (1, 1.8)]
After sorting: [(1, 1.8), (6, 2.3), (3, 5.6)]
Explanation: The code defines a list of tuples l and then prints the original list. The sorted() function is used to sort the list of tuples based on the second element of each tuple using a lambda function. The sorted list is then printed).

3). What is the output of the following code?

l = [1, 6, 8, 5, (4, 6, 8, 0), 5, 4]
print(“Original list: “, l)
for val in l:
    if type(val) is tuple:
        print(f”Elements in the tuple {val}: “, len(val))
a). Original list: [1, 6, 8, 5, (4, 6, 8, 0), 5, 4]
b). Original list: [1, 6, 8, 5, (4, 6, 8, 0), 5, 4]  Elements in the tuple (4, 6, 8, 0): 4
c). Elements in the tuple (4, 6, 8, 0): 4
d). None of the above

Corresct answer is: b). Original list: [1, 6, 8, 5, (4, 6, 8, 0), 5, 4]  Elements in the tuple (4, 6, 8, 0): 4
Explanation: The code initializes a list l containing various elements, including a tuple (4, 6, 8, 0). The code then iterates over each element in the list. When it encounters a tuple element, it prints the message “Elements in the tuple (4, 6, 8, 0): 4”. 

4). What is the output of the following code?

tup = (5, 4, 3, 1)
print(“Original tuple: “, tup)
product = 1
for val in tup:
    product *= val
print(“Multiplication of elements in the tuple: “, product)

a). Original tuple: (5, 4, 3, 1), Multiplication of elements in the tuple: 120
b). Original tuple: (5, 4, 3, 1), Multiplication of elements in the tuple: 60
c). Original tuple: (5, 4, 3, 1), Multiplication of elements in the tuple: 20
d). Original tuple: (5, 4, 3, 1), Multiplication of elements in the tuple: 1

Corresct answer is: b). Original tuple: (5, 4, 3, 1), Multiplication of elements in the tuple: 60
Explanation: The code initializes a variable product to 1. It then iterates through each element in the tuple tup and multiplies it with the current value of product. The final value of product will be the multiplication of all the elements in the tuple, which is 60.

5). What is the output of the following code?

tup = (‘4563′, ’68’, ‘1’,)
print(“Original tuple: “, tup)
l = []
for val in tup:
    if type(val) is str:
        l.append(int(val))
tup = tuple(l)
print(“After converting to integers: “, tup)

a). Original tuple: (‘4563′, ’68’, ‘1’)
After converting to integers: (4563, 68, 1)
b). Original tuple: (‘4563′, ’68’, ‘1’)
After converting to integers: (‘4563′, ’68’, ‘1’)
c). Original tuple: (4563, 68, 1)
After converting to integers: (4563, 68, 1)
d). Original tuple: (4563, 68, 1)
After converting to integers: (‘4563′, ’68’, ‘1’)

Corresct answer is: a). Original tuple: (‘4563′, ’68’, ‘1’)
After converting to integers: (4563, 68, 1)
Explanation: The code iterates over each value in the tup tuple. If the value is of type string (str), it is converted to an integer using the int() function and appended to the list l. Finally, the list l is converted back to a tuple, resulting in (4563, 68, 1).

6). What is the output of the following code?

tup = (4, 5, 3, 8)
str1 = “”
print(“Original tuple: “, tup)
for val in tup:
    str1 += str(val)
num = int(str1)
print(“Integer: “, num)

a). Original tuple: (4, 5, 3, 8), Integer: 4538
b). Original tuple: (4, 5, 3, 8), Integer: 4583
c). Original tuple: (4, 5, 3, 8), Integer: 5438
d). Original tuple: (4, 5, 3, 8), Integer: 5384

Corresct answer is: a). Original tuple: (4, 5, 3, 8), Integer: 4538
Explanation: The code initializes an empty string `str1` and a tuple `tup` with values (4, 5, 3, 8). It then iterates over each element in the tuple using a loop. In each iteration, the current element is converted to a string and concatenated with `str1`. After the loop, `str1` becomes “4538”. The string `str1` is then converted to an integer `num` using the `int()` function. 

7). What is the output of the following code?

a = (1, 6, 7)
b = (4, 3, 0)
c = (2, 6, 8)
print(f”Original tuples: {a},{b},{c}”)
s1 = map(sum, zip(a,b,c))
result = tuple(s1)
print(“Element wise sum of the given tuples: “,result)

a). Original tuples: (1, 6, 7), (4, 3, 0), (2, 6, 8)
Element wise sum of the given tuples: (7, 15, 15)
b). Original tuples: (1, 6, 7), (4, 3, 0), (2, 6, 8)
Element wise sum of the given tuples: (7, 15, 15, 0)
c). Original tuples: (1, 6, 7), (4, 3, 0), (2, 6, 8)
Element wise sum of the given tuples: (6, 15, 15)
d). Original tuples: (1, 6, 7), (4, 3, 0), (2, 6, 8)
Element wise sum of the given tuples: (7, 12, 15)

Corresct answer is: a). Original tuples: (1, 6, 7), (4, 3, 0), (2, 6, 8)
Element wise sum of the given tuples: (7, 15, 15)
Explanation: The code first defines three tuples: a = (1, 6, 7), b = (4, 3, 0), and c = (2, 6, 8). It then prints the original tuples using f-string formatting. The zip() function is used to combine the corresponding elements of the tuples, and the map() function with sum as the first argument is used to calculate the sum of each group of corresponding elements. The map object is converted to a tuple using tuple(), and the result is stored in the variable result. Finally, the element-wise sum of the tuples is printed, resulting in (7, 15, 15).

8). What is the output of the following code?

l = [(1, 5), (7, 8), (4, 0)]
l1 = []
print(“Original list: “, l)
for tup in l:
    l1.append(list(tup))
print(“After converting tuples inside a list to list: “, l1)

a). Original list: [(1, 5), (7, 8), (4, 0)]
After converting tuples inside a list to list: [[1, 5], [7, 8], [4, 0]]
b). Original list: [(1, 5), (7, 8), (4, 0)]
After converting tuples inside a list to list: [(1, 5), (7, 8), (4, 0)]
c). Original list: [1, 5, 7, 8, 4, 0]
After converting tuples inside a list to list: [1, 5, 7, 8, 4, 0]
d). Error: Cannot convert tuples to lists

Corresct answer is: a). Original list: [(1, 5), (7, 8), (4, 0)]
After converting tuples inside a list to list: [[1, 5], [7, 8], [4, 0]]
Explanation: The code starts with a list `l` containing tuples. The loop iterates over each tuple `tup` in `l` and converts each tuple into a list using the `list()` function. The converted lists are appended to a new list `l1`. The output will be the original list `l` followed by the modified list `l1`, where the tuples have been converted to lists.

9). What is the output of the following code?

l = [(1, 7), (-4, -5), (0, 6), (-1, 3)]
for tup in l:
if any(ele < 0 for ele in tup):
print(“Tuple having negative element: “, tup)
a). Tuple having negative element: (1, 7), Tuple having negative element: (0, 6)
b). Tuple having negative element: (-4, -5), Tuple having negative element: (-1, 3)
c). Tuple having negative element: (-1, 3)
d). Tuple having negative element: (0, 6), Tuple having negative element: (1, 7)

Corresct answer is: b). Tuple having negative element: (-4, -5), Tuple having negative element: (-1, 3)
Explanation: The code iterates over each tuple in the list l. The any() function checks if any element in the tuple is less than 0. Since the tuple (-4, -5), (-1, 3) has negative elements, it will be printed as “Tuple having negative element: (-4, -5), Tuple having negative element: (-1, 3)”.

10). What is the output of the following code?

tup = (6, 4, 9, 0, 2, 6, 1, 3, 4)
print(“Original tuple: “, tup)
new_tup = tuple(set(tup))
print(“New tuple: “, new_tup)

a). Original tuple: (6, 4, 9, 0, 2, 6, 1, 3, 4) New tuple: (6, 4, 9, 0, 2, 6, 1, 3)
b). Original tuple: (6, 4, 9, 0, 2, 6, 1, 3, 4) New tuple: (6, 4, 9, 0, 2, 1, 3)
c). Original tuple: (6, 4, 9, 0, 2, 6, 1, 3, 4) New tuple: (0, 1, 2, 3, 4, 6, 9)
d). Original tuple: (6, 4, 9, 0, 2, 6, 1, 3, 4) New tuple: (4, 6, 9, 0, 2, 1, 3)

Corresct answer is: c). Original tuple: (6, 4, 9, 0, 2, 6, 1, 3, 4) New tuple: (0, 1, 2, 3, 4, 6, 9)
Explanation: In the given code, the original tuple `tup` is created with elements (6, 4, 9, 0, 2, 6, 1, 3, 4). The `set()` function is used to convert the tuple into a set, which removes the duplicate elements. Then, the `tuple()` function is used to convert the set back into a tuple. The output will be “Original tuple: (6, 4, 9, 0, 2, 6, 1, 3, 4)” and “New tuple: (0, 1, 2, 3, 4, 6, 9)”.

11). What is the output of the following code?

l = [(4,8,3),(3,4,0),(1,6,2)]
print(“Original list of tuples: “,l)
i = 1
product = 1
for tup in l:
    for ele in tup:
        if tup.index(ele) == i:
            product *= ele
print(f”Product of {i} element in each tuple is {product}”)

a). Original list of tuples: [(4,8,3),(3,4,0),(1,6,2)]
Product of 1 element in each tuple is 56
b). Original list of tuples: [(4,8,3),(3,4,0),(1,6,2)]
Product of 1 element in each tuple is 180
c). Original list of tuples: [(4,8,3),(3,4,0),(1,6,2)]
Product of 1 element in each tuple is 192
d). Original list of tuples: [(4,8,3),(3,4,0),(1,6,2)]
Product of 1 element in each tuple is 12

Corresct answer is: c). Original list of tuples: [(4,8,3),(3,4,0),(1,6,2)]
Product of 1 element in each tuple is 192
Explanation: The code calculates the product of the element at index 1 in each tuple. The elements at index 1 in the given list of tuples are 8, 4, and 6. The product of these elements is 8 * 4 * 6 = 192.

12). What is the output of the following code?

l = [(‘s’,’q’,’a’),(‘t’,’o’),(‘o’,’l’,’s’)]
print(“Original list of tuples: “, l)
str1 = “”
for tup in l:
    for char in tup:
        str1 += char + ” “
print(“New string: “, str1)

a). Original list of tuples: [(‘s’,’q’,’a’),(‘t’,’o’),(‘o’,’l’,’s’)]
New string: “s q a t o o l s”
b). Original list of tuples: [(‘s’,’q’,’a’),(‘t’,’o’),(‘o’,’l’,’s’)]
New string: “sqa to ols”
c). Original list of tuples: [(‘s’,’q’,’a’),(‘t’,’o’),(‘o’,’l’,’s’)]
New string: “sqa tols”
d). Original list of tuples: [(‘s’,’q’,’a’),(‘t’,’o’),(‘o’,’l’,’s’)]
New string: “s q a t o l s”

Corresct answer is: a). Original list of tuples: [(‘s’,’q’,’a’),(‘t’,’o’),(‘o’,’l’,’s’)]
New string: “s q a t o o l s”
Explanation: The code snippet initializes a list of tuples `l`. It then iterates over each tuple in `l` and concatenates each character from the tuples into a string `str1`, separating them with a space. The output will be “Original list of tuples: [(‘s’,’q’,’a’),(‘t’,’o’),(‘o’,’l’,’s’)]” and “New string: “s q a t o o l s”.

13). What is the output of the following code?

a = (1, 2, 3, 4)
print(“Tuple: “, a)
b = ‘sqatools’
print(“String: “, b)
l = []
for ele in a:
    l.append(ele)
    l.append(b)
print(“New list: “, l)

a). Tuple: (1, 2, 3, 4), String: ‘sqatools’, New list: [1, ‘sqatools’, 2, ‘sqatools’, 3, ‘sqatools’, 4, ‘sqatools’]
b). Tuple: (1, 2, 3, 4), String: ‘sqatools’, New list: [1, 2, 3, 4, ‘sqatools’]
c). Tuple: (1, 2, 3, 4), String: ‘sqatools’, New list: [1, 2, 3, 4]
d). Error: ‘str’ object has no attribute ‘append’

Corresct answer is: a). Tuple: (1, 2, 3, 4), String: ‘sqatools’, New list: [1, ‘sqatools’, 2, ‘sqatools’, 3, ‘sqatools’, 4, ‘sqatools’]
Explanation: The code snippet initializes a tuple a with values (1, 2, 3, 4) and a string b with the value ‘sqatools’. It then creates an empty list l. The subsequent for loop iterates over each element in the tuple a). For each element, it appends the element itself (ele) and the string b to the list l.

14). What is the output of the following code?

l = [[‘sqatools’], [‘is’], [‘best’]]
print(“List of lists: “, l)
l1 = []
for lists in l:
    l1.append(tuple(lists))
tup = tuple(l1)
print(“Tuple of list of lists: “, tup)

a). List of lists: [[‘sqatools’], [‘is’], [‘best’]]
Tuple of list of lists: ((‘sqatools’,), (‘is’,), (‘best’,))
b). List of lists: [[‘sqatools’], [‘is’], [‘best’]]
Tuple of list of lists: ([‘sqatools’], [‘is’], [‘best’])
c). List of lists: [[‘sqatools’], [‘is’], [‘best’]]
Tuple of list of lists: ((‘sqatools’,), (‘is’,), (‘best’,))
d). Error: cannot convert list to tuple

Corresct answer is: a). List of lists: [[‘sqatools’], [‘is’], [‘best’]]
Tuple of list of lists: ((‘sqatools’,), (‘is’,), (‘best’,))
Explanation: The code starts with a list of lists, l, which contains three inner lists. Each inner list is converted into a tuple using the tuple() function and appended to a new list, l1. Finally, l1 is converted into a tuple, tup. The output will be “List of lists: [[‘sqatools’], [‘is’], [‘best’]]” and “Tuple of list of lists: ((‘sqatools’,), (‘is’,), (‘best’,))”.

15). What is the output of the following code?

tup = (56, 99)
str1 = “”
result = float(‘.’.join(str(ele) for ele in tup))
print(“Float number: “, result)

a). Float number: 56.99
b). Float number: 56.0
c). Float number: 0.5699
d). Error: unsupported operand type(s) for .join(): ‘float’ and ‘str’

Corresct answer is: a
Explanation: The code snippet converts the tuple (56, 99) into a float number by joining the elements with a dot using ‘.’.join() and then converting the resulting string to a float using float(). The resulting float number will be 56.99, and it will be printed as “Float number: 56.99”.

16). What is the output of the following code?

l = [(‘a’, ‘b’), (‘d’, ‘e’), (‘b’, ‘a’)]
print(“Original list of tuples: “, l)
temp = set(l) & {(b, a) for a, b in l}
sym = {(a, b) for a, b in temp if a < b}
print(“Tuples that are symmetric: “, sym)

a). Original list of tuples: [(‘a’, ‘b’), (‘d’, ‘e’), (‘b’, ‘a’)]
Tuples that are symmetric: {(‘a’, ‘b’), (‘b’, ‘a’)}
b). Original list of tuples: [(‘a’, ‘b’), (‘d’, ‘e’), (‘b’, ‘a’)]
Tuples that are symmetric: {(‘a’, ‘b’)}
c). Original list of tuples: [(‘a’, ‘b’), (‘d’, ‘e’), (‘b’, ‘a’)]
Tuples that are symmetric: {(‘b’, ‘a’)}
d). Original list of tuples: [(‘a’, ‘b’), (‘d’, ‘e’), (‘b’, ‘a’)]
Tuples that are symmetric: {}

Corresct answer is: b). Original list of tuples: [(‘a’, ‘b’), (‘d’, ‘e’), (‘b’, ‘a’)]
Tuples that are symmetric: {(‘a’, ‘b’)}
Explanation: The original list of tuples is [(‘a’, ‘b’), (‘d’, ‘e’), (‘b’, ‘a’)]. The code uses set operations and set comprehension to find the symmetric tuples. The symmetric tuples are (‘a’, ‘b’) and (‘b’, ‘a’). Thus, the output will be “Original list of tuples: [(‘a’, ‘b’), (‘d’, ‘e’), (‘b’, ‘a’)] Tuples that are symmetric: {(‘a’, ‘b’)}.”

17). What is the output of the following code?

tup = (‘s’, ‘q’, ‘a’, (‘t’, ‘o’, ‘o’, ‘l’, ‘s’), ‘i’, ‘s’, ‘b’, ‘e’, ‘s’, ‘t’)
print(“Original tuple: “, tup)
l = []
for char in tup:
    if type(char) != tuple:
        l.append(char)
new = tuple(l)
print(“New tuple: “, new)

a). Original tuple: (‘s’, ‘q’, ‘a’, (‘t’, ‘o’, ‘o’, ‘l’, ‘s’), ‘i’, ‘s’, ‘b’, ‘e’, ‘s’, ‘t’)
New tuple: (‘s’, ‘q’, ‘a’, ‘i’, ‘s’, ‘b’, ‘e’, ‘s’, ‘t’)
b). Original tuple: (‘s’, ‘q’, ‘a’, (‘t’, ‘o’, ‘o’, ‘l’, ‘s’), ‘i’, ‘s’, ‘b’, ‘e’, ‘s’, ‘t’)
New tuple: (‘s’, ‘q’, ‘a’, ‘t’, ‘o’, ‘o’, ‘l’, ‘s’, ‘i’, ‘s’, ‘b’, ‘e’, ‘s’, ‘t’)
c). Original tuple: (‘s’, ‘q’, ‘a’, (‘t’, ‘o’, ‘o’, ‘l’, ‘s’), ‘i’, ‘s’, ‘b’, ‘e’, ‘s’, ‘t’)
New tuple: (‘s’, ‘q’, ‘a’, (‘t’, ‘o’, ‘o’, ‘l’, ‘s’), ‘i’, ‘s’, ‘b’, ‘e’, ‘s’, ‘t’)
d). Error: tuple’ object has no attribute ‘append’

Corresct answer is: a). Original tuple: (‘s’, ‘q’, ‘a’, (‘t’, ‘o’, ‘o’, ‘l’, ‘s’), ‘i’, ‘s’, ‘b’, ‘e’, ‘s’, ‘t’)
New tuple: (‘s’, ‘q’, ‘a’, ‘i’, ‘s’, ‘b’, ‘e’, ‘s’, ‘t’)
Explanation: The code iterates through each element in the original tuple `tup` and checks if it is a tuple or not. If it is not a tuple, the element is appended to the list `l`. Finally, the list `l` is converted back to a tuple `new`. The output will be “Original tuple: (‘s’, ‘q’, ‘a’, (‘t’, ‘o’, ‘o’, ‘l’, ‘s’), ‘i’, ‘s’, ‘b’, ‘e’, ‘s’, ‘t’)” and “New tuple: (‘s’, ‘q’, ‘a’, ‘i’, ‘s’, ‘b’, ‘e’, ‘s’, ‘t’)”. The inner tuple `(‘t’, ‘o’, ‘o’, ‘l’, ‘s’)` is not included in the new tuple, as only individual non-tuple elements are added to the list `l`.

18). What is the output of the following code?

l = [(1, 5, 7), (3, 4, 2), (4, 9, 0)]
print(“Original list of tuples: “, l)
l1 = sorted(l, key=lambda x: max(x), reverse=True)
print(“After sorting: “, l1)

a). Original list of tuples: [(1, 5, 7), (3, 4, 2), (4, 9, 0)]
After sorting: [(4, 9, 0), (1, 5, 7), (3, 4, 2)]
b). Original list of tuples: [(1, 5, 7), (3, 4, 2), (4, 9, 0)]
After sorting: [(1, 5, 7), (3, 4, 2), (4, 9, 0)]
c). Original list of tuples: [(1, 5, 7), (3, 4, 2), (4, 9, 0)]
After sorting: [(4, 9, 0), (3, 4, 2), (1, 5, 7)]
d). Original list of tuples: [(4, 9, 0), (3, 4, 2), (1, 5, 7)]
After sorting: [(4, 9, 0), (3, 4, 2), (1, 5, 7)]

Corresct answer is: a). Original list of tuples: [(1, 5, 7), (3, 4, 2), (4, 9, 0)]
After sorting: [(4, 9, 0), (1, 5, 7), (3, 4, 2)]
Explanation: The original list of tuples is [(1, 5, 7), (3, 4, 2), (4, 9, 0)]. The sorting is based on the maximum value in each tuple, in descending order. After sorting, the list becomes [(4, 9, 0), (1, 5, 7), (3, 4, 2)] where the tuple with the maximum value of 9 is placed first, followed by tuples with values 7, 5, 4, 3, and 2.

19). What is the output of the following code?

tup1 = (‘s’, ‘q’, ‘a’)
tup2 = (‘t’, ‘o’, ‘o’, ‘l’)
print(f”Original tuple: {tup1} {tup2}”)
result = tup1 + tup2
print(“Combined tuple: “, result)

a). Original tuple: (‘s’, ‘q’, ‘a’) (‘t’, ‘o’, ‘o’, ‘l’)
Combined tuple: (‘s’, ‘q’, ‘a’, ‘t’, ‘o’, ‘o’, ‘l’)
b). Original tuple: (‘s’, ‘q’, ‘a’) (‘t’, ‘o’, ‘o’, ‘l’)
Combined tuple: (‘s’, ‘q’, ‘a’, ‘tool’)
c). Original tuple: (‘s’, ‘q’, ‘a’) (‘t’, ‘o’, ‘o’, ‘l’)
Combined tuple: (‘tool’, ‘s’, ‘q’, ‘a’)
d). Original tuple: (‘t’, ‘o’, ‘o’, ‘l’) (‘s’, ‘q’, ‘a’)
Combined tuple: (‘s’, ‘q’, ‘a’, ‘t’, ‘o’, ‘o’, ‘l’)

Corresct answer is: a). Original tuple: (‘s’, ‘q’, ‘a’) (‘t’, ‘o’, ‘o’, ‘l’)
Combined tuple: (‘s’, ‘q’, ‘a’, ‘t’, ‘o’, ‘o’, ‘l’)
Explanation: The code concatenates `tup1` and `tup2` using the `+` operator, resulting in a combined tuple `(‘s’, ‘q’, ‘a’, ‘t’, ‘o’, ‘o’, ‘l’)`. The original tuples are printed as `(‘s’, ‘q’, ‘a’)` and `(‘t’, ‘o’, ‘o’, ‘l’)`, respectively, and the combined tuple is printed as `(‘s’, ‘q’, ‘a’, ‘t’, ‘o’, ‘o’, ‘l’)`.

20). What is the output of the following code?

tup1 = [(1, 5), (4, 8), (3, 9)]
tup2 = [(3, 9), (5, 6), (1, 5), (0, 4)]
print(f”Tuples: {tup1} and {tup2}”)
result = (set(tup1) & set(tup2))
print(“Common elements between tuples: “, result)

a). Tuples: [(1, 5), (4, 8), (3, 9)] and [(3, 9), (5, 6), (1, 5), (0, 4)]
Common elements between tuples: [(1, 5), (5, 6)]
b). Tuples: [(1, 5), (4, 8), (3, 9)] and [(3, 9), (5, 6), (1, 5), (0, 4)]
Common elements between tuples: {(1, 5), (3, 9)}
c). Tuples: [(1, 5), (4, 8), (3, 9)] and [(3, 9), (5, 6), (1, 5), (0, 4)]
Common elements between tuples: [(3, 9), (0, 4)]
d). Tuples: [(1, 5), (4, 8), (3, 9)] and [(3, 9), (5, 6), (1, 5), (0, 4)]
Common elements between tuples: {(3, 9), (3, 9)}

Corresct answer is: b). Tuples: [(1, 5), (4, 8), (3, 9)] and [(3, 9), (5, 6), (1, 5), (0, 4)]
Common elements between tuples: {(1, 5), (3, 9)}
Explanation: The code defines two lists of tuples, `tup1` and `tup2`, and prints their values. The `&` operator is used to find the common elements between the two sets created from `tup1` and `tup2`. The resulting common elements are of type set and will be printed as `{(1, 5), (3, 9)}`.

21). What is the output of the following code?

l = [(8, 9), (4, 7), (3, 6), (8, 9)]
print(“Original list of tuples: “, l)
print(“Total number of unique tuples: “, len(set(l)))

a). Original list of tuples: [(8, 9), (4, 7), (3, 6), (8, 9)]
Total number of unique tuples: 3
b). Original list of tuples: [(8, 9), (4, 7), (3, 6), (8, 9)]
Total number of unique tuples: 4
c). Original list of tuples: [(8, 9), (4, 7), (3, 6), (8, 9)]
Total number of unique tuples: 2
d). Original list of tuples: [(8, 9), (4, 7), (3, 6), (8, 9)]
Total number of unique tuples: 1

Corresct answer is: a). Original list of tuples: [(8, 9), (4, 7), (3, 6), (8, 9)]
Total number of unique tuples: 3
Explanation: The code first prints the original list of tuples, which is [(8, 9), (4, 7), (3, 6), (8, 9)]. Then it uses the set() function to convert the list to a set, which removes the duplicate tuples. The length of the set is then printed, which is 2. Therefore, the output is “Original list of tuples: [(8, 9), (4, 7), (3, 6), (8, 9)] Total number of unique tuples: 3”.

22). What is the output of the following code?

tup = (5, 3, 9, 6)
print(“Original tuple: “, tup)
add = 0
for ele in tup:
    add += ele
print(“Average of elements in the tuple: “, add/len(tup))

a). Original tuple: (5, 3, 9, 6), Average of elements in the tuple: 5.75
b). Original tuple: (5, 3, 9, 6), Average of elements in the tuple: 6.25
c). Original tuple: (5, 3, 9, 6), Average of elements in the tuple: 6
d). Error: unsupported operand type(s) for /: ‘int’ and ‘tuple’

Corresct answer is: a). Original tuple: (5, 3, 9, 6), Average of elements in the tuple: 5.75
Explanation: The code initializes a tuple tup with values (5, 3, 9, 6). It then calculates the sum of all elements using a loop and stores it in the variable add). The average is calculated by dividing the sum add by the length of the tuple using the len() function. The output will be “Original tuple: (5, 3, 9, 6), Average of elements in the tuple: 5.75”.

23). What is the output of the following code?

A = (7, 4, 9)
B = (3,)
print(f”tuple1: {A}, tuple2: {B}”)
A, B = B, A
print(f”tuple1: {A}, tuple2: {B}”)

a). tuple1: (7, 4, 9), tuple2: (3,)
tuple1: (3,), tuple2: (7, 4, 9)
b). tuple1: (7, 4, 9), tuple2: (3,)
tuple1: (7, 4, 9), tuple2: (3,)
c). tuple1: (7, 4, 9), tuple2: (3,)
tuple1: (3,), tuple2: (7, 4, 9)
d). tuple1: (3,), tuple2: (7, 4, 9)
tuple1: (3,), tuple2: (7, 4, 9)

Corresct answer is: c). tuple1: (7, 4, 9), tuple2: (3,)
tuple1: (3,), tuple2: (7, 4, 9)
Explanation: In the code, tuple A is initially `(7, 4, 9)` and tuple B is `(3,)`. When the line `A, B = B, A` is executed, the values of A and B are swapped). Now A becomes `(3,)` and B becomes `(7, 4, 9)`. The first print statement outputs `tuple1: (7, 4, 9), tuple2: (3,)` because it is executed before the swap. The second print statement outputs `tuple1: (3,), tuple2: (7, 4, 9)` after the swap.

24). What is the output of the following code?

tup = (7, 4, 9, 2, 0)
if type(tup) == tuple:
    print(“True”)
else:
    print(“False”)

a). True
b). False
c). Error: invalid syntax
d). Error: unsupported operand type(s) for ==

Corresct answer is: a). True
Explanation: The code snippet checks whether the variable tup is of type tuple using the type() function. Since tup is indeed a tuple, the condition is True, and the code will print “True” as the output.

25). What is the output of the following code?

tup = (‘p’,’y’,’t’,’h’,’o’,’n’)
print(“Original tuple: “,tup)
print(“Last element of the tuple using negative indexing: “,tup[-1])

a). Original tuple: (‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
Last element of the tuple using negative indexing: ‘p’
b). Original tuple: (‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
Last element of the tuple using negative indexing: ‘n’
c). Original tuple: (‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
Last element of the tuple using negative indexing: ‘o’
d). Original tuple: (‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
Last element of the tuple using negative indexing: ‘t’

Corresct answer is: b). Original tuple: (‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
Last element of the tuple using negative indexing: ‘n’
Explanation: The original tuple is `(‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)`. Negative indexing allows accessing elements from the end of the tuple. `-1` represents the last element of the tuple. Therefore, the output will be “Last element of the tuple using negative indexing: ‘n'”.

Leave a Comment