Python Tuple MCQ : Set 3

Python Tuple MCQ

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

tup = (‘s’, ‘q’, ‘a’, ‘t’, ‘o’, ‘o’, ‘l’, ‘s’)
str1 = ”
for char in tup:
    str1 += char
print(“String: “, str1)

a). String: ‘s’, ‘q’, ‘a’, ‘t’, ‘o’, ‘o’, ‘l’, ‘s’
b). String: ‘sqltools’
c). String: ‘sqa’
d). Error: tuple object has no attribute ‘+’

Corresct answer is: b). String: ‘sqltools’
Explanation: The code iterates over each character in the tuple tup and concatenates them to the str1 variable. After the loop completes, str1 will contain the string ‘sqltools’, which is printed as the output.

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

tup = (‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
if ‘p’ in tup:
    print(“True”)
else:
    print(“False”)

a). True
b). False
c). Error: ‘p’ is not in tup
d). Error: invalid syntax

Corresct answer is: a). True
Explanation: The code checks if the element ‘p’ is present in the tuple tup using the in operator. Since ‘p’ is indeed present in the tuple, the condition is true, and the output will be “True”.

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

list1 = [12, 67]
tup = (6, 8, 4)
result = tuple(list(tup) + list1)
print(result)

a). (6, 8, 4, 12, 67)
b). (6, 8, 4, [12, 67])
c). (6, 8, 4, (12, 67))
d). Error: cannot concatenate ‘tuple’ and ‘list’ objects

Corresct answer is: a). (6, 8, 4, 12, 67)
Explanation: In the given code, list(tup) converts the tuple tup into a list, resulting in [6, 8, 4]. Similarly, list1 is already a list with values [12, 67]. Using the + operator concatenates the two lists, resulting in [6, 8, 4, 12, 67]. Finally, tuple() converts the concatenated list back into a tuple, resulting in (6, 8, 4, 12, 67). 

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

tup = (4, 6, 2)
print(“Sum of elements in the tuple: “, sum(tup))

a). Sum of elements in the tuple: 12
b). Sum of elements in the tuple: 10
c). Sum of elements in the tuple: (4, 6, 2)
d). Error: ‘tuple’ object has no attribute ‘sum’

Corresct answer is: a). Sum of elements in the tuple: 12
Explanation: The code initializes a tuple tup with the values (4, 6, 2). The sum() function is then used to calculate the sum of all the elements in the tuple. The output statement prints “Sum of elements in the tuple: ” followed by the calculated sum, which is 12. 

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

tup = (1, 3, 5, 7, 6)
print(“Original tuple: “, tup)
a = list(tup)
for i in a:
    j = a).index(i)
    b = i ** 2
    a[j] = b
tup = tuple(a)
print(“After squaring elements: “, tup)

a). Original tuple: (1, 3, 5, 7, 6), After squaring elements: (1, 9, 25, 49, 36)
b). Original tuple: (1, 3, 5, 7, 6), After squaring elements: (1, 9, 25, 49, 6)
c). Original tuple: (1, 3, 5, 7, 6), After squaring elements: (1, 3, 5, 7, 6)
d). Error: ‘tuple’ object has no attribute ‘index’

Corresct answer is: b). Original tuple: (1, 3, 5, 7, 6), After squaring elements: (1, 9, 25, 49, 6)
Explanation: The code starts with the tuple `tup` containing the values (1, 3, 5, 7, 6). It converts the tuple to a list `a` using the `list()` function. Then, it iterates over each element `i` in the list and calculates its square `b`. The square value is assigned back to the list at index `j` using the `index()` method). Finally, the list is converted back to a tuple `tup` using the `tuple()` function.

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

tup = (1, 2, 3, 4)
list1 = []
for a, b in zip(tup, tup[1:]):
    c = a * b
    list1.append(c)
tup = tuple(list1)
print(“Multiplying adjacent elements: “, tup)

a). Multiplying adjacent elements: (2, 6, 12)
b). Multiplying adjacent elements: (1, 4, 9, 16)
c). Multiplying adjacent elements: (1, 2, 3, 4)
d). Multiplying adjacent elements: []

Corresct answer is: a). Multiplying adjacent elements: (2, 6, 12)
Explanation: The code iterates over tup and tup[1:] simultaneously using the zip() function. It multiplies the adjacent elements and appends the result to list1. Finally, list1 is converted back to a tuple and assigned to tup. The output will be “Multiplying adjacent elements: (2, 6, 12)”, as the adjacent elements are multiplied).

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

