Which type of loop repeatedly checks a condition to determine whether to continue?
Answer : B
Awhile looprepeatedly executes a block of code as long as its condition remains true.
Example:
count = 1
while count <= 3:
print(count)
count += 1
In this example, Python checks the condition count <= 3 before each loop iteration. If the condition is true, the loop continues. When the condition becomes false, the loop stops.
A for loop is usually used to iterate over a sequence, such as a list, string, or range. Python does not have a built-in repeat loop construct.
Therefore, the correct answer isB. while loop.
SIMULATION
Write a complete function reverse_string(text) that takes a string and returns it reversed.
For example, reverse_string("hello") should return "olleh".
def reverse_string(text):
# TODO: Return the reversed string
pass
Answer : A
==========
Step 1: The function receives one string parameter named text.
Step 2: Python slicing can be used to reverse a string.
Step 3: The slice [::-1] means start at the end of the string and move backward by one character at a time.
Step 4: Return text[::-1].
Correct code:
def reverse_string(text):
return text[::-1]
Example:
print(reverse_string('hello'))
Output:
olleh
SIMULATION
Modify the function greet_with_default(name) by adding a default value of "World" to the name parameter so it can be called with or without an argument.
def greet_with_default(name):
# TODO: Add a default value of "World" to the name parameter
return "Hello " + name
Answer : A
==========
Step 1: A default parameter value allows a function to be called even when no argument is provided.
Step 2: The default value should be added in the function header.
Step 3: The parameter name should have the default value 'World'.
Correct code:
def greet_with_default(name='World'):
return 'Hello ' + name
Example:
print(greet_with_default())
print(greet_with_default('Alice'))
Output:
Hello World
Hello Alice
What sequence of steps is required to execute a Python script from a text editor using the terminal on a Windows device?
Answer : A
To run a Python script from a text editor using the terminal on Windows, the file should first be saved with the .py extension. Then, the terminal is opened, and the script is executed using the Python command followed by the filename.
Example:
python filename.py
The official Python documentation explains that when the interpreter is called with a filename argument, it reads and executes the script from that file. On Windows, the python command can be used from a terminal after Python is installed and configured correctly.
Therefore, the correct answer is A. Save file > open terminal > type python filename.py.
Which keyword moves a loop immediately to its subsequent iteration?
Answer : C
The Python keyword continue is used inside a loop to skip the rest of the current iteration and move immediately to the next iteration.
Example:
for number in range(5):
if number == 2:
continue
print(number)
Output:
0
1
3
4
When number is 2, the continue statement skips print(number) for that iteration and moves to the next loop cycle.
The official Python documentation states that the continue statement continues with the next iteration of the loop.
Therefore, the correct answer is C. continue.
What must be consistent within a Python code block for the code to run without syntax errors?
Answer : B
In Python, indentation is used to group statements into a code block. A block must have a consistent indentation level so Python can correctly understand which statements belong together.
For example:
if True:
print('Hello')
print('World')
Both print() statements belong to the same if block because they use the same indentation level.
If indentation is inconsistent, Python can raise an IndentationError or TabError.
Therefore, the correct answer isB. Indentation level.
Which punctuation mark must appear at the end of an if statement line?
Answer : B
In Python, an if statement line must end with a colon.
Example:
age = 18
if age >= 18:
print('Adult')
The colon : tells Python that an indented block of code follows. That indented block contains the statements that should run when the condition is true.
A semicolon, period, or comma is not used to begin the body of an if statement in Python.
Therefore, the correct answer is B. : colon.