Quiz 2 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 2
Quiz Complete!
Section: Multiple Choice Questions
How can we create the list k = [2, 4, 6] from the list m = [1, 2, 3, 4, 5, 6]?
5 Pointsk = m[1::2]
k = m[0::2]
k = m[0::1]
Choose the correct answer: The following code evaluates to
5 Points1(bool(0) and bool(1)) or (bool("") and bool("0"))False, since bool of 0 is False.
False, since bool of an empty string is False.
True, it’s a trick question
False, since bool of 0 and bool of an empty string is False.
False, since bool of an empty string and bool of the string “0” is False.
Which of the following is invalid?
5 Pointsabc = 1,000,000
a b c = 1000 2000 3000
a,b,c = 1000, 2000, 3000
a_b_c = 1,000,000
What is the problem in the following code?
5 Points1k = [1, 2, 3, 4, 5]2print(k)3k[9] = 104print(k)5
6s = "Hello World"7s[3] = 'F'8print(s)9
10x = 311y = 412z = 3 // y13m = 3 // z14
15if x > 3:16print(x)17else if y > 4:18print(y)IndexError: list assignment index out of range (k[9] = 10)
TypeError: ‘str’ object does not support item assignment (s[3] = ‘F’)
ZeroDivisionError: integer division or modulo by zero (m = 3 // z where z = 0)
IndentationError and SyntaxError: missing indentation and ‘else if’ should be ‘elif’
Section: Code Output Questions
What will be the output of the following program (without spaces)?
5 Points1k = [4, 5, 6, 1, 2, 3]2
3s = ""4for i in range(10):5 s += str(i)6
7for i in k:8 if str(i) in s:9 print(i, end="")What will be the output of the following code?
5 Points1T = (1, 2, 3, 4, 5, 6, 7, 8)2print(T[T.index(5)], end = " ")3print(T[T[T[6]-3]-6])What will be the output of the following code?
5 Points1D = {1 : {'A' : {1 : "A", 2 : "B"}, 2 : "B"}, 3 :"C", 'B' : "D", "D": 'E'}2print(D[D[D[1][2]]], end = " ")3print(D[D[1]["A"][2]])What will be the output of the following code?
5 Points1D = {1 : [1, 2, 3], 2: (4, 6, 8)}2D[1].append(4)3print(D[1], end = " ")4L = [D[2]]5L.append(10)6D[2] = tuple(L)7print(D[2])What will be the output of the following code?
5 Points1line = "I'll be here."2botato = ""3for i in line:4 if i in "abcdefghijklmnopqrstuvwxyz":5 botato += chr(ord(i)+3)6 else:7 botato += i8print(botato)Without spaces, what is the output of the following code?
5 Points1a, b, c = tuple([[1], [2], [3]])2[a.extend(b)].extend(c)3print(a)Section: Programming Questions
Triangle Pattern
20 PointsWrite a program which reads an integer n from the user and prints a triangle using only one loop, as follows for n = 5:
1 *2 ***3 *****4 *******5*********Triangle Pattern
Use string multiplication and formatting to create the triangle pattern. For each row i (0 to n-1), print (n-i-1) spaces followed by (2*i+1) asterisks.
Example Solution:
1n = int(input())2for i in range(n):3 spaces = " " * (n - i - 1)4 stars = "*" * (2 * i + 1)5 print(spaces + stars)Pascal Case Conversion
15 PointsWrite a program which reads a sentence and converts it to Pascal case (first letter of each word capitalized, no spaces). Print the result.
Pascal Case Conversion
Split the sentence into words, capitalize the first letter of each word, and join them together.
Example Solution:
1sentence = input()2words = sentence.split()3transformed = []4for word in words:5 transformed.append(word.capitalize())6result = "".join(transformed)7print(result)Common Exams
20 PointsRead two lists of grades between two students. For each student, first read the number of exams, then for each exam read the exam name and grade (space-separated). Print the exams that both students took and succeeded in (grade >= 60), each on a new line, in the order they appear in the first student’s list.
Common Exams
Store each student’s successful exams in a set, find the intersection, and print in the order of the first student’s list.
Example Solution:
1n1 = int(input())2student1 = []3for _ in range(n1):4 exam, grade = input().split()5 if int(grade) >= 60:6 student1.append(exam)7
8n2 = int(input())9student2 = []10for _ in range(n2):11 exam, grade = input().split()12 if int(grade) >= 60:13 student2.append(exam)14
15common = set(student1) & set(student2)16for exam in student1:17 if exam in common:18 print(exam)Student Dictionary
20 PointsRead multiple students, each with name and a list of grades. First read the number of students, then for each student read their name, the number of grades, and the grades (space-separated). Add them all to a dictionary, then print the dictionary.
Student Dictionary
Create a dictionary where keys are student names and values are lists of grades.
Example Solution:
1n = int(input())2students = {}3for _ in range(n):4 name = input()5 num_grades = int(input())6 grades = input().split()7 grades_int = []8 for g in grades:9 grades_int.append(int(g))10 students[name] = grades_int11print(students)String Formatting - 5 Characters Per Line
15 PointsRead a string and format it so each line contains at most 5 characters. Print each line on a new line.
String Formatting - 5 Characters Per Line
Slice the string into chunks of 5 characters and print each chunk on a new line.
Example Solution:
1s = input()2for i in range(0, len(s), 5):3 print(s[i:i+5])Reverse Words in Sentence
15 PointsRead a sentence and reverse the order of words inside it. Print the result with words separated by spaces.
Reverse Words in Sentence
Split the sentence into words, reverse the list, and join them back with spaces.
Example Solution:
1sentence = input()2words = sentence.split()3reversed_words = words[::-1]4print(" ".join(reversed_words))List of Strings to List of Lists of Words
15 PointsRead the number of strings, then read that many strings (each on a new line). Convert the list of strings to a list of lists of words, where each inner list contains the words from one string. Print the result.
List of Strings to List of Lists of Words
For each string, split it into words and add the list of words to the result.
Example Solution:
1n = int(input())2result = []3for _ in range(n):4 line = input()5 words = line.split()6 result.append(words)7print(result)Count Dictionary Values
15 PointsRead the number of items, then read that many items (strings). Create a dictionary that counts how many times each item appears. Print the dictionary.
Count Dictionary Values
Use a dictionary to count occurrences of each item.
Example Solution:
1n = int(input())2counts = {}3for _ in range(n):4 item = input()5 counts[item] = counts.get(item, 0) + 16print(counts)Filter Even Numbers from List
15 PointsRead the size of a list, followed by list elements. Create a new list containing only the even numbers from the original list. Print the new list.
Filter Even Numbers from List
Filter the list to include only even numbers.
Example Solution:
1n = int(input())2input_items = input().split()3L = []4for i in range(n):5 L.append(int(input_items[i]))6even_numbers = []7for x in L:8 if x % 2 == 0:9 even_numbers.append(x)10print(even_numbers)