
WGU Foundations of Programming (Python) - E010 JIV1 - Foundations-of-Programming-Python Exam Questions
QUESTION NO: 1
Complete the function double_number(num) that takes one number parameter and returns double that number.
For example, double_number(5) should return 10.
def double_number(num):
# TODO: Return double the input number
# Example: double_number(5) should return 10
pass
Complete the function double_number(num) that takes one number parameter and returns double that number.
For example, double_number(5) should return 10.
def double_number(num):
# TODO: Return double the input number
# Example: double_number(5) should return 10
pass
Correct Answer:
See the Step by Step Solution below in Explanation.
Explanation:
Step 1: The function receives one parameter named num.
Step 2: To double a number, multiply it by 2.
Step 3: The function should return the result using the return statement.
Correct code:
def double_number(num):
return num * 2
Example:
print(double_number(5))
Output:
10
Explanation:
Step 1: The function receives one parameter named num.
Step 2: To double a number, multiply it by 2.
Step 3: The function should return the result using the return statement.
Correct code:
def double_number(num):
return num * 2
Example:
print(double_number(5))
Output:
10
QUESTION NO: 2
Fix the indexing error in this function that should return the last character of a string.
def get_last_character(text):
return text[len(text)]
Fix the indexing error in this function that should return the last character of a string.
def get_last_character(text):
return text[len(text)]
Correct Answer:
See the Step by Step Solution below in Explanation.
Explanation:
Step 1: Python string indexing starts at 0.
Step 2: The last valid index of a string is len(text) - 1, not len(text).
Step 3: Python also allows negative indexing, where -1 means the last character.
Step 4: Replace text[len(text)] with text[-1] .
Correct code:
def get_last_character(text):
return text[-1]
Example:
print(get_last_character( " Python " ))
Output:
n
Explanation:
Step 1: Python string indexing starts at 0.
Step 2: The last valid index of a string is len(text) - 1, not len(text).
Step 3: Python also allows negative indexing, where -1 means the last character.
Step 4: Replace text[len(text)] with text[-1] .
Correct code:
def get_last_character(text):
return text[-1]
Example:
print(get_last_character( " Python " ))
Output:
n
QUESTION NO: 3
A program processes file names and extracts the last 3 characters representing the file extension. For example, files ' document.pdf ' and ' image.png ' would return ' pdf ' and ' png ' , respectively. Which slicing approach would work for either of the provided files?
A program processes file names and extracts the last 3 characters representing the file extension. For example, files ' document.pdf ' and ' image.png ' would return ' pdf ' and ' png ' , respectively. Which slicing approach would work for either of the provided files?
Correct Answer: C
Explanation: Only visible for Pass4Test members. You can sign-up / login (it's free).
QUESTION NO: 4
Fix the logic error in this function that should return the larger of two numbers.
def find_maximum(a, b):
if a < b:
return a
else:
return b
Fix the logic error in this function that should return the larger of two numbers.
def find_maximum(a, b):
if a < b:
return a
else:
return b
Correct Answer:
See the Step by Step Solution below in Explanation.
Explanation:
Step 1: The function should return the larger number.
Step 2: The condition a < b means b is larger than a.
Step 3: In the original code, when a < b is true, it incorrectly returns a.
Step 4: The return values need to be corrected.
Correct code:
def find_maximum(a, b):
if a < b:
return b
else:
return a
Alternative correct code:
def find_maximum(a, b):
if a > b:
return a
else:
return b
Example:
print(find_maximum(10, 20))
print(find_maximum(30, 15))
Output:
20
30
Explanation:
Step 1: The function should return the larger number.
Step 2: The condition a < b means b is larger than a.
Step 3: In the original code, when a < b is true, it incorrectly returns a.
Step 4: The return values need to be corrected.
Correct code:
def find_maximum(a, b):
if a < b:
return b
else:
return a
Alternative correct code:
def find_maximum(a, b):
if a > b:
return a
else:
return b
Example:
print(find_maximum(10, 20))
print(find_maximum(30, 15))
Output:
20
30
QUESTION NO: 5
How does Jupyter Notebook function as a development environment?
How does Jupyter Notebook function as a development environment?
Correct Answer: B
Explanation: Only visible for Pass4Test members. You can sign-up / login (it's free).
QUESTION NO: 6
Which data type is the value 3.14 in Python?
Which data type is the value 3.14 in Python?
Correct Answer: D
Explanation: Only visible for Pass4Test members. You can sign-up / login (it's free).
QUESTION NO: 7
Write a complete function word_count(text) that counts and returns the number of words in the given text.
Words are separated by spaces.
For example, word_count( " Hello world Python " ) should return 3.
def word_count(text):
# TODO: Count and return the number of words in text
pass
Write a complete function word_count(text) that counts and returns the number of words in the given text.
Words are separated by spaces.
For example, word_count( " Hello world Python " ) should return 3.
def word_count(text):
# TODO: Count and return the number of words in text
pass
Correct Answer:
See the Step by Step Solution below in Explanation.
Explanation:
Step 1: The function receives one string parameter named text.
Step 2: Since words are separated by spaces, use the .split() method to split the string into a list of words.
Step 3: Use len() to count how many items are in the list.
Step 4: Return that count.
Correct code:
def word_count(text):
return len(text.split())
Example:
print(word_count( " Hello world Python " ))
Output:
3
Explanation:
Step 1: The function receives one string parameter named text.
Step 2: Since words are separated by spaces, use the .split() method to split the string into a list of words.
Step 3: Use len() to count how many items are in the list.
Step 4: Return that count.
Correct code:
def word_count(text):
return len(text.split())
Example:
print(word_count( " Hello world Python " ))
Output:
3




