Unit 3: Strings and Lists; Tuples and Dictionaries - Practice Quiz

ECE181 — Introduction To Python 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Why is a string considered a compound data type in Python?

string a compound data type Easy
A. Because it can only store numbers
B. Because it can be modified in place
C. Because it always contains exactly one element that must be a letter of the alphabet from A to Z
D. Because it is made up of smaller pieces called characters

2 Which function returns the number of characters in a string?

length Easy
A. length()
B. len()
C. size()
D. count()

3 What is the index of the first character in a Python string?

accessing elements Easy
A. length
B. -1
C. 1
D. 0

4 What is the result of "python"[0:3]?

string slices Easy
A. "tho"
B. "pyt"
C. "python"
D. "pyth"

5 Which loop is commonly used to traverse each character of a string?

string traversal Easy
A. with statement
B. try block
C. for loop
D. if statement

6 What does the expression "apple" == "apple" evaluate to?

comparison Easy
A. True
B. None
C. False
D. 0

7 What does "hello".find("l") return?

find function Easy
A. 1
B. 2
C. 3
D. -1

8 What does the find() method return when the substring is not present?

find function Easy
A. -1
B. An error is raised because the searched substring could not be located anywhere in the string
C. 0
D. None

9 What is a common use of a counter variable while looping through a string?

looping and counting Easy
A. To reverse the string
B. To convert the string to uppercase
C. To delete characters from the string
D. To count occurrences of a character

10 Which of the following correctly creates a list of three numbers?

list values Easy
A. <1, 2, 3>
B. {1, 2, 3}
C. (1, 2, 3)
D. [1, 2, 3]

11 What does len([10, 20, 30, 40]) return?

length Easy
A. 0
B. 5
C. 3
D. 4

12 What does the expression 3 in [1, 2, 3] evaluate to?

membership Easy
A. False
B. True
C. None
D. 3

13 What is the result of [1, 2] + [3, 4]?

operations Easy
A. [4, 6]
B. [3, 4, 1, 2]
C. [1, 2, 3, 4]
D. [1, 2, [3, 4]]

14 What is the result of [10, 20, 30, 40][1:3]?

slices Easy
A. [10, 20]
B. [30, 40]
C. [20, 30]
D. [20, 30, 40]

15 Which keyword is used to remove an item from a list by its index?

deletion Easy
A. del
B. remove
C. erase
D. drop

16 What is printed by for x in [1, 2, 3]: print(x)?

list and for loops Easy
A. Each element 1, 2, 3 on separate lines
B. The list [1, 2, 3]
C. The length 3
D. The sum 6

17 What is a nested list?

list parameters and nested list Easy
A. A list that contains one or more lists as elements
B. A list with only numbers
C. A list stored in a file
D. A list that cannot be changed

18 Which symbol pair is typically used to create a tuple?

tuples Easy
A. Curly braces {}
B. Angle brackets <>
C. Square brackets []
D. Parentheses ()

19 What key property makes tuples different from lists?

operations on tuples Easy
A. Tuples can only hold strings
B. Tuples use square brackets
C. Tuples are immutable and cannot be changed after creation
D. Tuples cannot be indexed

20 Which of the following correctly creates a dictionary?

creating dictionary Easy
A. ("a", 1, "b", 2)
B. {"a": 1, "b": 2}
C. {"a", 1, "b", 2}
D. ["a": 1, "b": 2]

21 What is the output of the following code?

PYTHON
s = "Programming"
print(s[2:8:2])

string slices Medium
A. orm
B. oai
C. ogam
D. rgamn

22 What does the following loop print?

PYTHON
word = "abc"
for ch in word:
    print(ch * 2, end="")

string traversal Medium
A. abc
B. abcabc
C. aabbcc
D. aaabbbccc

23 What is the result of the following code?

PYTHON
s = "banana"
print(s.find("na"))

find function Medium
A. -1
B. 3
C. 4
D. 2

24 What is the output of the following comparison?

PYTHON
print("apple" < "banana")

comparison Medium
A. False
B. True
C. 0
D. Error

25 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)

looping and counting Medium
A. 5
B. 2
C. 4
D. 3

26 What is the output of the following code?

PYTHON
s = "Hello, World!"
print(len(s))