list1 = [12, 65, 34, 77]
list2 = []
for ele in list1:
    a = 2 * ele
    list2.append(a)

tup = tuple(list2)
print(“Original list: “, list1)
print(“After multiplying by 2: “, tup)

a). Original list: [12, 65, 34, 77], After multiplying by 2: (24, 130, 68, 154)
b). Original list: [12, 65, 34, 77], After multiplying by 2: [24, 130, 68, 154]
c). Original list: [12, 65, 34, 77], After multiplying by 2: (12, 65, 34, 77)
d). Error: invalid syntax

Corresct answer is: a). Original list: [12, 65, 34, 77], After multiplying by 2: (24, 130, 68, 154)
Explanation: The code initializes an empty list list2 and iterates over each element ele in list1. It multiplies each element by 2 and appends the result to list2. Then, the code converts list2 to a tuple tup. The output will be “Original list: [12, 65, 34, 77]” and “After multiplying by 2: (24, 130, 68, 154)”.

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

tup = (‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
print(“Original tuple: “, tup)
l = list(tup)
l.remove(‘h’)
tup = tuple(l)
print(“After removing an element: “, tup)


a). Original tuple: (‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
After removing an element: (‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
b). Original tuple: (‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
After removing an element: (‘p’, ‘y’, ‘t’, ‘o’, ‘n’)
c). Original tuple: (‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
After removing an element: (‘p’, ‘y’, ‘t’, ‘n’)
d). Original tuple: (‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
After removing an element: (‘p’, ‘y’, ‘o’, ‘n’)

Corresct answer is: c). Original tuple: (‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
After removing an element: (‘p’, ‘y’, ‘t’, ‘n’)
Explanation: The code converts the tuple `tup` into a list, removes the element `’h’` from the list, and then converts it back to a tuple. The output will be `(‘p’, ‘y’, ‘t’, ‘n’)`.

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

tup = (‘s’, ‘q’, ‘a’, ‘t’, ‘o’, ‘o’, ‘l’, ‘s’)
print(“Index of a: “, tup.index(‘a’))

a). Index of a: 2
b). Index of a: 3
c). Index of a: 6
d). Error: ‘a’ is not in tuple ‘tup’

Corresct answer is: a). Index of a: 2
Explanation: The code defines a tuple tup with the elements ‘s’, ‘q’, ‘a’, ‘t’, ‘o’, ‘o’, ‘l’, ‘s’. The index() method is used to find the index of the first occurrence of the element ‘a’ in the tuple. Since ‘a’ is at index 2, the output will be “Index of a: 2”.

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

tup = (‘v’, ‘i’, ‘r’, ‘a’, ‘t’)
print(“Original tuple: “, tup)
print(“Length of the tuple: “, len(tup))

a). Original tuple: (‘v’, ‘i’, ‘r’, ‘a’, ‘t’) Length of the tuple: 5
b). Original tuple: v i r a t Length of the tuple: 5
c). Original tuple: (‘v’, ‘i’, ‘r’, ‘a’, ‘t’) Length of the tuple: 1
d). Original tuple: v i r a t Length of the tuple: 1

Corresct answer is: a). Original tuple: (‘v’, ‘i’, ‘r’, ‘a’, ‘t’) Length of the tuple: 5
Explanation: The code defines a tuple tup with elements ‘v’, ‘i’, ‘r’, ‘a’, and ‘t’. The first print() statement outputs the original tuple as (‘v’, ‘i’, ‘r’, ‘a’, ‘t’), and the second print() statement outputs the length of the tuple, which is 5.

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

tup = ((5, ‘s’), (6, ‘l’))
print(“Tuple: “, tup)
D = dict(tup)
print(“Dictionary: “, D)

a). Tuple: ((5, ‘s’), (6, ‘l’))
Dictionary: {5: ‘s’, 6: ‘l’}
b). Tuple: [(5, ‘s’), (6, ‘l’)]
Dictionary: {5: ‘s’, 6: ‘l’}
c). Tuple: ((5, ‘s’), (6, ‘l’))
Dictionary: {(5, ‘s’): (6, ‘l’)}
d). Tuple: [(5, ‘s’), (6, ‘l’)]
Dictionary: {(5, ‘s’): (6, ‘l’)}

