Docsity

Python Conditional Statements and Loops, Exams of Advanced Education

Toggle sidebar
bg1
Python Exam With 100% Correct And
Verified Answers
The following code represents a ________________ if structure.
if a >= 1:
if a == 1: - Correct Answer-nested
Which line of code sets the value of the variable take_home to 500?
take_home == 1500 / (1 + 2)
take_home = 250 * 6 / 1 + 2
take_home = 250 * (5 % 3)
take_home = 250 * 5 - 3 - Correct Answer-take_home = 250 * (5 % 3)
Given the code
message = "Your percentage score of "
percentage = "95.6"
letter_grade = "% is an A grade."
What is the correct code to display the following result?
Your percentage score of 95.6% is an A grade. - Correct Answer-print(message +
percentage + letter_grade)
Given the following parallel lists, which piece of code will print out each student and
their major?
students = ["Jimmy", "Ravi", "Lauren"]
majors = ["Accounting", "Finance", "ISA"] - Correct Answer-for i,student in
enumerate(students):
print(student + ", Major: " + majors[i])
What will be displayed by the following code?
age = 18
score = 550
income = 60000
if age >= 20 or (score > 600 and income > 50000):
print("approved")
elif age > 18 and age < 20:
print("approved with condition")
else:
print("not approved") - Correct Answer-not approved
The while keyword takes a condition just like which other keyword? - Correct Answer-if
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Python Conditional Statements and Loops and more Exams Advanced Education in PDF only on Docsity!

Python Exam With 100% Correct And

Verified Answers

The following code represents a ________________ if structure. if a >= 1: if a == 1: - Correct Answer-nested Which line of code sets the value of the variable take_home to 500? take_home == 1500 / (1 + 2) take_home = 250 * 6 / 1 + 2 take_home = 250 * (5 % 3) take_home = 250 * 5 - 3 - Correct Answer-take_home = 250 * (5 % 3) Given the code message = "Your percentage score of " percentage = "95.6" letter_grade = "% is an A grade." What is the correct code to display the following result? Your percentage score of 95.6% is an A grade. - Correct Answer-print(message + percentage + letter_grade) Given the following parallel lists, which piece of code will print out each student and their major? students = ["Jimmy", "Ravi", "Lauren"] majors = ["Accounting", "Finance", "ISA"] - Correct Answer-for i,student in enumerate(students): print(student + ", Major: " + majors[i]) What will be displayed by the following code? age = 18 score = 550 income = 60000 if age >= 20 or (score > 600 and income > 50000): print("approved") elif age > 18 and age < 20: print("approved with condition") else: print("not approved") - Correct Answer-not approved The while keyword takes a condition just like which other keyword? - Correct Answer-if

Which of the following is a valid variable name in Python?: 1st place None of these answers 1st_place and - Correct Answer-None of these answers What is printed by the following program? my_total = 0 do_not_stop = True while do_not_stop == True: my_total += 5 if my_total > 10: do_not_stop = False print(my_total) - Correct Answer- A _____________ is Boolean variable often used to control the operation of loops. - Correct Answer-flag What code should be added to the end of the following code to display a single message if the city is not found in the list? city_input = input("Input a city") possible_cities = ["Detroit", "Cincinnati", "Atlanta"] city_is_there = False for city in possible_cities: if city_input == city: print("The city is there!") city_is_there = True break - Correct Answer-This code at the same level of indention as the for in the question above. if city_is_there == False: print("The city is not there") How many lines of output will be printed by the following code? sum = 0 for i in range(1,3): for j in range(1,4): sum += i * j print(sum) - Correct Answer- The index of the first element in a Python list is ____________. - Correct Answer-

The program asks the user for their current age, determines the optimal savings strategy, and prints the recommendation. It will provide the user with an error message if an optimal savings strategy cannot be determined. input_failure = False # Flag variable to check that savings_strategy was set

Ask for user's current age in years and convert the variable type if necessary

current_age = ??? Answer A ???

If current age is between 18 and 35, inclusive - strategy is Aggressive

if current_age >= 18 and current_age <= 35: savings_strategy = "Aggressive"

If current age is between 36 and 55, inclusive - strategy is Moderate

??? Answer B ??? savings_strategy = " - Correct Answer-A) int(input("What is your current age in whole years?")) B) elif current_age >= 36 and current_age <= 55: C) savings_strategy = "Conservative" D) input_failure = True E) input_failure == False You want to create a program that will allow the user to calculate the approximate annual fuel cost based on what type of vehicle is being driven and how many miles they drive in a year. The program will prompt the user for their vehicle type. Supported responses will be: car; truck; van; wagon; SUV; and motorcycle. The program will then ask the user for the average number of miles they drive each year, and fractional miles should be accepted. Each vehicle has an associated average rate of fuel consumption, measured in miles per gallon (or mpg). The rates are 39 mpg, 31 mpg, 22 mpg, 26 mpg, 23 mpg, and 65 mpg, respectively. Using this fuel consumption rate, the yearly mileage, and the average price of gas—assumed be constant at $2.35/gallon—the program will then calculate and display the approximate annual fuel cost using the formula: (annual gas cost) = (gas price) * (yearly miles) / (miles per gallon) gas_pri - Correct Answer-A) float(input("How many average miles do you drive each year?")) B) enumerate(auto_types) C) user_auto == an_auto D) gas_price * yearly_miles / miles_per_gallons[i] E) + str(round(annual_gas_cost, 2)) The following incomplete program will calculate freight charges. The freight company charges customers based on total cargo weight. The first 70 pounds of cargo cost $ per pound. Additional cargo above 70 pounds costs $3 per pound. The user will enter the weight of the cargo in pounds. The program will then calculate the freight charge and display the freight charge in dollars and cents format.

