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.** The Python interpreter reads and executes code line by line, without a separate compilation step. This gives Python its fast development cycle and interactive REPL.Q2. What year was Python created, and who created it?
Answer
Python was created in **1991** by **Guido van Rossum**. He named it after Monty Python's Flying Circus.Q3. On Windows, which critical checkbox must you select during Python installation?
Answer
**"Add python.exe to PATH."** Without this, the terminal won't be able to find the `python` command, and you'll get a "not recognized" error.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. 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`).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 tells both the operating system and code editors (like VS Code) that the file contains Python source code. It enables syntax highlighting, code completion, and ensures the file is recognized as runnable by the Python interpreter.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.