Corresct answer is: a). Tuple: ((5, ‘s’), (6, ‘l’))
Dictionary: {5: ‘s’, 6: ‘l’}
Explanation: The code first initializes a tuple `tup` containing two inner tuples. Printing the `tup` variable will output `((5, ‘s’), (6, ‘l’))`. Then, the `dict()` function is used to convert the tuple `tup` into a dictionary `D`. The output of `D` will be `{5: ‘s’, 6: ‘l’}`, where the first element of each inner tuple becomes a key, and the second element becomes its corresponding value in the dictionary.

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

list1 = [(3, 4), (6, 2), (8, 9)]
print(“List: “, list1)
print(“Tuple: “, list(zip(*list1)))

a). List: [(3, 4), (6, 2), (8, 9)]
Tuple: [(3, 6, 8), (4, 2, 9)]
b). List: [(3, 4), (6, 2), (8, 9)]
Tuple: [(4, 2, 9), (3, 6, 8)]
c). List: [(3, 6, 8), (4, 2, 9)]
Tuple: [(3, 4), (6, 2), (8, 9)]
d). List: [(4, 2, 9), (3, 6, 8)]
Tuple: [(3, 4), (6, 2), (8, 9)]

Corresct answer is: a). List: [(3, 4), (6, 2), (8, 9)]
Tuple: [(3, 6, 8), (4, 2, 9)]
Explanation: The code uses the zip() function with the unpacking operator * on the list list1. The zip() function takes the elements from each tuple in list1 and groups them together. The * operator is used to unpack the list of tuples. So, list(zip(*list1)) will result in [(3, 6, 8), (4, 2, 9)], which is a list of tuples where each tuple contains the corresponding elements from the original tuples.

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

tup = (4, 6, 8, 3, 1)
print(“Original tuple: “, tup)
tup1 = tuple(reversed(tup))
print(“Reversed tuple: “, tup1)

a). Original tuple: (4, 6, 8, 3, 1), Reversed tuple: (1, 3, 8, 6, 4)
b). Original tuple: (1, 3, 8, 6, 4), Reversed tuple: (4, 6, 8, 3, 1)
c). Original tuple: (4, 6, 8, 3, 1), Reversed tuple: (4, 6, 8, 3, 1)
d). Original tuple: (1, 3, 8, 6, 4), Reversed tuple: (1, 3, 8, 6, 4)

Corresct answer is: a). Original tuple: (4, 6, 8, 3, 1), Reversed tuple: (1, 3, 8, 6, 4)
Explanation: The original tuple is `(4, 6, 8, 3, 1)`. The `reversed()` function returns an iterator that yields the items of the tuple in reverse order. Converting the iterator to a tuple using `tuple()` will give the reversed tuple `(1, 3, 8, 6, 4)`. The code then prints the original tuple and the reversed tuple, resulting in the output “Original tuple: (4, 6, 8, 3, 1), Reversed tuple: (1, 3, 8, 6, 4)”.

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

l = [(‘s’,2),(‘q’,1),(‘a’,1),(‘s’,3),(‘q’,2),(‘a’,4)]
print(“List of tuples: “,l)
D = {}
for a, b in l:
    D.setdefault(a, []).append(b)
print(“Dictionary: “,D)

a). List of tuples: [(‘s’,2),(‘q’,1),(‘a’,1),(‘s’,3),(‘q’,2),(‘a’,4)]
Dictionary: {‘s’: [2, 3], ‘q’: [1, 2], ‘a’: [1, 4]}
b). List of tuples: [(‘s’,2),(‘q’,1),(‘a’,1),(‘s’,3),(‘q’,2),(‘a’,4)]
Dictionary: {‘s’: [3, 2], ‘q’: [2, 1], ‘a’: [4, 1]}
c). List of tuples: [(‘s’,2),(‘q’,1),(‘a’,1),(‘s’,3),(‘q’,2),(‘a’,4)]
Dictionary: {‘s’: [2, 3], ‘q’: [2, 1], ‘a’: [4, 1]}
d). List of tuples: [(‘s’,2),(‘q’,1),(‘a’,1),(‘s’,3),(‘q’,2),(‘a’,4)]
Dictionary: {‘s’: [3, 2], ‘q’: [1, 2], ‘a’: [1, 4]}

Corresct answer is: a). List of tuples: [(‘s’,2),(‘q’,1),(‘a’,1),(‘s’,3),(‘q’,2),(‘a’,4)]
Dictionary: {‘s’: [2, 3], ‘q’: [1, 2], ‘a’: [1, 4]}
Explanation: The code initializes an empty dictionary D and iterates through the tuples in the list l. For each tuple (a, b), it uses the setdefault() method to create a new key a in the dictionary if it doesn’t exist and appends b to the corresponding value list. The resulting dictionary will be {‘s’: [2, 3], ‘q’: [1, 2], ‘a’: [1, 4]}.

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