length Medium
A. 12
B. 14
C. 11
D. 13

27 Which statement about Python strings is correct?

string a compound data type Medium
A. Strings are immutable, so s[0] = "X" raises an error
B. Strings are stored as a list of integers
C. Strings are mutable and support item assignment
D. Strings can only store alphabetic characters

28 What is the output of the following code?

PYTHON
result = [1, 2] + [3, 4] * 2
print(result)

operations Medium
A. [4, 8, 3, 4]
B. [1, 2, 6, 8]
C. [1, 2, 3, 4, 3, 4]
D. [1, 2, 3, 4, 1, 2, 3, 4]

29 What does the following code print?

PYTHON
nums = [10, 20, 30, 40, 50]
print(nums[-3:-1])

slices Medium
A. [40, 50]
B. [30, 40, 50]
C. [20, 30]
D. [30, 40]

30 What is the output of the following code?

PYTHON
data = [[1, 2], [3, 4]]
print(3 in data)

membership Medium
A. True
B. Error
C. False
D. [3, 4]

31 What is the value of nums after this code runs?

PYTHON
nums = [1, 2, 3, 4, 5]
del nums[1:3]
print(nums)

deletion Medium
A. [1, 2, 5]
B. [1, 4, 5]
C. [2, 3, 4, 5]
D. [1, 3, 4, 5]

32 What does the following code print?

PYTHON
matrix = [[1, 2, 3], [4, 5, 6]]
print(matrix[1][2])

accessing elements Medium
A. 6
B. 4
C. 5
D. 3

33 What is the output of the following code?

PYTHON
nums = [1, 2, 3, 4]
total = 0
for n in nums:
    total += n * n
print(total)

list and for loops Medium
A. 20
B. 30
C. 10
D. 100

34 What does the following code print?

PYTHON
def modify(lst):
    lst.append(99)

nums = [1, 2, 3]
modify(nums)
print(nums)

list parameters and nested list Medium
A. [99]
B. [1, 2, 3, 99]
C. [99, 1, 2, 3]
D. [1, 2, 3]

35 What happens when the following code is executed?

PYTHON
t = (1, 2, 3)
t[0] = 10
print(t)

tuples Medium
A. Prints (10, 2, 3)
B. Prints (1, 2, 3)
C. Raises a TypeError
D. Prints [10, 2, 3]

36 What is the output of the following code?

PYTHON
t = (1, 2) * 3
print(len(t))

operations on tuples Medium
A. 5
B. 6
C. 3
D. 2

37 What is the output of the following code?

PYTHON
d = dict([("a", 1), ("b", 2)])
print(d["b"])

creating dictionary Medium
A. ("b", 2)
B. b
C. 1
D. 2

38 What does the following code print?

PYTHON
d = {"x": 1}
d["y"] = 2
d["x"] = 5
print(d)

adding-modifying-retrieving dictionary values Medium
A. {'x': 5}
B. {'x': 1, 'y': 2}
C. {'x': 1, 'y': 2, 'x': 5}
D. {'x': 5, 'y': 2}

39 What is the output of the following code?

PYTHON
d = {"a": 1, "b": 2, "c": 3}
del d["b"]
print(list(d.keys()))

deleting items Medium
A. ['b']
B. ['a', 'b', 'c']
C. ['a', 'c']
D. ['a', 'c', 'b']

40 What is the output of the following code?

PYTHON
d = {"a": 1, "b": 2}
print(d.get("c", 0))

dictionary methods Medium
A. c
B. Error
C. 0
D. None

41 What is the output of the following code?

PYTHON
s = "programming"
print(s[::-2])

string slices Hard
A. gimargo
B. pormig
C. gnimargorp
D. gimrgm

42 Which expression evaluates to True in Python 3?

comparison Hard
A. "ABC" > "abc"
B. "apple" < "Apple"
C. "Apple" < "apple"
D. "Z" < "A"

43 What does the following print?

PYTHON
s = "mississippi"
print(s.find("ss", 3))

find function Hard
A. 2
B. 4
C. -1
D. 5

44 What is the value of count after this loop?

PYTHON
s = "aAbBcCaA"
count = 0
for ch in s:
    if ch.lower() == 'a':
        count += 1
print(count)

