Exit Chapter 11 ยท ๐Ÿ”’ Tuples โ€” Quiz

Playing as a guest? to earn XP and track your progress.

Question 1 of 51 0 correct so far

๐Ÿ”’ Tuples Quiz

Q1.

Which statement best describes a tuple?

Explanation

A tuple is an ordered, immutable collection, usually written in round brackets and separated by commas.

Q2.

Which kind of data is best stored in a tuple rather than a list?

Explanation

Tuples suit data that must stay fixed, such as the days of the week or coordinates.

Q3.

What is T after running T = 4, 5, 6?

Explanation

The brackets are optional; the commas make the tuple.

Q4.

What is tuple("ABC")?

Explanation

tuple() converts a sequence into a tuple of its items; a string gives its characters.

Q5.

What is the type of x after x = (7)?

Explanation

(7) is just 7 inside grouping brackets; a tuple needs a comma.

Q6.

Which statement creates a tuple with ONE element?

Explanation

A single-element tuple needs a trailing comma: (5,).

Q7.

What is printed after b = (5,) and then print(b)?

Explanation

Python shows a one-element tuple with its trailing comma.

Q8.

What is the difference between T = (10) and T = (10,)?

Explanation

It is the comma, not the brackets, that creates a tuple.

Q9.

For T = ('a', 'e', 'i', 'o', 'u'), what does print(T[0], T[2], T[-1]) display?

Explanation

T[0] = 'a', T[2] = 'i' and T[-1] = 'u'.

Q10.

For T = ('a', 'e', 'i', 'o', 'u'), what does T[5] give?

Explanation

The valid indices are 0 to 4.

Q11.

What does T[0] = 100 give for T = (10, 20, 30)?

Explanation

Tuples are immutable, so their elements cannot be reassigned.

Q12.

What does T.append(40) give for a tuple T?

Explanation

Tuples have no append() method.

Q13.

Which statement is allowed on a tuple T?

Explanation

del T deletes the whole tuple variable; deleting individual items is not allowed.

Q14.

What is printed after this code?

T = (10, 20, 30) T = T + (40,) print(T)

Explanation

A NEW tuple is created by concatenation and assigned to T.

Q15.

How can you 'change' the element at index 1 of T = (10, 20, 30) to 200?

Explanation

L = list(T); L[1] = 200; T = tuple(L) gives (10, 200, 30).

Q16.

What is printed after this code?

T = (1, 2, [3, 4]) T[2][0] = 30 print(T)

Explanation

The tuple still holds the same list object; only the contents of that list changed.

Q17.

For T = (1, 2, [3, 4]), which statement raises a TypeError?

Explanation

Replacing an element of the tuple is not allowed, but the list inside it can be modified.

Q18.

What is (1, 2) + (3, 4)?

Explanation
  • concatenates two tuples.
Q19.

What is ("Hi",) * 3?

Explanation
  • repeats the elements of the tuple.
Q20.

What is (1, 2) + (3,) * 2?

Explanation
  • is applied first: (3,) * 2 = (3, 3); then + joins the tuples to give (1, 2, 3, 3).
Q21.

What does T + 3 give for T = (1, 2)?

Explanation

Only a tuple can be concatenated with a tuple; write T + (3,).

Q22.

What is the result of (1, 2, 5) < (1, 3)?

Explanation

Tuples are compared element by element; at index 1, 2 < 3.

Q23.

For T = (10, 20, 30, 40, 50, 60), what is T[4:1:-1]?

Explanation

Going backwards from index 4: indices 4, 3, 2 (1 is excluded).

Q24.

For T = (10, 20, 30, 40, 50, 60), what are T[2] and T[2:3]?

Explanation

Indexing returns an element, while slicing always returns a tuple.

Q25.

For T = (10, 20, 30, 40, 50, 60), what is T[::2]?

Explanation

It takes every 2nd element from the start.

Q26.

For T = ('P', 'Y', 'T', 'H', 'O', 'N'), what is T[-1:-4:-1]?

Explanation

It starts at -1 (N) and moves backwards to -3 (H); -4 is excluded.

