Limited-Time Offer: Enjoy 50% Savings! - Ends In 0d 00h 00m 00s Coupon code: 50OFF
Welcome to QA4Exam

Practice questions demo

Foundations-of-Programming-Python Exam Dumps and Practice Questions

Questions 1 to 5 of the 60-question Foundations-of-Programming-Python bank, each with its correct answer and an explanation. This is page 1 of 12.

1 / 12

Actual WGU Foundations-of-Programming-Python Exam Questions

Question 1

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

Correct answer: B

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 2

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

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 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?

Correct answer: C

The slice filename[-3:] extracts the last three characters from a string.

Example:

filename = 'document.pdf'

print(filename[-3:])

Output:

pdf

Another example:

filename = 'image.png'

print(filename[-3:])

Output:

png

Python strings support slicing, and negative indexes count from the end of the string.

Therefore, the correct answer isC. filename[-3:].


Question 4

Which action allows a Python script located in a different folder to be executed in the terminal?

Correct answer: C

To run a Python script from the terminal, the terminal should be in the correct directory, or the full file path should be provided.

The cd command is commonly used to change directories before running the script.

Example:

cd Documents

python my_script.py

Python's documentation explains that Python can execute a script when the interpreter is called with a filename argument.

Therefore, the correct answer isC. Use the cd command to navigate to the script's directory.


Question 5

SIMULATION

Write a complete function reverse_string(text) that takes a string and returns it reversed.

For example, reverse_string("hello") should return "olleh".

def reverse_string(text):

# TODO: Return the reversed string

pass

Correct answer: A

Step 1: The function receives one string parameter named text.

Step 2: Python slicing can be used to reverse a string.

Step 3: The slice [::-1] means start at the end of the string and move backward by one character at a time.

Step 4: Return text[::-1].

Correct code:

def reverse_string(text):

return text[::-1]

Example:

print(reverse_string('hello'))

Output:

olleh


Unlock all WGU Foundations-of-Programming-Python questions

Full exam access, validated answers, and unlimited practice with no download limits.