list1 = [(2,5,7),(3,4),(8,9,0,5)]
print(“Original list: “, list1)
for tup in list1:
    if len(tup) == 2:
        list1.remove(tup)

print(“After removing tuple having length 2: “, list1)

a). Original list: [(2,5,7),(3,4),(8,9,0,5)]
After removing tuple having length 2: [(2,5,7),(3,4),(8,9,0,5)]
b). Original list: [(2,5,7),(3,4),(8,9,0,5)]
After removing tuple having length 2: [(3,4),(8,9,0,5)]
c). Original list: [(2,5,7),(3,4),(8,9,0,5)]
After removing tuple having length 2: [(2,5,7),(8,9,0,5)]
d). Original list: [(2,5,7),(3,4),(8,9,0,5)]
After removing tuple having length 2: [(2,5,7),(3,4)]

Corresct answer is: c). Original list: [(2,5,7),(3,4),(8,9,0,5)]
After removing tuple having length 2: [(2,5,7),(8,9,0,5)]
Explanation: The code iterates over each tuple in `list1`. If a tuple has a length of 2, it is removed from the list. In the original list, the second tuple `(3,4)` has a length of 2, so it is removed). The resulting list after removing the tuple is `[(2,5,7),(8,9,0,5)]`.

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

l = [(None,),(5, 4),(1,6,7),(None,1)]
print(“Original list: “, l)
for tup in l:
    for ele in tup:
        if ele == None:
            l.remove(tup)
print(“After removing tuples: “, l)

a). Original list: [(None,),(5, 4),(1,6,7),(None,1)]
After removing tuples: [(5, 4),(1,6,7),(None,1)]
b). Original list: [(None,),(5, 4),(1,6,7),(None,1)]
After removing tuples: [(5, 4),(1,6,7)]
c). Original list: [(None,),(5, 4),(1,6,7),(None,1)]
After removing tuples: [(5, 4),(1,6,7),(None,)]
d). Original list: [(None,),(5, 4),(1,6,7),(None,1)]
After removing tuples: [(5, 4),(1,6,7),(None,1)]

Corresct answer is: b). Original list: [(None,),(5, 4),(1,6,7),(None,1)]
After removing tuples: [(5, 4),(1,6,7)]
Explanation: The code iterates over the list l and checks each element of the tuples. If an element is equal to None, the corresponding tuple is removed from the list.

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

l = [(1, 5), (7, 8), (4, 0), (3, 6)]
print(“Original list of tuples: “, l)
res = sorted(l, key=lambda tup: tup[0])
print(“After sorting list of tuples by first element: “, res)

a). Original list of tuples: [(1, 5), (7, 8), (4, 0), (3, 6)]
After sorting list of tuples by first element: [(1, 5), (3, 6), (4, 0), (7, 8)]
b). Original list of tuples: [(1, 5), (7, 8), (4, 0), (3, 6)]
After sorting list of tuples by first element: [(4, 0), (3, 6), (1, 5), (7, 8)]
c). Original list of tuples: [(1, 5), (7, 8), (4, 0), (3, 6)]
After sorting list of tuples by first element: [(1, 5), (7, 8), (3, 6), (4, 0)]
d). Original list of tuples: [(1, 5), (7, 8), (4, 0), (3, 6)]
After sorting list of tuples by first element: [(7, 8), (4, 0), (3, 6), (1, 5)]

Corresct answer is: a). Original list of tuples: [(1, 5), (7, 8), (4, 0), (3, 6)]
After sorting list of tuples by first element: [(1, 5), (3, 6), (4, 0), (7, 8)]
Explanation: The code sorts the list of tuples l based on the first element of each tuple using the sorted() function and a lambda function as the key. The output will be [(4, 0), (3, 6), (1, 5), (7, 8)], where the tuples are sorted in ascending order based on the first element.

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

l = [(1, 5), (7, 8), (4, 0), (3, 6)]
print(“Original list of tuples: “, l)
res = sorted(l, key=lambda tup: tup[1])
print(“After sorting list of tuples by second element: “, res)