Define variable for cargo weight and ask user for value to the nearest pound.

cargo_weight = ??? Answer A ???

Calculate freight charge based on cargo weight

if ??? Answer B ??? : # Check to see if user entered 70 or less freight_charge = ??? Answer C ??? # If so, calculate the freight charge accordingly else: # If not freight_charge = ??? Answer D ??? # Calculate the freight charge for > 70 pounds

Display the freight charge in dollars

print("The freight charge is: $" ??? Answer E ??? ) - Correct Answer-A) int(input("Input the cargo weight to the nearest pound.")) B) cargo_weight <= 70 C) cargo_weight * 5 D) 70 * 5 + (cargo_weight - 70) * E) + str(round(freight_charge)) This program is intended to calculate the area of a square. The user enters the length of a side as an integer. The program should calculate the area, store it in the variable called "area", and then display it as follows: "The area of the square is 4" Here is the incomplete program: side = int(input("Enter the length of a side:")) ???Answer X??? # Calculate the area and store it in a variable named area print( ???Answer Y??? ) # Display the resulting area in a message - Correct Answer-X) area = side * side Y) "The area of the square is " + str(area) Another valid answer for X would be area = side ** 2 Note that the answer for Y excludes print and the parentheses as they were in the program. When is 1 equal sign used vs 2 equal signs - Correct Answer-= : is used to assign a variable to a value == : is used for comparison or testing with an if usually what does % do - Correct Answer-calculates the remainder of the division What does enumerate() do? - Correct Answer-takes the index [i] for each value in the parallel lists In the following line of Python code, what is the argument to the input function? user_status = input("How are you today?") - Correct Answer-user_status Which symbol is used to assign a value to a variable in Python? - Correct Answer-= Which Python function can only provide output to the user? - Correct Answer-print()

Which of the following is the correct way to test if i is NOT equal to 5? - Correct Answer- i != 5 Which of the following would be a correct if statement line in Python? - Correct Answer- if i == 7: Which of the following are considered comparison operators? - Correct Answer-<, >, <=, >=, Which answer code is equivalent to the following? if x > 1: if y > 1: z = 10 - Correct Answer-if x > 1 and y > 1: z = 10 The block of code to be executed when an if or elif condition is true MUST: - Correct Answer-be indented Given the following Python conditional expression: pet_color == "black" or pet_color == "brown" and pet == "cat" How can we change this expression to ensure that the condition will only be true for a pet that is a brown or black cat? - Correct Answer-Put parentheses around the pet_color == "black" or pet_color == "brown" conditions When the user enters the number of hours worked as a whole number, which function below should be applied to that input before calculating the gross pay? - Correct Answer-int() What function is used to express the gross pay in dollars and cents format? Assume the variable is called gross_pay. - Correct Answer-round(gross_pay,2) Which type of loop is best to use when you want it to run until some condition is no longer true. - Correct Answer-while Given the code: for n in range (0, 30): print("I love Python!") What is the line print("I love Python!")? - Correct Answer-the body of the loop What keyword can be used to exit a loop early? - Correct Answer-break

When using the range() function in Python, which argument must always be specified? - Correct Answer-stop Given the following code, what is the primary function of the variable i? i= while i < 10: print("Go Redhawks!") i += 1 - Correct Answer-loop counter The body of the loop (the code that executes each time the loop runs) can contain _____________ statements. - Correct Answer-any number of Which type of loop is a better choice for the homework solution, since we know the number of times the loop will run? - Correct Answer-for To allow the cash flow at each time period to be entered in dollars and cents, what is the best Python data type to use for the variable? - Correct Answer-float In Python, how do you end the body of any loop? - Correct Answer-the line of code after the loop body is not indented Which of the following items must occur before entering the loop to perform the net present value calculations in the homework program? - Correct Answer-Initialize net present value variable to 0 What would be printed by the following code? fruits = ["apple", "banana", "cherry", "strawberry"] print(fruits[1 : 3]) - Correct Answer-banana, cherry Which statement would we use to stop a for loop if a match is found in our list? - Correct Answer-break Given the code: ice_cream = ["Vanilla", "Chocolate", "Strawberry", "Butter Pecan"] ___ x ___ ice_cream: print(x) What should be in place of the blanks that would allow us to loop through the items in the ice_cream list? - Correct Answer-for & in You access list items by referring to the _____________________. Correct! - Correct Answer-index number Which function allows us to determine the index of each element in a list? - Correct Answer-enumerate