Python Essentials 2 Module 2 Completion – Module Test Answers
Python Essentials 2: PE2: Module 2. Strings, String and List Methods, Exceptions test Answers full new questions
1. Entering the try: block implies that:
- none of the instructions from this block will be executed
- the block will be omitted
- all of the instructions from this block will be executed
- some of the instructions from this block may not be executed
Remember that the instructions inside a try block are executed sequentially. If an instruction generates an exception, the execution jumps to the except statements. Therefore, the remaining instructions within the try block are not executed.
2. The unnamed except: block:
- must be the first one
- can be placed anywhere
- must be the last one
- cannot be used if any named block has been used
The excepts within a try expect block should be listed from the specific exceptions to the general exceptions. This assures that in case of an error, the error will fall under the suitable exception. The unnamed exception must be the last exception listed because it is the most general exception.
3. The top‑most Python exception is called:
- Exception
- PythonException
- TopException
- BaseException
Remember that the BaseException class is, as the name suggests, the base class for all built-in exceptions in Python.
4. The following statement:
assert var == 0
- has no effect
- will stop the program when var != 0
- will stop the program when var == 0
- is erroneous
Remember that the assert keyword tests if a condition is true. If it is not, the program will raise an AssertionError.
5. What is the expected output of the following code?
try: print("5"/0) except ArithmeticError: print("arith") except ZeroDivisionError: print("zero") except: print("some")
- zero
- arith
- 0
- some
Let’s analyze this code snippet:
- The print function inside the try block is executed.
- If you look closely, “5” is a string, and it is divided by an integer zero.
- No ArithmeticError nor ZeroDivisionError is raised.
- Instead, a TypeError exception is raised, which will fall under the nameless except.
- The word some is printed in the console.
6. Which of the following are examples of built-in concrete Python exceptions? (Select two answers)
- IndexError
- ArithmeticError
- BaseException
- ImportError
7. ASCII is:
- short for American Standard Code for Information Interchange
- a standard Python module name
- a predefined Python variable name
- a character name
8. UTF‑8 is:
- a synonym for byte
- the 9th version of the UTF standard
- a form of encoding Unicode code points
- a Python version name
9. UNICODE is a standard:
- like ASCII, but much more expansive
- used by coders from universities
- honored by the whole universe
- for coding floating-point numbers
10. The following code:
x = '\'' print(len(x)) prints:
- 3
- 1
- 20
- 2
Let’s analyze this code snippet:
- A string variable named x is defined.
- The first and last single quotes delimit the contents within the variable, and are not part of the content.
- The backslash is the escape character, which also does not count as part of the content.
- The single quote after the backslash is the only character that is part of the variable contents.
- Using the print function, the character length of the variable is shown in the console, and the answer is 1.
11. The following code:
print(ord('c') - ord('a')) prints:
- 0
- 3
- 1
- 2
12. The following code:
print(chr(ord('z') ‑ 2)) prints:
- a
- y
- z
- x
13. The following code:
print(3 * 'abc' + 'xyz') prints:
- abcabcxyzxyz
- abcabcabcxyz
- xyzxyzxyzxyz
- abcxyzxyzxyz
The * and + operators replicate and concatenate when used with strings. The first operation 3 * ‘abc’ returns a string that contains abcabcabc. Then the second operation + ‘xyz’ concatenates xyz to the previous string. The resulting string abcabcabcxyz is printed in the console.
14. The following code:
print('Mike' > "Mikey") prints:
- True
- 1
- 0
- False
The > operator, when used with strings, compares character for character. This means characters in the same positions are compared from both the strings. Since Mike is shorter than Mikey, it cannot be greater. Therefore, the answer is False.
15. The following code:
print(float("1, 3"))
- prints 1.3
- prints 13
- prints 1,3
- raises a ValueError exception
The float function tries to convert the 1, 3 string into a floating-point value. Since it contains a comma and a whitespace, a ValueError exception is raised because it cannot be converted.