1 Which built-in function returns the number of items in a list?
A size()
B count()
C len()
D length()
Answer: C. len() len() works on lists, strings, tuples, sets and dictionaries.
2 What is the output of print(type([]))?
A <class 'tuple'>
B list
C <class 'array'>
D <class 'list'>
Answer: D. <class 'list'> [] is an empty list literal, so its type is list.
3 Which of these data types is immutable?
A tuple
B list
C dict
D set
Answer: A. tuple A tuple cannot be changed after it is created. Lists, dictionaries and sets can.
4 What does 3 ** 2 evaluate to in Python?
A 6
B 5
C 9
D 1.5
Answer: C. 9 ** is the power operator, so 3 ** 2 means 3 squared.
5 Which keyword is used to define a function?
A func
B function
C def
D define
Answer: C. def Functions start with def, for example: def add(a, b):
6 What does "abc"[::-1] return?
A 'abc'
B 'cba'
C An error
D 'bca'
Answer: B. 'cba' The slice step -1 walks the string backwards, so the string is reversed.
7 What numbers does range(2, 10, 3) produce?
A 2, 5, 8
B 2, 5, 8, 10
C 2, 3, 4
D 3, 6, 9
Answer: A. 2, 5, 8 It starts at 2, adds 3 each time, and stops before reaching 10.
8 Which block catches an error so the program does not crash?
A if / else
B def / return
C try / except
D for / while
Answer: C. try / except Code that may fail goes in try. The matching except block handles the error.
9 What is the value of bool("")?
A False
B None
C An error
D True
Answer: A. False Empty strings, empty lists, 0 and None all count as False.
10 What does len({1, 2, 2, 3}) return?
A 4
B 3
C 2
D An error
Answer: B. 3 A set keeps only unique values, so {1, 2, 2, 3} is really {1, 2, 3}.
Turn on JavaScript to check answers as you go. The correct answer to each question is: 1-C, 2-D, 3-A, 4-C, 5-C, 6-B, 7-A, 8-C, 9-A, 10-B.