
Get Perfect Results with Premium Foundations-of-Programming-Python Dumps Updated 62 Questions
Free Foundations-of-Programming-Python Exam Study Guide for the NEW Dumps Test Engine
NEW QUESTION # 27
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
Answer:
Explanation:
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
NEW QUESTION # 28
In Python, what must follow the in keyword in a for loop?
- A. A variable name
- B. A number
- C. A condition
- D. An iterable object
Answer: D
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.
NEW QUESTION # 29
Complete the function is_positive(number) that returns True if the number is greater than 0, and False otherwise.
def is_positive(number):
# TODO: Return True if number > 0, False otherwise
pass
Answer:
Explanation:
See the Step by Step Solution below in Explanation.
Explanation:
Step 1: The function receives one parameter named number.
Step 2: The condition number > 0 checks whether the number is positive.
Step 3: In Python, this comparison already returns either True or False.
Correct code:
def is_positive(number):
return number > 0
Example:
print(is_positive(5))
print(is_positive(-2))
print(is_positive(0))
Output:
True
False
False
NEW QUESTION # 30
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
Answer:
Explanation:
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
NEW QUESTION # 31
A program uses this while loop to count down:
count = 5
while count > 0:
print(count)
Which issue is preventing the loop from working correctly?
- A. There is a missing colon after the condition.
- B. It runs infinitely because count is never modified.
- C. The condition should use > = instead of > .
- D. The variable should be named differently.
Answer: B
Explanation:
The loop condition is:
while count > 0:
At the start, count is 5, so the condition is true. The program prints the value of count.
However, inside the loop, the value of count is never changed. That means count always stays 5, so the condition count > 0 is always true. As a result, the loop runs forever.
A corrected version would be:
count = 5
while count > 0:
print(count)
count = count - 1
This decreases count by 1 during each loop iteration. Eventually, count becomes 0, and the condition count >
0 becomes false.
Therefore, the correct answer isB. It runs infinitely because count is never modified.
NEW QUESTION # 32
What sequence of steps is required to execute a Python script from a text editor using the terminal on a Windows device?
- A. Save file > compile code > run executable
- B. Save file > open terminal > type python filename.py
- C. Save file > close text editor > open different IDE
- D. Open terminal > write code directly > press Enter
Answer: B
Explanation:
To run a Python script from a text editor using the terminal on Windows, the file should first be saved with the .py extension. Then, the terminal is opened, and the script is executed using the Python command followed by the filename.
Example:
python filename.py
The official Python documentation explains that when the interpreter is called with a filename argument, it reads and executes the script from that file. On Windows, the python command can be used from a terminal after Python is installed and configured correctly.
Therefore, the correct answer is A. Save file > open terminal > type python filename.py.
NEW QUESTION # 33
Complete the function update_grade(grades, student, new_grade) that takes a grades dictionary, a student name, and a new grade, then updates that student ' s grade and returns the dictionary.
For example, update_grade({ " Alice " : 85, " Bob " : 90}, " Alice " , 95) should return { " Alice " : 95, " Bob
" : 90}.
- A. def update_grade(grades, student, new_grade):grades.append(new_grade)return grades
- B. def update_grade(grades, student, new_grade):pass
- C. def update_grade(grades, student, new_grade):grades[student] = new_gradereturn grades
- D. def update_grade(grades, student, new_grade):student = new_gradereturn student
Answer: C
Explanation:
A dictionary stores data as key-value pairs. In this function, the student name is the key, and the grade is the value.
Correct code:
def update_grade(grades, student, new_grade):
grades[student] = new_grade
return grades
Example:
update_grade({ " Alice " : 85, " Bob " : 90}, " Alice " , 95)
Result:
{ " Alice " : 95, " Bob " : 90}
Python's built-in types documentation includes dictionaries as mapping types, where values are accessed and updated using keys.
Therefore, the correct answer isA.
NEW QUESTION # 34
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
Answer:
Explanation:
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
NEW QUESTION # 35
Complete the function get_second_item(items) that takes a list and returns the second item. Assume the input is always a list containing at least two elements.
For example, get_second_item([10, 20, 30]) should return 20.
def get_second_item(items):
# TODO: Return the second item from the list
pass
Answer:
Explanation:
See the Step by Step Solution below in Explanation.
Explanation:
Step 1: Python list indexing starts at 0.
Step 2: The first item is at index 0.
Step 3: The second item is at index 1.
Step 4: Return the value at index 1.
Correct code:
def get_second_item(items):
return items[1]
Example:
print(get_second_item([10, 20, 30]))
Output:
20
NEW QUESTION # 36
Which for loop correctly iterates through a list of student names?
- A. for name in [ ' Alice ' , ' Bob ' , ' Carol ' ]:
- B. for name with [ ' Alice ' , ' Bob ' , ' Carol ' ]:
- C. for name from [ ' Alice ' , ' Bob ' , ' Carol ' ]:
- D. for name = [ ' Alice ' , ' Bob ' , ' Carol ' ]
Answer: A
Explanation:
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 ' ]:.
NEW QUESTION # 37
Fix the indentation error in this function that should return a greeting message.
def greet(name):
return " Hello " + name
Answer:
Explanation:
See the Step by Step Solution below in Explanation.
Explanation:
Step 1: In Python, the code inside a function must be indented.
Step 2: The return statement belongs inside the function body.
Step 3: Add indentation before the return statement.
Correct code:
def greet(name):
return " Hello " + name
Example:
print(greet( " Alice " ))
Output:
Hello Alice
NEW QUESTION # 38
Complete the function calculate_tip(bill, tip_percent) that calculates and returns the tip amount based on the bill and tip percentage.
For example, calculate_tip(50, 20) should return 10.0.
def calculate_tip(bill, tip_percent):
# TODO: Calculate and return the tip amount
pass
Answer:
Explanation:
See the Step by Step Solution below in Explanation.
Explanation:
Step 1: The function receives two parameters: bill and tip_percent.
Step 2: Convert the percentage into a decimal by dividing tip_percent by 100.
Step 3: Multiply the bill amount by that decimal value.
Correct code:
def calculate_tip(bill, tip_percent):
return bill * tip_percent / 100
Example:
print(calculate_tip(50, 20))
Output:
10.0
NEW QUESTION # 39
Fix the missing return statement in this function that should return whether a number is even.
def is_even(number):
if number % 2 == 0:
True
else:
False
Answer:
Explanation:
See the Step by Step Solution below in Explanation.
Explanation:
Step 1: The expression number % 2 == 0 checks whether the number is even.
Step 2: The original code writes True and False, but it does not return them.
Step 3: A function must use the return statement to send a value back to the caller.
Correct code:
def is_even(number):
if number % 2 == 0:
return True
else:
return False
Simplified correct code:
def is_even(number):
return number % 2 == 0
Example:
print(is_even(4))
print(is_even(7))
Output:
True
False
NEW QUESTION # 40
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:
Explanation:
See the Step by Step Solution below in Explanation.
Explanation:
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
NEW QUESTION # 41
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. filename[9:]
- B. filename[-3:]
- C. filename[:-3]
- D. filename[:9]
Answer: B
Explanation:
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:] .
NEW QUESTION # 42
Which symbol begins a single-line comment in Python?
- A. // double forward slash
- B. * asterisk
- C. # pound symbol
- D. % percent symbol
Answer: C
Explanation:
In Python, a single-line comment begins with the # symbol.
Example:
# This is a comment
print( " Hello " )
Python ignores the comment when the program runs. The Python documentation identifies # as the symbol used for comments.
Therefore, the correct answer isB. # pound symbol.
NEW QUESTION # 43
How does Jupyter Notebook function as a development environment?
- A. Cell-based interactive coding platform
- B. Command-line terminal replacement
- C. Framework for compiling Python into standalone applications
- D. Code editor limited to static text editing
Answer: A
Explanation:
Jupyter Notebook works as acell-based interactive coding platform. A notebook is made up of cells, and code can be written and executed inside those cells.
For example, a code cell may contain:
x = 10
print(x)
When the cell is run, the output appears directly below the cell.
The Jupyter Notebook documentation explains that notebooks consist of a sequence of cells, and the contents of a cell can be executed using keyboard shortcuts or the toolbar button.
Jupyter Notebook is not simply a command-line terminal replacement, not limited to static text editing, and not mainly a framework for compiling Python into standalone applications.
Therefore, the correct answer isB. Cell-based interactive coding platform.
NEW QUESTION # 44
......
Foundations-of-Programming-Python PDF Dumps Extremely Quick Way Of Preparation: https://www.passcollection.com/Foundations-of-Programming-Python_real-exams.html
Download Foundations-of-Programming-Python Dumps (2026) - Free PDF Exam Demo: https://drive.google.com/open?id=1TX140Qu5E3P9eO6c9MlifGiIOw5lAJ0I

