Free WGU Foundations-of-Programming-Python Practice Test & Real Exam Questions
Which keyword moves a loop immediately to its subsequent iteration?
Correct Answer: B
Vote an answer
Explanation: Only visible for Pass4Leader members. You can sign-up / login (it's free).
Which keyword is used to exit a loop prematurely in Python?
Correct Answer: D
Vote an answer
Explanation: Only visible for Pass4Leader members. You can sign-up / login (it's free).
A program needs to display only positive, non-zero numbers from a list containing a mixture of integer and decimal values. Which conditional statement should be placed inside the loop?
Correct Answer: D
Vote an answer
Explanation: Only visible for Pass4Leader members. You can sign-up / login (it's free).
Which for loop correctly iterates through a list of student names?
Correct Answer: C
Vote an answer
Explanation: Only visible for Pass4Leader members. You can sign-up / login (it's free).
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]
def first_three(text):
return text[0:2]
Correct Answer:
See the Step by Step Solution below in Explanation.
Explanation:
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
Explanation:
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
Write a complete function calculate_average(grades) that takes a list of grades and returns the average as a float.
For example, calculate_average([85, 92, 78]) should return 85.0.
def calculate_average(grades):
# TODO: Calculate and return the average grade as a float
pass
For example, calculate_average([85, 92, 78]) should return 85.0.
def calculate_average(grades):
# TODO: Calculate and return the average grade as a float
pass
Correct Answer:
See the Step by Step Solution below in Explanation.
Explanation:
Step 1: The function receives one parameter named grades, which is a list of numbers.
Step 2: Use sum(grades) to add all the grades together.
Step 3: Use len(grades) to count how many grades are in the list.
Step 4: Divide the total by the number of grades.
Correct code:
def calculate_average(grades):
return sum(grades) / len(grades)
Example:
print(calculate_average([85, 92, 78]))
Output:
85.0
Explanation:
Step 1: The function receives one parameter named grades, which is a list of numbers.
Step 2: Use sum(grades) to add all the grades together.
Step 3: Use len(grades) to count how many grades are in the list.
Step 4: Divide the total by the number of grades.
Correct code:
def calculate_average(grades):
return sum(grades) / len(grades)
Example:
print(calculate_average([85, 92, 78]))
Output:
85.0
Which action allows a Python script located in a different folder to be executed in the terminal?
Correct Answer: D
Vote an answer
Explanation: Only visible for Pass4Leader members. You can sign-up / login (it's free).
