PCEP Control Flow and Functions 1 — Questions and Answers
Question 1: Which of the following is the correct syntax for an "if" statement in Python?
- if condition { statement }
- if condition: statement (Correct answer)
- if (condition) then statement
- if condition: { statement }
Correct answer: if condition: statement
Python's `if` statement uses a specific and clear syntax: the `if` keyword, followed by a condition, and then a colon `:`. The code block to be executed if the condition is true must be indented below the `if` line. This structure clearly defines the conditional logic, making Python code readable and consistent.
Question 2: What does the "return" statement do in a function?
- It ends the function and sends a value back to the caller. (Correct answer)
- It pauses the function execution.
- It starts the function execution.
- It prints a value to the console.
Correct answer: It ends the function and sends a value back to the caller.
The `return` statement is used inside a function to immediately exit its execution and optionally send a value back to the part of the code that called the function. Once `return` is executed, no further code within that function will run. This allows functions to produce results that can be used elsewhere in the program.
Question 3: How do you define a function in Python?
- function myFunction():
- def myFunction(): (Correct answer)
- func myFunction():
- define myFunction():
Correct answer: def myFunction():
In Python, functions are defined using the `def` keyword, followed by the function name, a pair of parentheses `()`, and a colon `:`. The code block that constitutes the function's body must then be indented below this definition line. This syntax establishes a new callable function, encapsulating a block of reusable code.
Question 4: Which keyword is used to exit a loop prematurely in Python?
- break (Correct answer)
- continue
- return
- exit
Correct answer: break
The `break` statement in Python is used to terminate the current loop (either `for` or `while`) prematurely. When `break` is encountered, the loop immediately stops, and program execution continues with the statement immediately following the loop. This is useful for exiting a loop based on a specific condition being met.
Question 5: In Python, what does the "else" block of a "for" loop execute for?
- When the loop encounters a break statement
- When the loop has no more items to iterate over (Correct answer)
- When the loop encounters an exception
- When the loop is explicitly terminated by the user
Correct answer: When the loop has no more items to iterate over
In Python, a `for` loop can optionally have an `else` block, which executes only if the loop completes all its iterations without encountering a `break` statement. If the loop is terminated by a `break`, the `else` block is skipped entirely. This provides a way to execute specific code only when a loop finishes naturally.
Which of the following is the correct syntax for an "if" statement in Python?