a). Original list of tuples: [(1, 5), (7, 8), (4, 0), (3, 6)]
After sorting list of tuples by second element: [(4, 0), (1, 5), (3, 6), (7, 8)]
b). Original list of tuples: [(1, 5), (7, 8), (4, 0), (3, 6)]
After sorting list of tuples by second element: [(4, 0), (7, 8), (1, 5), (3, 6)]
c). Original list of tuples: [(1, 5), (7, 8), (4, 0), (3, 6)]
After sorting list of tuples by second element: [(4, 0), (3, 6), (1, 5), (7, 8)]
d). Original list of tuples: [(1, 5), (7, 8), (4, 0), (3, 6)]
After sorting list of tuples by second element: [(1, 5), (3, 6), (4, 0), (7, 8)]

Corresct answer is: a). Original list of tuples: [(1, 5), (7, 8), (4, 0), (3, 6)]
After sorting list of tuples by second element: [(4, 0), (1, 5), (3, 6), (7, 8)]
Explanation: The original list of tuples l is [(1, 5), (7, 8), (4, 0), (3, 6)]. The sorted() function is used to sort the list of tuples based on the second element of each tuple. The key=lambda tup: tup[1] specifies that the second element of each tuple should be used for sorting.

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

l = [(4, 5, 6), (6,), (2, 3), (6, 7, 8, 9, 0)]
print(“Original list of tuples: “, l)
res = sorted(l, key=lambda tup: len(tup))
print(“After sorting list of tuples by length of tuples: “, res)

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

Corresct answer is: a). Original list of tuples: [(4, 5, 6), (6,), (2, 3), (6, 7, 8, 9, 0)]
After sorting list of tuples by length of tuples: [(6,), (2, 3), (4, 5, 6), (6, 7, 8, 9, 0)]
Explanation: The code sorts the list of tuples `l` based on the length of each tuple using the `sorted()` function and a lambda function as the `key` parameter. The resulting sorted list of tuples will be `[(6,), (2, 3), (4, 5, 6), (6, 7, 8, 9, 0)]`.

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

tup = (‘a’, ‘b’, ‘c’, ‘d’, ‘b’, ‘a’, ‘b’)
print(“Original tuple: “, tup)
d = {}
for char in tup:
    if char not in d:
        d[char] = 0
    if char in d:
        d[char] += 1
print(“Frequency count: “, d)

a). Original tuple: (‘a’, ‘b’, ‘c’, ‘d’, ‘b’, ‘a’, ‘b’)
Frequency count: {‘a’: 2, ‘b’: 3, ‘c’: 1, ‘d’: 1}
b). Original tuple: (‘a’, ‘b’, ‘c’, ‘d’, ‘b’, ‘a’, ‘b’)
Frequency count: {‘a’: 2, ‘b’: 2, ‘c’: 1, ‘d’: 1}
c). Original tuple: (‘a’, ‘b’, ‘c’, ‘d’, ‘b’, ‘a’, ‘b’)
Frequency count: {‘a’: 3, ‘b’: 3, ‘c’: 1, ‘d’: 1}
d). Original tuple: (‘a’, ‘b’, ‘c’, ‘d’, ‘b’, ‘a’, ‘b’)
Frequency count: {‘a’: 1, ‘b’: 2, ‘c’: 1, ‘d’: 1}

Corresct answer is: a). Original tuple: (‘a’, ‘b’, ‘c’, ‘d’, ‘b’, ‘a’, ‘b’)
Frequency count: {‘a’: 2, ‘b’: 3, ‘c’: 1, ‘d’: 1}
Explanation: The code iterates over each character in the tuple tup and updates the dictionary d with the frequency count of each character.

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

l = [(1,4),(4,5,6),(2,),(7,6,8,9),(3,5,6,0,1)]
l1 = []
print(“Original list of tuples: “, l)
for tup in l:
    if len(tup)>3:
        l1.append(tup)
print(“After removing tuples: “, l1)

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

Corresct answer is: c). Original list of tuples: [(1,4),(4,5,6),(2,),(7,6,8,9),(3,5,6,0,1)]
After removing tuples: [(7,6,8,9),(3,5,6,0,1)]
Explanation: The code iterates over each tuple in the list l and checks if the length of the tuple is greater than 3. If it is, the tuple is appended to the list l1. The original list of tuples is [(1,4),(4,5,6),(2,),(7,6,8,9),(3,5,6,0,1)], and after removing tuples with length greater than 3, the updated list is [(4,5,6),(7,6,8,9),(3,5,6,0,1)].

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