looping and counting Hard
A. 2
B. 4
C. 8
D. 3

45 What is the output?

PYTHON
s = "abcde"
result = ""
for i in range(len(s)):
    result += s[i] * i
print(result)

string traversal Hard
A. aabbbcccc
B. abbcccdddd
C. abcde
D. bccdddeeee

46 What is printed?

PYTHON
a = [1, 2, 3]
b = a * 2
b[0] = 99
print(a[0], b[0])

operations Hard
A. 1 99
B. 1 1
C. 99 99
D. 99 1

47 What is the output?

PYTHON
def modify(lst):
    lst = lst + [4]
    lst.append(5)
x = [1, 2, 3]
modify(x)
print(x)

list parameters and nested list Hard
A. [1, 2, 3, 5]
B. [1, 2, 3]
C. [1, 2, 3, 4, 5]
D. [1, 2, 3, 4]

48 What is the result?

PYTHON
lst = [0, 1, 2, 3, 4, 5]
lst[1:4] = [10, 20]
print(lst)

slices Hard
A. [10, 20, 3, 4, 5]
B. [0, 10, 20, 2, 3, 4, 5]
C. [0, 10, 20, 3, 4, 5]
D. [0, 10, 20, 4, 5]

49 What is the output?

PYTHON
lst = [10, 20, 30, 40, 50]
del lst[1:4:2]
print(lst)

deletion Hard
A. [10, 30, 40, 50]
B. [20, 40]
C. [10, 40, 50]
D. [10, 30, 50]

50 What does this print?

PYTHON
matrix = [[1, 2], [3, 4], [5, 6]]
total = 0
for row in matrix:
    total += row[-1]
print(total)

list and for loops Hard
A. 21
B. 9
C. 12
D. 6

51 What is the output?

PYTHON
nested = [[1, 2], [3, 4]]
print(3 in nested, [3, 4] in nested)

membership Hard
A. False True
B. False False
C. True False
D. True True

52 What is printed?

PYTHON
grid = [[0]*3]*3
grid[0][0] = 1
print(grid)

accessing elements Hard
A. [[1, 0, 0], [0, 0, 0], [1, 0, 0]]
B. [[1, 1, 1], [0, 0, 0], [0, 0, 0]]
C. [[1, 0, 0], [1, 0, 0], [1, 0, 0]]
D. [[1, 0, 0], [0, 0, 0], [0, 0, 0]]

53 Which of the following creates a tuple containing a single integer element?

tuples Hard
A. t = (5)
B. t = tuple(5)
C. t = (5,)
D. t = 5

54 What is the output?

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

operations on tuples Hard
A. (1, [2, 3], 4)
B. (1, [2, 3], 4, 5)
C. TypeError raised
D. (1, [2, 3, 5], 4)

55 What does the following produce?

PYTHON
d = dict(zip("abc", [1, 2, 3, 4]))
print(d)

creating dictionary Hard
A. {'a': 1, 'b': 2, 'c': 3, 'd': 4}
B. ValueError raised
C. {'a': 1, 'b': 2, 'c': 3}
D. {'a': 1, 'b': 2, 'c': 3, None: 4}

56 What is printed?

PYTHON
d = {'x': 1}
print(d.get('y', 0) + d.setdefault('y', 5))
print(d)

adding-modifying-retrieving dictionary values Hard
A. 0 then {'x': 1}
B. 5 then {'x': 1, 'y': 5}
C. 10 then {'x': 1, 'y': 5}
D. 5 then {'x': 1}

57 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))

deleting items Hard
A. 2 -1 3
B. None -1 2
C. 2 -1 2
D. 2 None 2

58 What does this print?

PYTHON
d = {'a': 1, 'b': 2}
d.update({'b': 20, 'c': 30})
print(sum(d.values()))

dictionary methods Hard
A. 23
B. 33
C. 51
D. 53

59 What is the output?

PYTHON
d = {1: 'a', 2: 'b'}
for k in list(d.keys()):
    d[k*10] = d[k]
print(len(d))

operations on dictionary Hard
A. RuntimeError raised
B. 4
C. 2
D. 6

60 What is printed?

PYTHON
s = "a\tb\nc"
print(len(s))

length Hard
A. 3
B. 5
C. 6
D. 7