PCEP Fundamentals of Python Programming 1 — Questions and Answers
Question 1: Which of the following is the correct syntax to output "Hello, World!" in Python?
- print("Hello, World!") (Correct answer)
- echo "Hello, World!"
- console.log("Hello, World!")
- printf("Hello, World!")
Correct answer: print("Hello, World!")
In Python, the `print()` function is the standard way to display output to the console. To output a string like "Hello, World!", you pass the string as an argument inside the parentheses of the `print()` function, enclosed in either single or double quotes. This syntax is universally used for producing console output in Python programs.
Question 2: Which data type is used to store textual data in Python?
- int
- float
- str (Correct answer)
- bool
Correct answer: str
In Python, the `str` data type (short for string) is specifically designed to store textual data, which consists of sequences of characters. Strings are immutable, meaning their content cannot be changed after creation. This data type is fundamental for handling any text-based information, such as names, messages, or file paths, within Python programs.
Question 3: Which of the following is used to start a comment in Python?
- /*
- //
- # (Correct answer)
- <!--
Correct answer: #
In Python, the hash symbol `#` is used to denote a single-line comment. Any text following `#` on that line is ignored by the Python interpreter and serves as documentation for human readers. This allows programmers to add explanatory notes, clarify complex logic, or temporarily disable code sections without affecting program execution.
Question 4: Which of the following Python expressions correctly calculates the length of a string?
- len(string) (Correct answer)
- string.length()
- length(string)
- string.len()
Correct answer: len(string)
Python provides the built-in `len()` function to determine the number of items in an object. When applied to a string, `len(string)` returns the total count of characters within that string. This is the standard and most direct way to get a string's length, which is crucial for various string manipulation tasks.
Question 5: Which of the following statements correctly initializes a dictionary in Python?
- dict = { 'name': 'Alice', 'age': 25 } (Correct answer)
- dict = [ 'name': 'Alice', 'age': 25 ]
- dict = ( 'name': 'Alice', 'age': 25 )
- dict = 'name': 'Alice', 'age': 25
Correct answer: dict = { 'name': 'Alice', 'age': 25 }
In Python, dictionaries are created using curly braces `{}` and store data as key-value pairs. Each key is separated from its value by a colon `:`, and pairs are separated by commas. The syntax `dict = { 'name': 'Alice', 'age': 25 }` correctly initializes a dictionary named `dict` with two key-value entries, making it ready for use.
Which of the following is the correct syntax to output "Hello, World!" in Python?