Python Essentials 1 – Module 2 Test Answers

Python Essentials 1 – Module 2 Test Answers

Python Essentials 1 Module 2 Completion – Module Test Quiz Answers

PE1: Module 2. Python Data Types, Variables, Operators, and Basic I/O Operations

1. The \n digraph forces the print() function to:

  • duplicate the character next to the digraph
  • break the output line
  • stop its execution
  • output exactly two characters: \ and n
Explanation: The digraph, called the newline character, causes the current line to end at the point indicated by the digraph, and creates a new line that starts right after it.

2. The meaning of the keyword parameter is determined by:

  • its position within the argument list
  • its connection with existing variables
  • the argument’s name specified along with its value
  • its value
Explanation: Keyword parameters (also called named parameters) are parameters that have values determined by a keyword name followed by an equals sign (=) and a default value assigned to that keyword name. An example of a keyword argument: def my_function(x=1):.

3. The value twenty point twelve times ten raised to the power of eight should be written as:

  • 20.12E8
  • 20.12*10^8
  • 20.12E8.0
  • 20E12.8
Explanation: Keeping in mind that Python chooses the most efficient format for presenting numbers, and the letter E is used to mean ten to the power of in scientific notation, the correct way of writing the number 20.12 × 108 is 20.12E8.

4. The 0o prefix means that the number after it is denoted as:

  • decimal
  • octal
  • binary
  • hexadecimal
Explanation: If an integer number is preceded by 0o or 0O, it will be treated as an octal value. For example: 0o246 is an octal number with a decimal value equal to 166.

If an integer number is preceded by 0x or 0X, it will be treated as a hexadecimal value. For example: 0x246 is a hexadecimal number with a decimal value equal to 582.

Finally, if an integer number is preceded by 0b or 0B, it will be treated as a binary value. For example: 0b1111 is a binary number with a decimal value equal to 15.

5. The ** operator:

  • does not exist
  • performs exponentiation
  • performs duplicated multiplication
  • performs floating-point multiplication
Explanation: The ** operator performs the exponent arithmetic in Python. It is also called the power operator.

6. The result of the following division:

1 / 1
  • cannot be predicted
  • cannot be evaluated
  • is equal to 1.0
  • is equal to 1
Explanation: The / operator is one of the two types of division operator in Python that divides its left operand by its right operand, and returns a floating-point value.

The // operator, called the floor division operator, performs a similar operation, but rounds down the result and returns an integer number.

7. Which of the following statements are true? (Select two answers)

  • The right argument of the % operator cannot be zero.
  • The result of the / operator is always an integer value.
  • The ** operator uses right-sided binding.
  • Addition precedes multiplication.
Explanation: The % operator (modulus) returns the remainder of a division, and because you cannot divide by zero, the right operand must be a non-zero number. Otherwise, a ZeroDivisionException will be raised.

The ** operator uses right-sided binding, which means the expression 2**2**3 is evaluated from right to left: 2**3 = 8, and 2**8 = 256.

8. Left-sided binding determines that the result of the following expression:

1 // 2 * 3

is equal to:

  • 0.0
  • 0
  • 0.16666666666666666
  • 4.5
Explanation: Left-sided binding means that the expression is evaluated from left to right: 1 // 2 = 0, and 0 * 3 = 0.

9. Which of the following variable names are illegal? (Select two answers)

  • and
  • true
  • True
  • TRUE
Explanation: True and and are Python keywords (reserved words), and they cannot be used as variable names. And since Python is case-sensitive, the names true and TRUE are perfectly legal, though not necessarily the best names.

10. The print() function can output values of:

  • any number of arguments (excluding zero)
  • not more than five arguments
  • any number of arguments (including zero)
  • just one argument
