Foundations-of-Programming-Python Exam Questions & Answers
Foundations of Programming (Python) - E010 JIV1 • WGU
100% money-back guarantee
Sample Foundations-of-Programming-Python Questions
Practice with real exam-style questions, each with the verified correct answer and explanation.
Which type of loop repeatedly checks a condition to determine whether to continue?
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
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
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
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]
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
In the code for item in my_list:, what does item represent?
In the loop:
for item in my_list:
print(item)
item represents the current element from my_list during each loop iteration.
For example:
my_list = ['apple', 'banana']
for item in my_list:
print(item)
The loop first processes 'apple', then 'banana'. Python's documentation explains that a for statement iterates over the items of a sequence in order.
Therefore, the correct answer isB. The current element being processed.
How can the first four characters be extracted from the string 'computer'?
Python strings are sequences, and sequence values can be accessed using indexing and slicing.
The first character in a Python string has index 0. To extract the first four characters from 'computer', the slice should start at index 0 and stop at index 4.
Example:
'computer'[0:4]
Result:
'comp'
In Python slicing, the start index is included, but the stop index is excluded. So [0:4] includes characters at indexes 0, 1, 2, and 3.
The official Python documentation explains that slicing with a[start:stop] selects items from the start index up to, but not including, the stop index.
Therefore, the correct answer is A. 'computer'[0:4].
Get access to all 60 verified questions with detailed answers.
Unlock All Foundations-of-Programming-Python Questions