In-Class Questions and Video Solutions

Lecture 7

  1. Black Box and Glass Box Testing

    With the below implementation, is the test set “n = 4 | n = -4 | n = 5” path complete?

    def is_even(n):
     """ 
     Returns True if a number is even
     and False if not 
     """
     if n > 0 and n % 2 == 0:
     return True
     elif n < 0 and n % 2 == 0:
     return True
     else: 
     return False
    

     Yes  check
     No  close
    Check

With the above implementation, which value for n is incorrectly labeled by is_even?

 n is very large (and positive)  close
  n is very small (and negative)  close
 n is 0  check
Check Show Solution

  1. Errors

    Below is a piece of code and an error shown when running it. What is the problem?

    L = 3
    for i in range(len(L)):
     print(i)
    
    ERROR MESSAGE:
    
     File "C:/Users/Ana/.spyder2-py3/temp.py", line 2, in 
     for i in range(len(L)):
    
    TypeError: object of type 'int' has no len()
    

     You are not allowed to name an integer with the variable name L  close
     range is not allowed to have an expression inside its parentheses  close
     You are not allowed to call len on an integer  check
     You are not allowed to print the loop variable i  close
    Check Show Solution

  2. Exceptions

    If the user enters “twenty” in the code below what does the program do?

    try:
     n = int(input("How old are you? "))
     percent = round(n*100/80, 1)
     print("You've gone through", percent, "% of your life!")
    except ValueError:
     print("Oops, must enter a number.")
    except ZeroDivisionError:
     print("Division by zero.")
    except:
     print("Something went very wrong.")
    

     prints "You've gone through 25.0 % of your life!"  close
     prints "Oops, must enter a number."  check
    Check

If the user enters “0” in the code above what does the program do?

 prints "You've gone through 0.0 % of your life!"  check
 prints "Division by zero."  close
Check Show Solution

Course Info
Learning Resource Types
assignment Problem Sets
notes Lecture Notes
theaters Lecture Videos
assignment_turned_in Programming Assignments with Examples