Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Chapter 05: How to test and debug a program Murach\'s Python Programming No Answ

ID: 3749986 • Letter: C

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 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 you’ve 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 you’ve 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 didn’t account for a numerator that was larger than the denominator.

b.

Programmer didn’t round results to a specific number of decimal places.

c.

Programmer didn’t 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

a.

exceptional

c.

runtime

b.

syntax

d.

logic

Explanation / Answer

Multiple Choice

1. b (syntax errors)

syntax errors can be found out during compile time itself .So a syntax error prevents progarm from compiling and running.

2. c( run time)

Exceptional errors can be forseen before hand and we can include logic to handle it.So when an exception occurs, control moves to these exception handle logic.If exceptions are not handled , it throws an exception and stops the program .In that case it becomes a runtime error.

Run time errors stops the program execution.

3.b

In Phyton , we don't use much of paranthesis.Correct indendation is used to define scope of statements ulike java,c,c++ where paranthesis defines it.So forget to close a paranthesis is not a common syntax error in phyton.

4.c

While testing , we are checking if the program is working as expection.So need to test for valid ,invalid ,boudary values and check if we are getting expected results.

5.c

Values of global constants doesn't chamge.So no point in displaying it.

6.c

Top down approach is a way by which you code and test the top level things before breaking down it further.No where it is mentioned we should be coding and testing the difficult things first.

7.a

To test a function in a modeule , we run the module with such inputs that it calls the desired function

8.b

When we put the breakpoint on the line causing a bug.We won't be able to trace it exactly why the bug is caused.So better to put a break point on the line before it,nalyse the variable values and chk why the bug is caused.

9.d

We cannot see the values of variables that we have stepped through.Only values of variables in scope can be seen.

10.a

lists out the method calls from the point when the application was started to the point where the exception was thrown.

11.b

: missing in function definition

12.d

round function , wraps the argument to 2 decimal places

13.c

Possibility of 0 in denominator has to be considered.

14.a

score not defined

15.d

No error, but this leads to change of value of score in main also

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote