WGU Foundations of Programming (Python) (E010) Exam Questions

Page: 1 / 14
Total 60 questions
Question 1

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?



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 2

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



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


Question 3

Which data type is the value 3.14 in Python?



Answer : B

The value 3.14 is afloatbecause it contains a decimal point.

Example:

type(3.14)

This returns:

<class 'float'>

Python's built-in numeric types include integers and floating-point numbers. A value with a decimal part, such as 3.14, is treated as a floating-point number.

Therefore, the correct answer isB. Float.


Question 4

Which terminal command is used to navigate to a different directory before running a Python script?



Answer : C

The command cd means ''change directory.'' It is used in a terminal to move from one folder to another.

Example:

cd Desktop

After navigating to the correct folder, a Python script can be run with a command such as:

python script.py

Therefore, the correct answer isC. cd.


Question 5

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



Answer : B

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

Which type of loop repeatedly checks a condition to determine whether to continue?



Answer : B

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.


Question 7

Which for loop correctly iterates through a list of student names?



Answer : A

The correct Python for loop syntax uses the keyword in.

Correct code:

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

print(name)

This loop processes each name in the list one at a time. Python's documentation explains that a for statement iterates over the items of a sequence or iterable.

Therefore, the correct answer isA. for name in ['Alice', 'Bob', 'Carol']:.


Page:    1 / 14   
Total 60 questions