PCEP Fundamentals of Python Programming 3 — Questions and Answers
Question 1: What keyword is used to define a function in Python?
- function
- define
- def (Correct answer)
- fun
Correct answer: def
The `def` keyword is used to define a function in Python.
Question 2: What is the output of `print(2 ** 3)` in Python?
- 6
- 8 (Correct answer)
- 9
- 5
Correct answer: 8
The `**` operator is exponentiation, so `2 ** 3` equals 2 to the power of 3, which is 8.
Question 3: Which Python built-in function converts a string to an integer?
- str()
- float()
- int() (Correct answer)
- num()
Correct answer: int()
`int()` converts a compatible string or float to an integer value.
Question 4: What is the correct way to write a single-line comment in Python?
- // This is a comment
- /* This is a comment */
- # This is a comment (Correct answer)
- -- This is a comment
Correct answer: # This is a comment
Single-line comments in Python begin with the `#` symbol.
Question 5: What is the output of `print(round(3.567, 2))`?
- 3.5
- 3.56
- 3.57 (Correct answer)
- 4.0
Correct answer: 3.57
`round(3.567, 2)` rounds to 2 decimal places, giving 3.57 because the third decimal is 7.
Question 6: In Python, which of the following correctly creates a multi-line string?
- """line1\nline2""" (Correct answer)
- "line1" + "line2"
- multi(line1, line2)
- 'line1', 'line2'
Correct answer: """line1\nline2"""
Triple quotes (`"""..."""` or `'''...'''`) allow multi-line strings in Python.
Question 7: What does the `input()` function return in Python 3?
- int
- float
- str (Correct answer)
- bytes
Correct answer: str
`input()` always returns user input as a string in Python 3, regardless of what the user types.
What keyword is used to define a function in Python?