Which components are required in every Python while loop?
Answer : A
A Python while loop needs a condition and an indented code block.
Example:
count = 1
while count <= 3:
print(count)
count += 1
The condition is count <= 3. The indented block contains the code that runs while the condition is true. Python's compound statement documentation includes while as a control-flow statement.
Therefore, the correct answer isA. A condition and an indented code block.
A program needs to update all values in a list by adding 5 to each number. Which for loop structure accomplishes this task?
Answer : B
To update the actual values inside a list, the program must update each value by itsindex.
The correct code is:
for i in range(len(numbers)):
numbers[i] = numbers[i] + 5
Here, range(len(numbers)) produces valid index positions for the list. The variable i is used to access and update each list element.
Example:
numbers = [10, 20, 30]
for i in range(len(numbers)):
numbers[i] = numbers[i] + 5
print(numbers)
Output:
[15, 25, 35]
Option A does not correctly update the original list:
for number in numbers:
number = number + 5
This changes only the temporary loop variable number, not the actual elements stored inside the list.
Therefore, the correct answer isB.
How does Jupyter Notebook function as a development environment?
Answer : B
Jupyter Notebook works as acell-based interactive coding platform. A notebook is made up of cells, and code can be written and executed inside those cells.
For example, a code cell may contain:
x = 10
print(x)
When the cell is run, the output appears directly below the cell.
The Jupyter Notebook documentation explains that notebooks consist of a sequence of cells, and the contents of a cell can be executed using keyboard shortcuts or the toolbar button.
Jupyter Notebook is not simply a command-line terminal replacement, not limited to static text editing, and not mainly a framework for compiling Python into standalone applications.
Therefore, the correct answer isB. Cell-based interactive coding platform.
Complete the function add_item(numeric_list, new_number) that takes a list of numbers and a new number, and returns a new list that appends the new number to the end of the list.
For example, add_item([1, 2, 3], 4) should return [1, 2, 3, 4].
Answer : A
The correct function uses the list method append() to add new_number to the end of numeric_list.
Correct code:
def add_item(numeric_list, new_number):
numeric_list.append(new_number)
return numeric_list
Example:
add_item([1, 2, 3], 4)
Result:
[1, 2, 3, 4]
Python's data structures documentation explains that list.append(x) adds an item to the end of the list.
Therefore, the correct answer isA.
What advantage does an integrated terminal provide compared to using a separate terminal application?
Answer : C
An integrated terminal is built into a development environment or code editor. Its main advantage is that the developer can write code and run terminal commands in the same application.
This avoids switching back and forth between a separate terminal window and the code editor.
Therefore, the correct answer isC. Eliminates need to switch between applications.
SIMULATION
Fix the off-by-one error in this function that should return the first 3 characters of a string.
def first_three(text):
return text[0:2]
Answer : A
==========
Step 1: Python string slicing uses this format:
text[start:stop]
Step 2: The start index is included.
Step 3: The stop index is excluded.
Step 4: To return the first 3 characters, start at index 0 and stop at index 3.
Correct code:
def first_three(text):
return text[0:3]
Simplified correct code:
def first_three(text):
return text[:3]
Example:
print(first_three('Python'))
Output:
Pyt
Which data type does the expression 5 > 3 evaluate to in Python?
Answer : C
In Python, comparison expressions such as 5 > 3 evaluate to aBooleanvalue.
The expression:
5 > 3
checks whether 5 is greater than 3. Since this statement is true, Python returns:
True
True and False are Boolean values in Python.
Therefore, the correct answer isC. Boolean.