Q27.

For T = ('P', 'Y', 'T', 'H', 'O', 'N'), what is 'y' in T?

Explanation

Membership is case-sensitive; T contains 'Y', not 'y'.

Q28.

What is 'packing' a tuple?

Explanation

Packing combines several values into one tuple.

Q29.

What is 'unpacking' a tuple?

Explanation

For example, roll, name, marks = (101, "Aman", 88).

Q30.

After roll, name, marks = (101, "Aman", 88), what is the value of name?

Explanation

The values are assigned in order: roll = 101, name = 'Aman', marks = 88.

Q31.

What does a, b = (1, 2, 3) give?

Explanation

The number of variables must equal the number of elements.

Q32.

What does a, b, c = (1, 2) give?

Explanation

There are fewer values than variables.

Q33.

The one-line swap a, b = b, a works because of:

Explanation

The right side packs (b, a) into a tuple and the left side unpacks it.

Q34.

Which are the only two methods available for tuples?

Explanation

Tuples are read-only, so they have only the two methods that read them: count() and index().

Q35.

For T = (4, 9, 1, 9, 7), what are T.count(9) and T.index(9)?

Explanation

9 occurs twice, and its first occurrence is at index 1.

Q36.

For T = (4, 9, 1, 9, 7), what is sum(T)?

Explanation

4 + 9 + 1 + 9 + 7 = 30.

Q37.

What does sorted((3, 1, 2)) return?

Explanation

sorted() always returns a LIST, even when it is given a tuple.

Q38.

How do you get a sorted TUPLE from T = (40, 10, 30, 20)?

Explanation

Wrap sorted() in tuple(); tuples have no sort() method.

Q39.

What does T.sort() give for a tuple T?

Explanation

Tuples have no sort(), append(), insert(), remove() or pop() methods.

Q40.

What is the output of print(len((1, (2, 3), [4, 5, 6])))?

Explanation

The nested tuple and the list each count as ONE element.

Q41.

For results = (("Riya", 92), ("Aman", 85), ("Zoya", 78)), what is results[1][0]?

Explanation

results[1] is ('Aman', 85), and index 0 of that is 'Aman'.

Q42.

What does this code print first?

for name, marks in (("Riya", 92), ("Aman", 85)): print(name, "scored", marks)

Explanation

Each inner tuple is unpacked into name and marks as the loop runs.

Q43.

Which statement comparing lists and tuples is correct?

Explanation

Lists are mutable with many methods; tuples are immutable, slightly faster, and can be dictionary keys.

Q44.

Which is an advantage of tuples over lists?

Explanation

Tuples are read-only, so values like the months of the year cannot be changed by mistake.

Q45.

Which can be used as a dictionary key: a tuple of immutable values or a list?

Explanation

Tuples of immutable values can be dictionary keys; lists cannot.

Q46.

What is printed after this code?

T = (10, 20, 30, 40, 50) print(T[1:4], T[-2], T[::-2])

Explanation

Indices 1-3 give (20, 30, 40); T[-2] is 40; the reverse step of 2 gives (50, 30, 10).

Q47.

What is printed after this code?

T = (10, 20, 30, 40, 50) a, b, c = T[:3] print(a + b + c, T.index(c))

Explanation

T[:3] is (10, 20, 30); their sum is 60, and 30 is at index 2.

Q48.

What is printed after this code?

T1 = (1, 2, [3, 4]) T1[2].append(5) print(T1)

Explanation

The list inside the tuple is mutable, so append() works on it.

Q49.

What is printed after this code?

T3 = ("A",) * 2 + ("B",) print(T3, T3.count("A"), T3.index("B"))

Explanation

T3 is ('A', 'A', 'B'); 'A' occurs twice, and 'B' is at index 2.

Q50.

In a tuple program, how do you 'add' an element x to T?

Explanation

Create a new tuple with concatenation โ€” and remember the comma in (x,).

Q51.

For the input (10, 20, 30, 40, 50), what tuple of 'elements above the mean' is printed?

Explanation

The mean is 30.0; only 40 and 50 are greater than it.