l = [(‘s’,’q’),(‘t’,’o’,’o’,’l’),(‘p’,’y’),(‘s’,’q’)]
print(“Original list of tuples: “,l)
d = {}
for tup in l:
    if tup not in d:
        d[tup] = 0
    if tup in d:
        d[tup] += 1
print(“Frequency count : “,d)

a). Original list of tuples: [(‘s’,’q’),(‘t’,’o’,’o’,’l’),(‘p’,’y’),(‘s’,’q’)]
Frequency count: {(‘s’,’q’): 2, (‘t’,’o’,’o’,’l’): 1, (‘p’,’y’): 1}
b). Original list of tuples: [(‘s’,’q’),(‘t’,’o’,’o’,’l’),(‘p’,’y’),(‘s’,’q’)]
Frequency count: {(‘s’,’q’): 1, (‘t’,’o’,’o’,’l’): 1, (‘p’,’y’): 1}
c). Original list of tuples: [(‘s’,’q’),(‘t’,’o’,’o’,’l’),(‘p’,’y’),(‘s’,’q’)]
Frequency count: {(‘s’,’q’): 1, (‘t’,’o’,’o’,’l’): 2, (‘p’,’y’): 1}
d). Original list of tuples: [(‘s’,’q’),(‘t’,’o’,’o’,’l’),(‘p’,’y’),(‘s’,’q’)]
Frequency count: {(‘s’,’q’): 2, (‘t’,’o’,’o’,’l’): 2, (‘p’,’y’): 1}

Corresct answer is: a). Original list of tuples: [(‘s’,’q’),(‘t’,’o’,’o’,’l’),(‘p’,’y’),(‘s’,’q’)]
Frequency count: {(‘s’,’q’): 2, (‘t’,’o’,’o’,’l’): 1, (‘p’,’y’): 1}
Explanation: The code initializes an empty dictionary d). It then iterates over each tuple tup in the list l. If the tuple tup is not already in the dictionary d, it adds it with a value of 0. Then, it increments the value of tup in d by 1. The final d dictionary contains the frequency count of each tuple in the list.

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

tup = (1, 2, 3, 4)
if len(set(tup)) == len(tup):
    print(“Tuple is distinct”)
else:
    print(“Tuple is not distinct”)

a). Tuple is distinct
b). Tuple is not distinct
c). Error: invalid syntax
d). Error: unsupported operand type(s)

Corresct answer is: a). Tuple is distinct
Explanation: The code snippet checks whether the given tuple tup is distinct or not. It does so by converting the tuple to a set using the set() function, which removes any duplicate elements since sets only contain unique elements. The len() function is then used to compare the lengths of the original tuple and the set. If the lengths are equal, it means that all elements in the tuple are distinct. 

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

tup1 = (3, 4, 5, 4, 6, 3)
if len(set(tup1)) == len(tup1):
    print(“Tuple is distinct”)
else:
    print(“Tuple is not distinct”)

a). “Tuple is distinct”
b). “Tuple is not distinct”
c). Error: invalid syntax
d). Error: undefined variable ‘tup1’

Corresct answer is: b). “Tuple is not distinct”
Explanation: The code checks if the length of the set created from tup1 is equal to the length of tup1. If they are not equal, it means there are duplicate elements in the tuple, and the output will be “Tuple is not distinct.”

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

tup = (4, 1, 7, 5)
print(“Original tuple: “, tup)
tup1 = str(tup)
print(“After converting to a string: “, tup1)
print(“Type of new tuple: “, type(tup1))

a). Original tuple: (4, 1, 7, 5)
After converting to a string: (4, 1, 7, 5)
Type of new tuple: tuple
b). Original tuple: (4, 1, 7, 5)
After converting to a string: (4, 1, 7, 5)
Type of new tuple: <class ‘str’>
c). Original tuple: [4, 1, 7, 5]
After converting to a string: ‘4, 1, 7, 5’
Type of new tuple: list
d). Original tuple: (4, 1, 7, 5)
After converting to a string: [4, 1, 7, 5]
Type of new tuple: list

Corresct answer is: b). Original tuple: (4, 1, 7, 5)
After converting to a string: (4, 1, 7, 5)
Type of new tuple: <class ‘str’>
Explanation: In the given code, the tuple `tup` is converted to a string using the `str()` function. The resulting string will be (4, 1, 7, 5). The `type()` function is used to determine the type of the variable `tup1`, which will be `str`.

Leave a Comment