TetSharing

Python final quiz

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?

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?

# starts a comment, which Python skips entirely. (Chapter 2: Syntax and comments)

3. What does x, y = y, x do?

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?

The / operator always produces a float. (Chapter 4: Numbers and math)

5. What does "a-b-c".split("-") return?

split returns a list of the pieces between separators. (Chapter 5: Strings)

6. Which f-string prints 1,500,000?

The comma option inserts thousands separators. (Chapter 6: f-strings and formatting)

7. What does 3 < 5 < 4 evaluate to?

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"?

The condition is false, so the expression after else is used. (Chapter 8: if, elif, else)

9. What does nums.sort() return?

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?

Tuples are immutable, so item assignment raises TypeError. (Chapter 10: Tuples)

11. What is len({"x": 1, "x": 2})?

Keys are unique; the second "x" replaces the first value. (Chapter 11: Dictionaries)

12. What is {1, 2, 3} & {2, 3, 4}?

& keeps only values found in both sets. (Chapter 12: Sets)

13. What does enumerate(["a", "b"]) yield?

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?

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?

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?

Extra positional arguments are collected into a tuple. (Chapter 16: Default and keyword arguments)

17. What does sorted(["b", "A", "c"]) return?

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?

The trailing if keeps only words longer than one character. (Chapter 18: List comprehensions)

19. Which exception does int("4.2") raise?

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?

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:?

The context manager closes the file even if an exception occurs. (Chapter 21: Reading and writing files)

22. When does __init__ run?

Creating an instance, as in BankAccount("Ava"), calls __init__. (Chapter 22: Classes and objects)

← Back to the Python tutorial