Docsity

Python Coding Practice Questions, Exams of Nursing

Toggle sidebar
bg1
PYTHON exam(questions with 100%
correct answers)(13)
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])
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Python Coding Practice Questions and more Exams Nursing in PDF only on Docsity!

PYTHON exam(questions with 100%

correct answers)(13)

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

The following program is intended to display True Love. The program contains an error. Which answer best describes the problem with the program? a = True b = "Love" print(a + " " + b) - correct answer ✔✔A string cannot be concatenated with a Boolean Comments that span multiple lines (block comments) must include ______ on lines before and after the comment. - correct answer ✔✔''' Which data type in Python is the best to use for a variable that will have the value 3.14159? - correct answer ✔✔float Which of the following can be used to assign 2 percent to variable discount?: discount =. discount = 2% discount = 2 * 100 None of these answers - correct answer ✔✔none of these answers If the characters entered by the user are not numbers, what happens when the following line of code runs? user_age = int(input("What is your age?")) - correct answer ✔✔A syntax error occurs Given the following list, which statement would print New York? city_list = ["Atlanta", "New York", "Baltimore", "Phoenix", "Dallas"] - correct answer ✔✔print(city_list[1])

If variable x has the value 3.99, which of the following will set variable y equal to 3? (Assume any math library functions have already been imported.) y = trunc(x) y = round(x - 1) All of these answers y = floor(x) - correct answer ✔✔None of these answers Which operator is used for exponentiation in Python? - correct answer ✔✔** his program looks at the user's current age and suggests an appropriate retirement savings strategy: "Aggressive" for users between 18 and 35, inclusive; "Moderate" for users between 36 and 55, inclusive; and "Conservative" for users over the age of 55. 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"

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 statements would result in the value of the variable my_num being the same as the value of my_num squared? - correct answer ✔✔my_num = my_num** What is displayed when the following code is run? a_dog = "fido" two_dogs = a_dog + 1 print (two_dogs) - correct answer ✔✔fido The following code is intended to add one to the user's current age and then display it. What is the error in the code? current_age = input("Please enter your current age:") current_age = current_age + 1 print("Your age next year will be " + str(current_age)) - correct answer ✔✔You must convert current_age to a number before adding 1 to it Given the following line of code: labor_hours = float(input("How many labor hours were worked to produce the output?"))

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"]

In the homework assignment, what is the best way to confirm the user provided valid inputs for the product code and quantity? - correct answer ✔✔a check is not required