Module 4 Chapter 2 Answers
Question 1
What is a comment in Python?
A. * This is a test
B. // This is a test
C. /* This is a test */
D. # This is a test
โ Correct Answer: D. # This is a test
๐น Explanation:
In Python, comments start with a #
symbol. Everything after #
on the same line is ignored by the interpreter.
Question 2
What does the following code print out?
print("123" + "abc")
A. hello world
B. 123+abc
C. This is a syntax error because you cannot add strings
D. 123abc
โ Correct Answer: D. 123abc
๐น Explanation:
The +
operator concatenates (joins) two strings, so “123” + “abc” becomes “123abc”. Python allows string concatenation.
Question 3
Which of the following is a bad Python variable name?
A. _spam
B. SPAM23
C. 23spam
D. Spam
โ Correct Answer: C. 23spam
๐น Explanation:
Variable names cannot start with a number in Python.
Question 4
Which of the following is not a Python reserved word?
A. speed
B. else
C. for
D. if
โ Correct Answer: A. speed
๐น Explanation:
else
, for
, and if
are reserved keywords in Python. speed
is not a keyword.
Question 5
Assume the variable x
has been initialized to an integer value (e.g., x = 3
). What does the following statement do?
x = x + 2
A. Retrieve the current value for x, add two to it and put the sum back into x
B. Create a function called “x” and put the value 2 in the function
C. This would fail as it is a syntax error
D. Exit the program
โ Correct Answer: A. Retrieve the current value for x, add two to it and put the sum back into x
๐น Explanation:
This statement increases the value of x
by 2 and stores the result in x
.
Question 6
Which of the following elements of a mathematical expression in Python is evaluated first?
A. Multiplication *
B. Addition +
C. Subtraction –
D. Parentheses ( )
โ Correct Answer: D. Parentheses ( )
๐น Explanation:
Operations inside parentheses are evaluated first according to Python’s operator precedence.
Question 7
What is the value of the following expression?
42 % 10
Hint: the %
is the remainder operator.
A. 420
B. 1042
C. 10
D. 2
โ Correct Answer: D. 2
๐น Explanation:
42 % 10
computes the remainder when 42 is divided by 10, which is 2.
Question 8
What will be the value of x
after the following statement executes?
x = 1 + 2 * 3 - 8 / 4
A. 4
B. 2.0
C. 8
D. 5.0
โ Correct Answer: D. 5.0
๐น Explanation:
Following Python’s operator precedence:
- Multiplication and division are done first:
2 * 3 = 6
and8 / 4 = 2.0
- Then,
1 + 6 - 2.0 = 5.0
Question 9
What will be the value of x
when the following statement is executed?
x = int(98.6)
A. 99
B. 98
C. 100
D. 6
โ Correct Answer: B. 98
๐น Explanation:
The int()
function converts a float to an integer by truncating the decimal part. int(98.6)
becomes 98
.
Question 10
What does the Python input()
function do?
A. Pause the program and read data from the user
B. Connect to the network and retrieve a web page
C. Read the memory of the running program
D. Take a screenshot from an area of the screen
โ Correct Answer: A. Pause the program and read data from the user
๐น Explanation:
input()
waits for user input from the keyboard and returns it as a string.
More questions from same assignment:
Question 1
In the following code, what is 98.6
?
A. A variable
B. A constant
C. A conditional statement
D. An iteration/loop statement
โ Correct Answer: B. A constant
๐น Explanation:
98.6
is a literal constant (a fixed value).- It is not a variable because it is not assigned to anything.
- A conditional statement (
if
,else
) makes decisions, and an iteration statement (for
,while
) repeats code.
Question 2
What does the following code print out?
A. hello world
B. 123+abc
C. This is a syntax error because you cannot add strings
D. 123abc
โ Correct Answer: D. 123abc
๐น Explanation:
- The
+
operator concatenates (joins) two strings, so"123" + "abc"
becomes"123abc"
. - There is no syntax error because Python allows string concatenation.
Question 3
Which of the following is a bad Python variable name?
A. #spam
B. SPAM23
C. _spam
D. spam_23
โ Correct Answer: A. #spam
๐น Explanation:
- Variable names cannot start with
#
because#
indicates a comment in Python. _spam
,SPAM23
, andspam_23
are valid variable names.
Question 4
Which of the following is NOT a Python reserved word?
A. speed
B. else
C. for
D. if
โ Correct Answer: A. speed
๐น Explanation:
- Reserved words like
else
,for
, andif
have special meanings in Python. speed
is not a reserved word and can be used as a variable name.
Question 5
What does the following statement do?
A. Exit the program
B. Retrieve the current value for x
, add two to it, and store the sum back into x
C. Create a function called x
and put the value 2
in the function
D. This would fail as it is a syntax error
โ
Correct Answer: B. Retrieve the current value for x
, add two to it, and store the sum back into x
๐น Explanation:
- This is variable reassignment: the new value of
x
is the old value plus 2. - This does not create a function or cause an error.
Question 6
Which element of a mathematical expression in Python is evaluated first?
A. Multiplication *
B. Subtraction -
C. Addition +
D. Parentheses ( )
โ
Correct Answer: D. Parentheses ( )
๐น Explanation:
- Operations follow PEMDAS (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction).
- Parentheses
( )
have highest precedence.
Question 7
What is the value of the following expression?
(Hint: %
is the remainder operator.)
A. 420
B. 2
C. 4210
D. 42
โ Correct Answer: B. 2
๐น Explanation:
%
(modulo) returns the remainder of a division.42 รท 10
gives a remainder of2
, so42 % 10
is2
.
Question 8
What will be the value of x
after this statement executes?
A. 2.0
B. 5.0
C. 4
D. 8
โ Correct Answer: B. 5.0
๐น Explanation:
-
Follow operator precedence (PEMDAS):
x = 1 + (2 * 3) - (8 / 4)
x = 1 + 6 - 2
x = 5.0
-
The result is 5.0 because division (
/
) returns a float.
Question 9
What will be the value of x
when this statement is executed?
A. 100
B. 98
C. 6
D. 99
โ Correct Answer: B. 98
๐น Explanation:
int(98.6)
converts the floating-point number98.6
into an integer by truncating (removing the decimal).- It does not round to 99.
Question 10
What does the Python input()
function do?
A. Pause the program and read data from the user
B. Connect to the network and retrieve a web page
C. Read the memory of the running program
D. Take a screenshot from an area of the screen
โ Correct Answer: A. Pause the program and read data from the user
๐น Explanation:
input()
waits for user input, pauses execution, and returns the input as a string.- It does not access memory, take screenshots, or retrieve web pages.