Chapter 2 Quiz
Test your understanding of Chapter 2 concepts. Try to answer each question without looking back at the chapter. After answering, expand the solution to check your work.
Q1. Python is best described as: - (a) A compiled language - (b) An interpreted language - (c) A markup language - (d) A database language
Answer
**(b) An interpreted language.** You run your source file directly with the Python interpreter. It translates the code to bytecode behind the scenes and executes it, but there's no separate compile step for you to run and no executable file to manage. This gives Python its fast development cycle and interactive REPL.Q2. Who created Python, and in what year was it first released?
Answer
Python was created by **Guido van Rossum** and first released in **1991** (he began working on it in December 1989). He named it after Monty Python's Flying Circus.Q3. If you install Python on Windows with the classic python.org installer, which checkbox on the first screen must you tick, and why?
Answer
**"Add python.exe to PATH."** The PATH is the list of folders the terminal searches for commands. Without this box ticked, the terminal can't find the `python` command, and you'll get a "not recognized" error. (The newer Python install manager asks a similar question about PATH; say yes there too.)Q4. What is the REPL, and what does the acronym stand for?
Answer
The REPL is Python's interactive mode. It stands for **Read-Eval-Print Loop**: Python *reads* your input, *evaluates* (executes) it, *prints* the result, and *loops* back for more input. The prompt is `>>>`.Q5. What will the following code output?
print("hello", "world", sep="***")
Answer
hello***world
The `sep` parameter changes the separator between `print()` arguments from the default space to `***`.
Q6. What will the following code output?
print("A", end="")
print("B", end="")
print("C")
Answer
ABC
The `end=""` parameter prevents `print()` from adding a newline, so all three letters appear on the same line.
Q7. What type of data does input() always return?
Answer
`input()` **always returns a string** (`str`), even if the user types a number. If the user types `42`, you get the string `"42"`, not the integer `42`.Q8. Identify the error in this code and name the error type:
print("Hello, World!)
Answer
**SyntaxError: unterminated string literal.** The closing double quote before the closing parenthesis is missing, so Python reads the rest of the line as part of the string. Because it's a syntax error, nothing in the file runs until it's fixed. The fix is `print("Hello, World!")`.Q9. Identify the error in this code and name the error type:
greeting = "Hello"
print(greting)
Answer
**NameError: name 'greting' is not defined.** The variable name is misspelled — it was defined as `greeting` but referenced as `greting` (missing an `e`). Recent versions of Python (3.10 and later) even add a hint to the end of the message: `Did you mean: 'greeting'?`Q10. What is the difference between a terminal and a shell?
Answer
The **terminal** is the window/application you type into — the visual interface. The **shell** is the program running inside the terminal that interprets your commands (e.g., `bash`, `zsh`, `cmd.exe`, `PowerShell`). The terminal is the container; the shell is the command processor inside it.Q11. What command moves you up one directory level in the terminal?
Answer
cd ..
The `..` represents the parent directory.
Q12. What is the output of print("=" * 5)?
Answer
=====
The `*` operator repeats the string `"="` five times.
Q13. What does this code do?
# print("Hello")
Answer
**Nothing.** The entire line is a comment (it starts with `#`), so Python ignores it. The `print()` call never executes.Q14. True or False: In Python, print('Hello') and print("Hello") produce different output.
Answer
**False.** Single quotes and double quotes are interchangeable in Python for defining strings. Both produce the output `Hello`.Q15. When reading a Python error message, which line should you read first, and why?
Answer
Read the **last line first** — it contains the error type (e.g., `SyntaxError`, `NameError`) and a human-readable description of what went wrong. Then look at the line number to find *where* in your code the error occurred.Q16. What is the purpose of the .py file extension?
Answer
The `.py` extension labels the file as Python source code. Editors like VS Code use it to switch on Python syntax highlighting, code completion, and the Run button, and your operating system uses it to decide which program opens the file. (Strictly speaking, `python` will try to run a file with any name you give it — but the `.py` convention is what makes the tools around Python work, and in Chapter 12 you'll see that Python's `import` statement looks for modules in files ending in `.py`.)Q17. What will this code output?
name = input("Name: ")
print(f"Hi, {name}!")
Assume the user types Sam.
Answer
Name: Sam
Hi, Sam!
The `input()` function displays the prompt `Name: ` and waits for user input. The f-string then inserts the value of `name` into the output.
Q18. Which of the following is a good comment, and why?
# Option A
x = 9.81 # set x to 9.81
# Option B
x = 9.81 # gravitational acceleration in m/s^2
Answer
**Option B** is the good comment. It explains *why* the value is 9.81 — it represents gravitational acceleration. Option A merely restates what the code already says, adding no new information for the reader.Q19. What happens if you run a Python script without saving your latest changes first?
Answer
Python runs the **last saved version** of the file, not the version currently in your editor. Your latest changes won't be reflected in the output. This is a common source of confusion — always save (Ctrl+S) before running.Q20. Name two things you can do in the REPL that you can't easily do by running a script file.