Question: Chapter 05: How to test and debug a program Murach's Python Programming No Answers MULTIPLE CHOICE 1. Which type of error prevents a program from

Chapter 05: How to test and debug a program

Murach's Python Programming

No Answers

MULTIPLE CHOICE

1. Which type of error prevents a program from compiling and running?

a.

exceptional

c.

runtime

b.

syntax

d.

logic

2. Which type of error throws an exception that stops execution of the program?

a.

exceptional

c.

runtime

b.

syntax

d.

logic

3. Which of the following is not a common type of syntax error?

a.

forgetting a colon

c.

invalid variable names

b.

forgetting to close a parentheses

d.

improper indentation

4. When you plan the test runs for a program, you should do all but one of the following. Which one is it?

a.

list the valid entries for each test run

b.

list the invalid entries and unexpected user actions for each test run

c.

list the expected exceptions for each test run

d.

list the expected results for each test run

5. 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?

a.

display the name of the function that the print() function is in

b.

display the values of the local variables in the function

c.

display the values of the global constants used by the function

d.

display the values of the global variables used by the function

6. Which of the following is not true about top-down coding and testing?

a.

You start with just a few functions and the code in the main() function that calls those functions.

b.

You add the code for a few functions at a time, including the code in the main() function that calls those functions.

c.

You should always start by coding and testing the most difficult functions.

d.

Top-down testing makes debugging easier because you know that any errors are caused by the code youve just added.

7. To test the functions of a module from the IDLE shell, you

a.

run the module with varying input values

b.

run the module and then call any function from the IDLE shell

c.

import the module and then call any function from the IDLE shell

d.

call any function in the module by using the default namespace

8. When you use the IDLE debugger, you start by setting a breakpoint

a.

on a comment line

b.

on the statement you believe is causing the bug

c.

on a statement before the statement you think is causing the bug

d.

on the function definition for the function that you think is causing the bug

9. When the IDLE debugger reaches a breakpoint, you can do all but one of the following. Which one is it?

a.

step through the program one statement at a time

b.

run the program until the next breakpoint is reached

c.

view the values of the local variables that are in scope

d.

view the values of all the variables that youve stepped through

10. The stack is available when an exception occurs. It displays a list of

a.

the functions that have been called since the program started

b.

just the functions that were called prior to the exception

c.

all the local variables used by the program and their values

d.

all the local and global variables used by the program and their values

11. 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 amt

4.

5. def main():

6. print("Welcome to the 6% tax calculator! ")

7. total = int(input("Please enter the total amount: "))

8. print("The total amount after tax is: ", sales_tax(total))

a.

line 1, runtime error

c.

line 3, runtime error

b.

line 1, syntax error

d.

line 8, logic error

12. Given 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.995000000000001

Which of the following would produce a user-friendly correct result?

a.

change line 1 to: discount_rate = 10%

b.

change line 2 to: item = int(89.95)

c.

change line 3 to: discount = int(item * discount_rate)

d.

change line 4 to: print("The discount on this item is $", round(discount, 2))

Continued on the next page

13. 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?

a.

Programmer didnt account for a numerator that was larger than the denominator.

b.

Programmer didnt round results to a specific number of decimal places.

c.

Programmer didnt test for a 0 value in the denominator.

d.

All three of the above would cause a crash.

Continued on the next page

Continued on the next page

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()

14. Refer to Code Example 5-2: What is the error in the main() function?

a.

The input statement on line 11 gets a variable named grade but sends in an undefined variable named score on line 12

b.

The input statement on line 11 does not define grade as an int

c.

The function call on line 12 should send in fname and lname as arguments, not last and first

d.

There are no errors in main()

15. Refer to Code Example 5-2: What is the error on line 6?

a.

You cannot change the value of score after it has been displayed..

b.

You cannot add a number to itself.

c.

The variable score has been input as a string so it must be converted to an int or float.

d.

There is no error on line 6

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!