Explanation: The print() function can take no arguments at all (e.g. print()), three arguments (e.g. print("one", "two", "three"), or three thousand three hundred thirty three… (though we haven’t actually checked it!).

11. What is the output of the following snippet?

x = 1
y = 2
z = x
x = y
y = z
print(x, y)
  • 2 1
  • 1 1
  • 1 2
  • 2 2
Explanation: Let’s analyze this example:

– the value 1 is assigned to variable x, and variable x is initiated (so x = 1)
– the value 2 is assigned to variable y, and variable y is initiated (so y = 2)
– the value assigned to variable x is assigned to variable z, and variable z is initiated (so z = 1)
– the x variable is assigned the value which is assigned to variable y (so x = 2)
– the y variable is assigned the value which is assigned to variable z (so y = 1)
– the values ssigned to variables x and y are now printed, giving the following output: 2 1 (note: the print() function separates the outputted values with a whitespace)

12. What is the output of the following snippet if the user enters two lines containing 2 and 4 respectively?

x = input()
y = input()
print(x + y)
  • 4
  • 24
  • 2
  • 6
Explanation: Let’s analyze this example:

the input() function reads the arguments entered by the user (2 and 4 respectively) and converts them to strings,
variables x and y are assigned the strings inputted by the user,
the print() function outputs the result of the concatenation operation to the screen (the process of adding/merging strings): “2” + “4”; the + operator adds a string to another string, and outputs 24.

13. What is the output of the following snippet if the user enters two lines containing 2 and 4 respectively?

x = int(input())
y = int(input())
 
x = x // y
y = y // x
 
print(y)
  • 8.0
  • the code will cause a runtime error
  • 2.0
  • 4.0
Explanation: Let’s analyze this example:
– the x variable is assigned the integer value of 2 (2 is inputted by the user, converted to a string by the input() function, and then converted to an integer by the int() function)
– the y variable is assigned the integer value of 4 (4 is inputted by the user, converted to a string by the input() function, and then converted to an integer by the int() function)
– an operation is performed resulting in the x variable being assigned the value of 0 (2 // 4=0)
– an operation is being performed, but a ZeroDivisionException is raised, because the // operator cannot accept 0 as its right operand. The program is terminated.

14. What is the output of the following snippet if the user enters two lines containing 2 and 4 respectively?

x = int(input())
y = int(input())
 
x = x / y
y = y / x
 
print(y)
  • the code will cause a runtime error
  • 2.0
  • 8.0
  • 4.0
Explanation: Let’s analyze this example:

the x variable is assigned the integer value of 2 (2 is inputted by the user, converted to a string by the input() function, and then converted to an integer by the int() function)
the y variable is assigned the integer value of 4 (4 is inputted by the user, converted to a string by the input() function, and then converted to an integer by the int() function)
an operation is performed resulting in the x variable being assigned the value of 0.5 (2 / 4 = 0.5)
an operation is performed resulting in the y variable being assigned the value of 8.0 (4 / 0.5 = 8.0)
the value assigned to the y variable (8.0) is printed to the screen.

15. What is the output of the following snippet if the user enters two lines containing 11 and 4 respectively?

x = int(input())
y = int(input())
 
x = x % y
x = x % y
y = y % x
 
print(y)
  • 1
  • 2
  • 3
  • 4
Explanation: Let’s analyze this example:

the x variable is assigned the integer value of 11 (11 is inputted by the user, converted to a string by the input() function, and then converted to an integer by the int() function)
the y variable is assigned the integer value of 4 (4 is inputted by the user, converted to a string by the input() function, and then converted to an integer by the int() function)
an operation is performed resulting in the x variable being assigned the value of 3 (11 % 4 = 3)
an operation is performed resulting in the x variable being assigned the value of 0 (3 % 11 = 0)
an operation is performed resulting in the y variable being assigned the value of 1 (4 % 3 = 1)
the value assigned to the y variable (1) is printed to the screen.

16. What is the output of the following snippet if the user enters two lines containing 3 and 6 respectively?

x = input()
y = int(input())
 
print(x * y)
  • 333333
  • 666
  • 36
  • 18
Explanation: Let’s analyze this example:

the x variable is assigned the value of “3” (3 is inputted by the user and converted to a string by the input() function)
the y variable is assigned the value of 6 (6 is inputted by the user, converted to a string by the input() function, and then converted to an integer by the int() function)
the print() function outputs the result of the following string multiplication: “3” * 6, that is 333333

17. What is the output of the following snippet?

z = y = x = 1
print(x, y, z, sep='*')
  • x y z
  • 1 1 1
  • x*y*z
  • 1*1*1
Explanation: Let’s analyze the example:

the variables z, y, and x are declared and initialized, and the value 1 is assigned to each of them by using the mechanism of assigning the same value to multiple variables,
the values assigned to the three variables are printed to the screen and separated by the * symbol using the sep keyword argument.

18. What is the output of the following snippet?

y = 2 + 3 * 5.
print(Y)
  • the snippet will cause an execution error
  • 17
  • 25.
  • 17.0
Explanation: Python is case-sensitive, so y and Y are two different variables. Since the program attempts to print to the screen a value associated with a variable that does not exist in the local namespace, Python doesn’t recognize it, and a NameError exception is raised.

19. What is the output of the following snippet?

x = 1 / 2 + 3 // 3 + 4 ** 2
print(x)
  • 8
  • 17
  • 17.5
  • 8.5
Explanation: The principle of operator precedence (order of operations) takes effect here. Let’s see what happens here:

first, the 4 ** 2 expression is evaluated with 16 as the result,
second, the 1 / 2 expression is evaluated with 0.5 as the result,
third, the 3 // 3 expression is evaluated with 1 as the result,
finally, the three values are added (0.5 + 1 + 16), and the resulting value (17.5) is assigned to the x variable and printed to the screen.

20. What is the output of the following snippet if the user enters two lines containing 2 and 4 respectively?

x = int(input())
y = int(input())
 
print(x + y)
  • 4
  • 2
  • 24
  • 6
Explanation: The values 2 and 4 are inputted by the user, converted from strings to integers, and assigned to the x and y variables respectively. The print() function outputs the result of integer addition (2 + 4) to the screen.

Leave a Reply

Your email address will not be published. Required fields are marked *