Quiz 1 Overview
This quiz consolidates everything you learned about Python collections including lists, tuples, dictionaries, sets, and string operations.
- Format: Three sections. Questions display one section at a time after you start.
- Timer: 60 minutes total. The timer cannot be paused or reset once the quiz starts.
- Scoring:
- MCQ questions may award partial points for each correct option.
- Code output questions expect exact answers and grant full points when correct.
- Coding questions are evaluated based on test cases.
- Your final score and percentage are computed automatically when you submit or when time runs out.
Python Collections - Chapter 3 Quiz 1
Quiz Complete!
Section: Multiple Choice Questions
Which of the following is a Python Tuple?
5 Points(1)
1,
None, it’s a trick question
(1.2.3)
Tuples are mutable
5 PointsTrue
False
Which of the following statements is used to create an empty set in Python?
5 PointsNone, it’s a trick question
()
[]
{}
To get key,value pairs from a dictionary we use the
5 Points.items() method
.fromkeys() method
.values() method
.keys() method
.get() method
In Python, dictionaries are immutable
5 PointsFalse, they are mutable
True
False, but their keys are mutable
If s is a string, how can we reverse it?
5 Pointss[::-1]
s[-1:0:-1]
s[len(s):0:-1]
s[len(s):-1:-1]
Section: Code Output Questions
What will be the output of the following code?
5 Points1for i in range(3, -6, 1):2 print(i, end=" ")Nothing, it’s a trick question
3 4 5
3 4 5 6
3 2 1 0 -1 -2 -3 -4 -5 -6
3 2 1 0 -1 -2 -3 -4 -5
What will be the output here?
5 Points1m = [1, 2, 3, 4]2n = m3n[3] = 24print(n, m)[1, 2, 3, 2] [1, 2, 3, 2]
[1, 2, 3, 2] [1, 2, 3, 4]
[1, 2, 3, 4] [1, 2, 3, 2]
[1, 2, 3, 4] [1, 2, 3, 4]
What will be the output of the following code?
5 Points1str1 = '{2}, {1} and {0}'.format('a', 'b', 'c')2str2 = '{0}{1}{0}'.format('abra', 'cad')3print(str1, str2)c, b and a abracadabra
a, b and c abracadabra
c, b and a abraabracad
a, b and c abraabracad
Error
Write down the exact output (without spaces):
5 Points1L = [1, 2, 3, 2, 1, 3]2J = []3for i in L:4 if i not in J:5 J.append(i)6for i in J:7 print(i, end="")Without spaces, what is the output of the following code?
5 Points1a = ("Hello") * 32print(a)What is the output of the following code?
5 Points1a = [1, 2, 3, 4, 5]2while a:3 if len(a) < 3:4 break5 print(a.pop(),end="")6else:7 print("End")Extract Tuple Slice
10 PointsWrite a Python code to extract ('c', 'd') from the tuple:
1b = ('a', 'b', 'c', 'd', 'e', 'f')Do not include any spaces in your answer.
Slice Assignment
10 PointsGiven the following list:
1a = [1, 2, 6]Write a Python statement using slice assignment which fills in the missing values such that a becomes [1, 2, 3, 4, 5, 6].
Section: Programming Questions
Unique Elements Check
15 PointsWrite a program which reads the size of a list, followed by list elements (each on a new line), then prints Unique if all elements are unique, otherwise it prints NotUnique.
Unique Elements Check
Compare the length of the list with the length of a set created from the list. If they’re equal, all elements are unique.
Example Solution:
1n = int(input())2L = []3for _ in range(n):4 L.append(int(input()))5if len(L) == len(set(L)):6 print("Unique")7else:8 print("NotUnique")Print Even Indices from Tuple
15 PointsWrite a program which reads the size of a tuple, followed by tuple elements (each on a new line), then prints all the numbers in it at even indices (0, 2, 4, …), each on a new line.
Print Even Indices from Tuple
Read the size, read the elements, create a tuple, and iterate through even indices.
Example Solution:
1n = int(input())2elements = []3for _ in range(n):4 elements.append(int(input()))5elements = tuple(elements)6for i in range(0, len(elements), 2):7 print(elements[i])Remove Even Values from Tuple
15 PointsComplete the following function: given a tuple T (read from input), it should remove all the elements with even values, then return and print the new tuple. Read the size first, then the elements (each on a new line).
Remove Even Values from Tuple
Since tuples are immutable, create a new tuple with only odd values.
Example Solution:
1n = int(input())2T = []3for _ in range(n):4 T.append(int(input()))5T = tuple(T)6lst = []7for x in T:8 if x % 2 != 0:9 lst.append(x)10result = tuple(lst)11print(result)Most Repeated Value in Dictionary
15 PointsWrite a program that reads the number of items, followed by the items (as strings), creates a dictionary counting occurrences, then prints the most repeated value. The testcases are generated so that there is no tie.
Most Repeated Value in Dictionary
Count occurrences using a dictionary, then find the key with the maximum value.
Example Solution:
1n = int(input())2D = {}3for _ in range(n):4 item = input()5 most_repeated = item6 D[item] = D.get(item, 0) + 17
8for key, value in D.items():9 if D[most_repeated] < value:10 most_repeated = key11print(most_repeated)Consecutive Differences
15 PointsWrite a Python program that reads the size of a list, followed by list elements (each on a new line), then finds and prints the difference between consecutive elements (space-separated).
Consecutive Differences
Iterate through the list and calculate the difference between each element and the next one.
Example Solution:
1n = int(input())2L = []3for _ in range(n):4 L.append(int(input()))5differences = []6for i in range(len(L) - 1):7 differences.append(str(L[i+1] - L[i]))8print(" ".join(differences))Replace Vowels with X
15 PointsWrite a program which reads a sentence and then replaces every vowel letter (a, e, i, o, u, A, E, I, O, U) with X without using the replace method. Print the modified sentence.
Replace Vowels with X
Iterate through each character and replace vowels with ‘X’, keeping other characters unchanged.
Example Solution:
1sentence = input()2vowels = "aeiouAEIOU"3result = ""4for char in sentence:5 if char in vowels:6 result += "X"7 else:8 result += char9print(result)Swap Cases
15 PointsRead a string and swap cases (convert uppercase to lowercase and vice versa). Print the result.
Swap Cases
Iterate through each character and swap its case using string methods.
Example Solution:
1s = input()2result = ""3for char in s:4 if char.isupper():5 result += char.lower()6 elif char.islower():7 result += char.upper()8 else:9 result += char10print(result)Common Elements in Two Lists
15 PointsWrite a program that reads two lists: first the size and elements of list 1 (each element on a new line), then the size and elements of list 2 (each element on a new line). Print the common elements (space-separated, in the order they appear in the first list). If no common elements, print nothing.
Common Elements in Two Lists
Find the intersection of both lists, preserving the order from the first list.
Example Solution:
1n1 = int(input())2list1 = []3for _ in range(n1):4 list1.append(int(input()))5n2 = int(input())6list2 = []7for _ in range(n2):8 list2.append(int(input()))9set2 = set(list2)10common = []11for x in list1:12 if x in set2:13 common.append(x)14result = []15for x in common:16 print(x, end=" ")