PE1 : Python Essentials 1 – Module 2 Test Exam Answers
-
The
\n
digraph forces theprint()
function to:- stop its execution
- duplicate the character next to the digragh
- break the output line
- output exactly two characters:
\
andn
Explanation & Hint: 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.
-
What is the output of the following snippet if the user enters two lines containing 2 and 4 respectively?
x = int(input()) y = int(intpu()) x = x / y y = y / x print(y)
8.0
- the code will cause a runtime error
4.0
2.0
-
Explanation & Hint: 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.
-
What is the output of the following snippet?
z = y = x = 1 print(x, y, z, sep='*')
1 1 1
x*y*z
1*1*1
x y z
-
Explanation & Hint: 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.
-
What is the output of the following snippet?
x = 1 / 2 + 3 // 3 + 4 ** 2 print(x)
8.5
0
17.5
17
-
Explanation & Hint: 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.
-
What is the output of the following snippet if the user enter two lines containing
2
and4
respectively?x = int(input()) y = int(input()) print(x + y)
2
24
4
6
-
Explanation & Hint:
-
Which of the following variable names are illegal? (Select two answers)
- true
and
- TRUE
True
-
Explanation & Hint: 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.
-
The result of the following division:
1 / 1
- is equal to
1.0
- is equal to
1
- cannot be evaluated
- cannot be predicted
-
Explanation & Hint: 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.
- is equal to
-
The
**
operator:- does not exist
- performs duplicated multiplication
- performs exponentiation
- perform floating-point multiplication
-
Explanation & Hint: The ** operator performs the exponent arithmetic in Python. It is also called the power operator.
-
What is the output of the following snippet?
y = 2 + 3 * 5. print(Y)
- the snippet will cause an execution error
25.
17
17.0
-
Explanation & Hint: 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.
-
What is the output of the following snippet?
x = 1 y = 2 z = x x = y y = z print (x, y)
1 2
1 1
2 2
2 1
-
Explanation & Hint: 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)
-
The value twenty point twelve times ten raised to the power of eight should be written as:
20.12*10^8
20E12.8
20.12E8.0
20.12E8
-
Explanation & Hint: 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
.
-
What is the output of the following snippet if the user enter two lines containing
2
and4
respectively?x = int(intput()) y = int(intput()) x = x // y y = y //x print(y)
- the code will cause a runtime error
2.0
8.0
4.0
-
Explanation & Hint: 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.
-
What is the output of the following snippet if the user enter two lines containing
11
and4
respectively?x = int(intput()) y = int(intput()) x = x % y x = x % y y = y % x print(y)
3
2
4
1
-
Explanation & Hint: There is a typo in the code provided. The function
intput()
should beinput()
. Assuming you intendedinput()
instead ofintput()
, here is the corrected code and the step-by-step explanation:Final Output:
1
-
The
print()
function can output values of:- not more than five arguments
- any number of arguments (including zero)
- any number of arguments (excluding zero)
- just one argument
-
Explanation & Hint: 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!).
-
What is the output of the following snippet if the user enters two lines containing
3
and6
respectively?x = input() y = int(input()) print(x * y)
333333
36
18
666
-
Explanation & Hint: 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
-
Left-sided binding determines that the result of the following expression:
1 // 2 * 3
is equal to :
0.0
0
4.5
0.166666666666666666
-
Explanation & Hint: Left-sided binding means that the expression is evaluated from left to right: 1 // 2 = 0, and 0 * 3 = 0.
-
Which of the following statements are true? (Select two answers)
- The result of the
/
operator is always an integer value. - The
**
operator uses right-sided binding. - Adding precedes multiplication.
- The right argument of the
%
operator cannot be zero. -
Explanation & Hint: 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.
- The result of the
-
What is the output of the following snippet if the user enters two lines containing
2
and4
respectively?x = input() y = input() print(x + y)
4
2
6
24
-
Explanation & Hint: 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.
-
The meaning of the keyword parameter is determined by:
- its value
- the argument’s name specified along with its value
- its connection with existing variables
- its position within the argument list
-
Explanation & Hint: 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):.
-
The
0o
prefix means that the number after it is denoted as:- hexadecimal
- decimal
- binary
- octal
-
Explanation & Hint: The correct answer is octal.
If an integer number is preceded by
0o
or0O
, 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
or0X
, 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
or0B
, it will be treated as a binary value. For example:0b1111
is a binary number with a decimal value equal to 15.Explanation:
The prefix
0o
is used in Python to denote numbers written in octal (base 8) format. In octal numbering, digits range from 0 to 7.For example:
Here,
0o10
in octal translates to 1×81+0×80=81 \times 8^1 + 0 \times 8^0 = 8 in decimal.Python also uses specific prefixes for other number systems:
0x
: Hexadecimal (base 16).0b
: Binary (base 2).- No prefix or just digits: Decimal (base 10).
Thus, the prefix
0o
is explicitly for octal numbers.
-
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 & Hint: Let’s break down the code step by step to calculate the output:
Final Output:
1
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 (3 % 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.
-
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)
- 6
- 2
- 4
- 24
-
Explanation & Hint: 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.
-
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
- 8.0
- 2.0
- 4.0
-
Explanation & Hint: Let’s analyze the code step by step to determine its behavior:
Key Issue:
- In the second step (
y = y // x
),x
becomes 0 because integer division (//
) of2
by4
results in0
. - Dividing any number by
0
(even in integer division) causes aZeroDivisionError
.
Output:
The code will cause a runtime error.
Correct answer: “the code will cause a runtime error”
- In the second step (