One question from each of the 22 chapters. Pick an answer for each, then press Check answers . Each explanation links back to the chapter it comes from, so you know exactly what to reread.
22 questions 1. Which statement about how Python runs a .py file is true? It starts at a function named main. It executes statements from the top of the file downward. It must be compiled by hand first. It runs the last line first. The interpreter starts at line one and works downward; no main function is required. (Chapter 1: Get started )
2. What happens to text after # on a line? It is printed. It becomes a string. Python ignores it. It causes a SyntaxError. # starts a comment, which Python skips entirely. (Chapter 2: Syntax and comments )
3. What does x, y = y, x do? Raises an error Sets both names to the same value Swaps the values of x and y Creates a tuple named x The right side is evaluated first into a pair, then unpacked into x and y in that order. (Chapter 3: Variables )
4. What does 10 / 5 return in Python 3? 2 2.0 "2" An error The / operator always produces a float. (Chapter 4: Numbers and math )
5. What does "a-b-c".split("-") return? "abc" ['a', 'b', 'c'] ('a', 'b', 'c') ['a-b-c'] split returns a list of the pieces between separators. (Chapter 5: Strings )
6. Which f-string prints 1,500,000? f"{1500000:.0}" f"{1500000:,}" f"{1500000:%}" f"{1500000:>,}" The comma option inserts thousands separators. (Chapter 6: f-strings and formatting )
7. What does 3 < 5 < 4 evaluate to? True False An error 4 It means 3 < 5 and 5 < 4; the second comparison is false. (Chapter 7: Booleans and logic )
8. What is "big" if 3 > 5 else "small"? "big" "small" True An error The condition is false, so the expression after else is used. (Chapter 8: if, elif, else )
9. What does nums.sort() return? The sorted list A copy of the list None The number of items sort() works in place and returns None; sorted() returns a new list. (Chapter 9: Lists )
10. What happens with t = (1, 2) then t[0] = 9? t becomes (9, 2) TypeError, tuples cannot be changed t becomes a list Nothing Tuples are immutable, so item assignment raises TypeError. (Chapter 10: Tuples )
11. What is len({"x": 1, "x": 2})? 1 2 3 An error Keys are unique; the second "x" replaces the first value. (Chapter 11: Dictionaries )
12. What is {1, 2, 3} & {2, 3, 4}? {1, 2, 3, 4} {2, 3} {1, 4} {1} & keeps only values found in both sets. (Chapter 12: Sets )
13. What does enumerate(["a", "b"]) yield? "a", "b" (0, "a"), (1, "b") (1, "a"), (2, "b") 0, 1 enumerate yields (index, item) pairs, starting at 0 unless you pass start. (Chapter 13: for loops and range() )
14. When does the else block of a loop run? Always Only when the loop body never runs When the loop ends without a break Only after a break Loop else is skipped if break ends the loop. (Chapter 14: while, break, continue )
15. In def area(w, h):, what are w and h? Parameters Arguments Return values Global variables Names in the definition are parameters; the values passed in a call are arguments. (Chapter 15: Functions )
16. Inside def g(*args):, what type is args? list tuple dict set Extra positional arguments are collected into a tuple. (Chapter 16: Default and keyword arguments )
17. What does sorted(["b", "A", "c"]) return? ['A', 'b', 'c'] ['b', 'A', 'c'] ['A', 'B', 'C'] ['c', 'b', 'A'] Uppercase letters sort before lowercase, and A is the only uppercase one. (Chapter 17: Lambda and sorting keys )
18. What does [w for w in ["a", "bb", "ccc"] if len(w) > 1] return? ["a"] ["bb", "ccc"] [2, 3] [False, True, True] The trailing if keeps only words longer than one character. (Chapter 18: List comprehensions )
19. Which exception does int("4.2") raise? TypeError ValueError SyntaxError ZeroDivisionError The argument is a string (the right type) but not a valid integer literal. (Chapter 19: Errors and exceptions )
20. What does from collections import Counter let you write? Counter("abc") directly Only collections.Counter("abc") collections() Nothing until you call load() from ... import brings the listed name straight into your file. (Chapter 20: Modules and imports )
21. What is the main benefit of with open(...) as f:? It reads faster. The file is closed automatically when the block ends. It prevents all errors. It creates a backup. The context manager closes the file even if an exception occurs. (Chapter 21: Reading and writing files )
22. When does __init__ run? When the class is defined When a new instance is created When the program ends Only when you call it by name Creating an instance, as in BankAccount("Ava"), calls __init__. (Chapter 22: Classes and objects )
Check answers