Limited-Time Offer: Enjoy 50% Savings! Ends in 00h 00m 00s Coupon code: 50OFF
Skip to content

Free WGU Foundations of Programming (Python) Foundations-of-Programming-Python Exam Questions

Page: 1 / 6 Total 60 questions

Want more questions? Get Premium Access.

Question 1

SIMULATION

Complete the function get_max(a, b) that returns the larger of two numbers. If they are equal, return either one.

def get_max(a, b):

# TODO: Return the larger of a and b

pass

Correct Answer: A. See the Step by Step Solution below in Explanation
Explanation:

Step 1: The function receives two parameters: a and b.

Step 2: Compare the two values using an if statement.

Step 3: If a is greater than or equal to b, return a.

Step 4: Otherwise, return b.

Correct code:

def get_max(a, b):

if a >= b:

return a

else:

return b

Simplified correct code:

def get_max(a, b):

return max(a, b)

Example:

print(get_max(10, 20))

print(get_max(30, 15))

print(get_max(5, 5))

Output:

20

30

5


Question 2

SIMULATION

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: A. 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


Question 3

Which Python data structure allows duplicate values and supports methods like append() and remove()?

Correct Answer: C. List
Explanation:

A list is a Python data structure that can store multiple values. Lists can contain duplicate values, and they support methods such as append() and remove().

Example:

fruits = ['apple', 'banana', 'apple']

fruits.append('orange')

fruits.remove('banana')

print(fruits)

Output:

['apple', 'apple', 'orange']

The value 'apple' appears more than once, showing that lists allow duplicates.

The official Python documentation lists list.append() as a method that adds an item to the end of a list and list.remove() as a method that removes the first matching value from a list.

Therefore, the correct answer is C. List.


Question 4

SIMULATION

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: A. 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


Question 5

Which type of loop is designed for iterating a specific number of times?

Correct Answer: B. for loop
Explanation:

A for loop is commonly used when a program needs to repeat code for each item in a sequence or for a specific number of times using range().

Example:

for number in range(5):

print(number)

This loop runs 5 times. Python's documentation explains that the for statement iterates over items of a sequence or iterable.

Therefore, the correct answer isB. for loop.


Question 6

In Python, what must follow the in keyword in a for loop?

Correct Answer: B. An iterable object
Explanation:

In a Python for loop, the in keyword is followed by an iterable object, such as a list, string, tuple, dictionary, set, or range() object.

Example:

for name in ['Alice', 'Bob', 'Carol']:

print(name)

Here, ['Alice', 'Bob', 'Carol'] is the iterable object. Python's documentation explains that a for statement iterates over the items of a sequence or other iterable object.

Therefore, the correct answer isB. An iterable object.


Question 7

Which index position is returned when the string method .find('python') is applied to the string 'learning python programming'?

Correct Answer: B. 9
Explanation:

The .find() method returns the lowest index where the searched substring is found.

Example:

'learning python programming'.find('python')

Output:

9

Indexing starts at 0 in Python. In the string 'learning python programming', the word 'python' begins at index 9. Python's built-in types documentation describes str.find() as returning the lowest index where the substring is found.

Therefore, the correct answer isB. 9.


Question 8

What does the string method ''.join() return when applied to the list ['Hello', 'World']?

Correct Answer: B. 'HelloWorld'
Explanation:

The string method join() combines the strings in an iterable using the string before .join() as the separator.

In this question, the separator is an empty string:

''.join(['Hello', 'World'])

Because the separator is empty, Python joins 'Hello' and 'World' directly together with no space or comma between them.

Result:

'HelloWorld'

The official Python documentation defines str.join() as returning a string made by joining the strings in the given iterable.

Therefore, the correct answer is B. 'HelloWorld'.


Question 9

SIMULATION

Fix the missing function call in this function that should return the uppercase version of a string.

def make_uppercase(text):

return text.upper

Correct Answer: A. See the Step by Step Solution below in Explanation
Explanation:

Step 1: In Python, upper is a string method.

Step 2: A method must be called using parentheses ().

Step 3: text.upper refers to the method itself, but it does not execute it.

Step 4: Use text.upper() to return the uppercase version of the string.

Correct code:

def make_uppercase(text):

return text.upper()

Example:

print(make_uppercase('hello'))

Output:

HELLO


Question 10

Which Python data structure cannot be modified after creation?

Correct Answer: C. Tuple
Explanation:

Atupleis immutable, which means it cannot be changed after it is created.

Example:

coordinates = (10, 20)

After this tuple is created, its elements cannot be modified directly. Python's data model documentation explains that tuples are immutable, while lists and dictionaries are mutable.

Therefore, the correct answer isC. Tuple.