PCEP-30-02 practice question 19 of 35

How can you create multiline comments in Python? (select 3)

Full set: 548 questions

Domain Computer Programming and Python Fundamentals

Answer options
A: A multiline comment can be placed between * and *
B: A multiline comment can be placed between the string """ (triple quotation marks) Correct answer
C: A multiline comment can be placed between /* and */
D: We can create a multiline comment from multiple single-line comments by inserting the # sign at the beginning of each line Correct answer
E: A multiline comment can be placed between the string ''' (triple apostrophe) Correct answer
F: A multiline comment can be placed between -- and --
Correct Answer:
B. A multiline comment can be placed between the string """ (triple quotation marks); D. We can create a multiline comment from multiple single-line comments by inserting the # sign at the beginning of each line; E. A multiline comment can be placed between the string ''' (triple apostrophe)
Explanation

Python does not have a specific syntax for multiline comments in the way other languages might use / ... /. However, the effect of a multiline comment can be achieved in two ways. The most direct method is to create a block of single-line comments by starting each line with a hash (#) symbol. Alternatively, Python's syntax for multiline string literals, which uses either triple double-quotes ("""...""") or triple single-quotes ('''...'''), can be used. When a multiline string is not assigned to a variable or used in an expression, the Python interpreter ignores it during execution, making it function as a de facto multiline comment. This technique is also the standard for creating documentation strings (docstrings).

Why Incorrect

A. The and syntax is not a valid comment format in Python.
C. The / and / syntax is used for block comments in languages like C, Java, and JavaScript, but not in Python.
F. The -- and -- syntax is used for comments in languages like SQL and Ada, but it is not valid in Python.

References

1. Python Institute, Python Essentials 1, Module 2, Section 2.1.5 – Comments: "If you want to add a comment that spans several lines, you need to put a hash at the beginning of each line." This directly supports option D.

2. Official Python 3 Documentation, The Python Tutorial, Section 3.1.2. Strings: "String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''... Often used for docstrings". This supports the use of triple-quoted strings (options B and E) which, when not assigned, act as comments.

3. Severance, C. R. (2016). Python for Everybody: Exploring Data in Python 3. Chapter 2, Section 2.8 - Comments, Page 23: "As your programs become bigger and more complicated, they get more difficult to read... It is a good idea to add notes to your programs to explain in natural language what the program is doing. These notes are called comments, and they start with the # symbol... Anything from the # to the end of the line is ignored". This reference supports option D as the primary method for commenting. The book later introduces multiline strings for docstrings.

Scroll to Top