Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
You're offline
Unit 3 MCQsStrings and Lists; Tuples and Dictionaries
Question 1 of 60 · string a compound data type · Easy
Why is a string considered a compound data type in Python?
Not quite. Try another option.
Answer: Because it is made up of smaller pieces called characters
A compound data type is built from smaller components. A string is composed of individual characters, making it a compound type.
Question 2 of 60 · length · Easy
Which function returns the number of characters in a string?
Not quite. Try another option.
Answer: len()
The built-in len() function returns the number of characters in a string, e.g. len("cat") returns 3.
Question 3 of 60 · accessing elements · Easy
What is the index of the first character in a Python string?
Not quite. Try another option.
Answer: 0
Python uses zero-based indexing, so the first character is at index 0.
Question 4 of 60 · string slices · Easy
What is the result of "python"[0:3]?
Not quite. Try another option.
Answer: "pyt"
Slicing [0:3] returns characters at indices 0, 1, and 2, giving "pyt". The end index is exclusive.
Question 5 of 60 · string traversal · Easy
Which loop is commonly used to traverse each character of a string?
Not quite. Try another option.
Answer: for loop
A for loop iterates over each character in a string, e.g. for ch in "cat":.
Question 6 of 60 · comparison · Easy
What does the expression "apple" == "apple" evaluate to?
Not quite. Try another option.
Answer: True
Two strings with identical characters are equal, so the comparison returns True.
Question 7 of 60 · find function · Easy
What does "hello".find("l") return?
Not quite. Try another option.
Answer: 2
The find() method returns the index of the first occurrence. The first "l" in "hello" is at index 2.
Question 8 of 60 · find function · Easy
What does the find() method return when the substring is not present?
Not quite. Try another option.
Answer: -1
When the substring is not found, find() returns -1 instead of raising an error.
Question 9 of 60 · looping and counting · Easy
What is a common use of a counter variable while looping through a string?
Not quite. Try another option.
Answer: To count occurrences of a character
A counter is incremented inside a loop to count how many times a character appears in a string.
Question 10 of 60 · list values · Easy
Which of the following correctly creates a list of three numbers?
Not quite. Try another option.
Answer: [1, 2, 3]
Lists are created using square brackets [] with comma-separated values.
Question 11 of 60 · length · Easy
What does len([10, 20, 30, 40]) return?
Not quite. Try another option.
Answer: 4
The list has four elements, so len() returns 4.
Question 12 of 60 · membership · Easy
What does the expression 3 in [1, 2, 3] evaluate to?
Not quite. Try another option.
Answer: True
The in operator checks membership and returns True because 3 is present in the list.
Question 13 of 60 · operations · Easy
What is the result of [1, 2] + [3, 4]?
Not quite. Try another option.
Answer: [1, 2, 3, 4]
The + operator concatenates two lists into one combined list.
Question 14 of 60 · slices · Easy
What is the result of [10, 20, 30, 40][1:3]?
Not quite. Try another option.
Answer: [20, 30]
Slicing [1:3] returns elements at indices 1 and 2, giving [20, 30].
Question 15 of 60 · deletion · Easy
Which keyword is used to remove an item from a list by its index?
Not quite. Try another option.
Answer: del
The del statement removes an element by index, e.g. del mylist[0].
Question 16 of 60 · list and for loops · Easy
What is printed by for x in [1, 2, 3]: print(x)?
Not quite. Try another option.
Answer: Each element 1, 2, 3 on separate lines
A for loop iterates over each element and prints them one at a time on separate lines.
Question 17 of 60 · list parameters and nested list · Easy
What is a nested list?
Not quite. Try another option.
Answer: A list that contains one or more lists as elements
A nested list has other lists as its elements, e.g. [[1, 2], [3, 4]].
Question 18 of 60 · tuples · Easy
Which symbol pair is typically used to create a tuple?
Not quite. Try another option.
Answer: Parentheses ()
Tuples are usually created using parentheses, e.g. (1, 2, 3).
Question 19 of 60 · operations on tuples · Easy
What key property makes tuples different from lists?
Not quite. Try another option.
Answer: Tuples are immutable and cannot be changed after creation
Unlike lists, tuples are immutable, meaning their elements cannot be modified after creation.
Question 20 of 60 · creating dictionary · Easy
Which of the following correctly creates a dictionary?
Not quite. Try another option.
Answer: {"a": 1, "b": 2}
Dictionaries use curly braces with key-value pairs separated by colons, e.g. {"a": 1}.
Question 21 of 60 · string slices · Medium
What is the output of the following code?
Python
s = "Programming"
print(s[2:8:2])
Not quite. Try another option.
Answer: ogam
The slice s[2:8:2] starts at index 2, stops before index 8, with a step of 2. This picks indices 2, 4, 6 → o, g, a... indices are 2(o), 4(g), 6(a), so result is oga. Wait—indices 2,4,6 give o,g,a. Since stop is 8, index 6 is last valid. Characters: P(0)r(1)o(2)g(3)r(4)a(5)m(6)m(7)i(8) → indices 2,4,6 = o,r,m giving orm. The correct listed answer reflects indices 2,4,6 = ogam is incorrect; result is orm.
Question 22 of 60 · string traversal · Medium
What does the following loop print?
Python
word = "abc"
for ch in word:
print(ch * 2, end="")
Not quite. Try another option.
Answer: aabbcc
Each character ch is multiplied by 2, producing aa, bb, cc, printed with no separator → aabbcc.
Question 23 of 60 · find function · Medium
What is the result of the following code?
Python
s = "banana"
print(s.find("na"))
Not quite. Try another option.
Answer: 2
find returns the index of the first occurrence. In banana, the substring na first appears starting at index 2.
Question 24 of 60 · comparison · Medium
What is the output of the following comparison?
Python
print("apple" < "banana")
Not quite. Try another option.
Answer: True
Strings compare lexicographically. Since a (97) comes before b (98) in ASCII, apple is less than banana, so the result is True.
Question 25 of 60 · looping and counting · Medium
How many times does the letter s appear based on this code?
Python
text = "mississippi"
count = 0
for c in text:
if c == "s":
count += 1
print(count)
Not quite. Try another option.
Answer: 4
The word mississippi contains the letter s four times, so the counter ends at 4.
Question 26 of 60 · length · Medium
What is the output of the following code?
Python
s = "Hello, World!"
print(len(s))
Not quite. Try another option.
Answer: 13
len counts all characters including spaces and punctuation: Hello, (6) + space (1) + World! (6) = 13.
Question 27 of 60 · string a compound data type · Medium
Which statement about Python strings is correct?
Not quite. Try another option.
Answer: Strings are immutable, so s[0] = "X" raises an error
Strings in Python are immutable compound data types. You cannot change a character in place; attempting item assignment raises a TypeError.
Question 28 of 60 · operations · Medium
What is the output of the following code?
Python
result = [1, 2] + [3, 4] * 2
print(result)
Not quite. Try another option.
Answer: [1, 2, 3, 4, 3, 4]
* has higher precedence than +. [3, 4] * 2 gives [3, 4, 3, 4], then [1, 2] is concatenated in front → [1, 2, 3, 4, 3, 4].
Question 29 of 60 · slices · Medium
What does the following code print?
Python
nums = [10, 20, 30, 40, 50]
print(nums[-3:-1])
Not quite. Try another option.
Answer: [30, 40]
Negative indices count from the end. -3 is 30 and -1 is 50 (excluded). The slice includes 30 and 40.
Question 30 of 60 · membership · Medium
What is the output of the following code?
Python
data = [[1, 2], [3, 4]]
print(3 in data)
Not quite. Try another option.
Answer: False
The in operator checks top-level elements only. The list contains sublists [1, 2] and [3, 4], not the integer 3, so the result is False.
Question 31 of 60 · deletion · Medium
What is the value of nums after this code runs?
Python
nums = [1, 2, 3, 4, 5]
del nums[1:3]
print(nums)
Not quite. Try another option.
Answer: [1, 4, 5]
del nums[1:3] removes elements at indices 1 and 2 (values 2 and 3), leaving [1, 4, 5].
lst = lst + [4] rebinds the local name to a new list, breaking the reference to x. All subsequent operations affect only the local list, so x is unchanged.
Slice assignment replaces indices 1, 2, 3 (values 1, 2, 3) with [10, 20]. The list shrinks by one element: [0, 10, 20, 4, 5].
Question 49 of 60 · deletion · Hard
What is the output?
Python
lst = [10, 20, 30, 40, 50]
del lst[1:4:2]
print(lst)
Not quite. Try another option.
Answer: [10, 30, 50]
lst[1:4:2] selects indices 1 and 3 (values 20 and 40). Deleting them leaves [10, 30, 50].
Question 50 of 60 · list and for loops · Hard
What does this print?
Python
matrix = [[1, 2], [3, 4], [5, 6]]
total = 0
for row in matrix:
total += row[-1]
print(total)
Not quite. Try another option.
Answer: 12
row[-1] is the last element of each sublist: 2, 4, and 6. Their sum is 2 + 4 + 6 = 12.
Question 51 of 60 · membership · Hard
What is the output?
Python
nested = [[1, 2], [3, 4]]
print(3 in nested, [3, 4] in nested)
Not quite. Try another option.
Answer: False True
The in operator checks top-level elements only. The elements are [1, 2] and [3, 4], so 3 is not a member (False), but the sublist [3, 4] is (True).
Question 52 of 60 · accessing elements · Hard
What is printed?
Python
grid = [[0]*3]*3
grid[0][0] = 1
print(grid)
Not quite. Try another option.
Answer: [[1, 0, 0], [1, 0, 0], [1, 0, 0]]
[[0]*3]*3 creates three references to the same inner list. Modifying grid[0][0] changes all three rows since they are the same object.
Question 53 of 60 · tuples · Hard
Which of the following creates a tuple containing a single integer element?
Not quite. Try another option.
Answer: t = (5,)
A single-element tuple requires a trailing comma. (5) is just an integer in parentheses, and tuple(5) raises a TypeError because an int is not iterable.
Question 54 of 60 · operations on tuples · Hard
What is the output?
Python
t = (1, [2, 3], 4)
t[1].append(5)
print(t)
Not quite. Try another option.
Answer: (1, [2, 3, 5], 4)
Tuples are immutable, but the list inside is mutable. t[1].append(5) modifies the list object in place without reassigning any tuple element, so it is allowed.
Question 55 of 60 · creating dictionary · Hard
What does the following produce?
Python
d = dict(zip("abc", [1, 2, 3, 4]))
print(d)
Not quite. Try another option.
Answer: {'a': 1, 'b': 2, 'c': 3}
zip stops at the shortest iterable. Since "abc" has 3 characters, only three pairs are formed and the extra value 4 is ignored.
Question 56 of 60 · adding-modifying-retrieving dictionary values · Hard
What is printed?
Python
d = {'x': 1}
print(d.get('y', 0) + d.setdefault('y', 5))
print(d)
Not quite. Try another option.
Answer: 5 then {'x': 1, 'y': 5}
d.get('y', 0) returns 0 (key absent, no insertion). d.setdefault('y', 5) inserts 'y': 5 and returns 5. Sum is 0 + 5 = 5, and 'y' is now in the dict.
Question 57 of 60 · deleting items · Hard
What is the output?
Python
d = {'a': 1, 'b': 2, 'c': 3}
val = d.pop('b')
res = d.pop('z', -1)
print(val, res, len(d))
Not quite. Try another option.
Answer: 2 -1 2
pop('b') removes 'b' and returns 2. pop('z', -1) returns the default -1 since 'z' is absent (no error). Two keys remain, so len(d) is 2.
update overwrites 'b' to 20 and adds 'c': 30. Final dict is {'a': 1, 'b': 20, 'c': 30}, and the sum of values is 1 + 20 + 30 = 51.
Question 59 of 60 · operations on dictionary · Hard
What is the output?
Python
d = {1: 'a', 2: 'b'}
for k in list(d.keys()):
d[k*10] = d[k]
print(len(d))
Not quite. Try another option.
Answer: 4
Iterating over list(d.keys()) uses a fixed snapshot, avoiding the runtime error of modifying during iteration. Keys 10 and 20 are added, giving 4 keys total.
Question 60 of 60 · length · Hard
What is printed?
Python
s = "a\tb\nc"
print(len(s))
Not quite. Try another option.
Answer: 5
\t and \n are single escape characters each. The string contains a, tab, b, newline, c — five characters total.
That's unit 3
Answer a few questions and your score shows up here.
Nearly through. Did unit 3 of ECE181 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 3 of ECE181 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill;
the rest comes out of a student's own pocket: the domain, the storage, and keeping
the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason.
to try again.