Chapter 5 Quiz
The stack is available when an exception occurs. It displays a list of
just the functions that were called prior to the exxception
What line number of the following code contains an error and what type of error is it? 1. def sales_tax(amt) 2. sale = amt + (amt * .06) 3. return sale 4. 5. def main(): 6. print("Welcome to the 6% tax calculator!\n") 7. total = int(input("Please enter the total amount: ")) 8. print("The total amount after tax is: ", sales_tax(total))
line 1, syntax error
What line number of the following code contains an error and what type of error is it? 1. count = 1 2. while count <= 4: 3. print(count, end = " ") 4. i *= 1 5. print("\nThe loop has ended.")
line 4, runtime error
When you used the IDLE debugger, you start by setting a breakpoint
on a statement before the statement you think is causing the bug
What type of error throws an exception that stops execution of the program?
runtime
Which type of error prevents a program from compiling and running?
syntax
Which of the following is not true about top-down coding and testing?
You should always start by coding and testing the most difficult function
A programmer created this code for a client: def divide(numerator, denominator): quotient = numerator / denominator return quotient def main(): numerator = int(input("Enter the numerator: ")) denominator = int(input("Enter the denominator: ")) quotient = divide(numerator, denominator) print("The quotient is ", quotient) if __name__ == "__main__": main() The programmer tested this code with the following three sets of data: 1. numerator = 9, denominator = 3 2. numerator = -50, denominator = 5 3. numerator = 389, denominator = -26 The results were: 1. The quotient is 3.0 2. The quotient is -10.0 3. The quotient is -14.961538461538462 However, once the program was in use, the client reported that sometimes the program crashed. Can you explain why.
Programmer didn't test for a 0 value in the denominator
Code Example 5-2 1. # This application displays a student's score after a 5-point curve 2. 3. def display_info(fname, lname, score): 4. print("Hello, " , fname, " " , Lname) 5. print("Your score on this exam is ", score) 6. score = score + 5 7. 8. def main(): 9. first = input("first name: ") 10. last = input("last name: ") 11. grade = input("exam score: ") 12. display_info(last, first, score) 13. 14. # if started as the main module, call the main function 15. if __name__ == "__main__": 16. main() Assuming the coding errors have been fixed, what is the logic error in this program?
The curve is calculted after the score has been displayed
Code example: 1. # This application displays a student's score after a 5-point curve 2. 3. def display_info(fname, lname score): 4 . print("Hello, ", fname, " ",Lname) 5. print("Your score on this exam is ", score) 6. score = score + 5 7. 8. def main ( ): 9. first = input("first name: ") 10. last = input("last name: ") 11. grade = input("exam score: ") 12 display_info(last, first, score) 13 14 # if started as the main module, call the main function 15. if __name__ == "__main__": 16. main( ) What is the error in the main ( ) function?
The input statement on line 11 gets a variable named grade but send in a undefined variable named score on line 12
Code example: 1. # This application displays a student's score after a 5-point curve 2. 3. def display_info(fname, lname score): 4 . print("Hello, ", fname, " ",Lname) 5. print("Your score on this exam is ", score) 6. score = score + 5 7. 8. def main ( ): 9. first = input("first name: ") 10. last = input("last name: ") 11. grade = input("exam score: ") 12 display_info(last, first, score) 13 14 # if started as the main module, call the main function 15. if __name__ == "__main__": 16. main( ) What is the first error in the display_info( ) function?
The variable Lname on line 4 does not exist
Code Example 5-2 1. # This application displays a student's score after a 5-point curve 2. 3. def display_info(fname, lname, score): 4. print("Hello, " , fname, " " , Lname) 5. print("Your score on this exam is ", score) 6. score = score + 5 7. 8. def main(): 9. first = input("first name: ") 10. last = input("last name: ") 11. grade = input("exam score: ") 12. display_info(last, first, score) 13. 14. # if started as the main module, call the main function 15. if __name__ == "__main__": 16. main() What is error on line 6?
The variable score has been input as a string so it must be converted to an int or float
Give the following code and its output: 1. discount_rate = .1 2. item = 89.95 3. discount = item * discount_rate 4. print("The discount on this item is $", discount)) Output: The discount on this item is $ 8.995000000001 Which of the following would produce a user-friendly correct result?
change line 4 to: print("The discount on this item is $", round(discount, 2))
Given the following code, if the user worked 45 hours at $10.00/hour, the output is as shown below. 1. def main(): 2. hours = float(input("How many hours did you work? ")) 3. rate = float(input("What is your hourly rate? ")) 4. if (hours > 40) and (rate < 15): 5. pay = (hours * rate) + (hours - 40 * rate * 1.5) 6. else: 7. pay = hours * rate 8. print("Your pay is: $ ", round(pay, 2)) Output:Your pay is: $ -105.0 Which line should be changed to fix this logic error?
change line 5 to: pay = (40 * rate) + ((hours - 40) * rate * 1.5)
Code Example 5-1 1. count = 1 2. item_total = 0 3. item = 0 4. while count < 4: 5. item = int(input("Enter item cost: ")) 6. item_total += item 7. count += 1 8. average_cost = round(item_total / count) 9. print("Total cost:", item_total, "\nAverage cost:", average_cost) If the user enters 5,10, and 15 at the prompts, the output is: Total cost: 30 Average cost: 8 Which line should be changed to fix this logic error?
change line 8 to average_cost = round(item_total / (count - 1))
When you trace the execution of a program, you insert print( ) functions at key points in the program. It makes sense for these functions to do all but one of the following. Which one is it?
displays the values of the global constants used by the function
To test the functions of a module from the IDLE shell, you
import the module and then call any function from the IDLE shell
Which of the following is not a common type of syntax error?
invalid variable names
When the IDLE debugger reaches a breakpoint, you can do all but one of the following. Which one is it?
view the values of all variables that you've stepped through