IT 116: Introduction to Scripting
Answers to Mid-term Questions - Odd Columns

Each of the following questions is worth 4 points.

  1. What is the result of the following calculation in Python's interactive mode?
    7 ** 2
    49
  2. What is the result of the following calculation in Python's interactive mode?
    2 + 3 * 5
    17
  3. Write the Python boolean literals.
    True and False
  4. If you typed the following in Python's interactive mode, what would the result be?
    True and False
    False
  5. If you typed the following in Python's interactive mode, what would the result be?
    True or False
    True
  6. What do you call a loop that does not stop?
    an infinite loop
  7. If you want to stop a Python script running on Unix, what do you do?
    Control C
  8. What happens in a while loop when the boolean expression after the keyword while becomes false?
    the loop stops
  9. Will a while loop with the following header ever stop?
    while True:
    no
  10. Where does the loop variable in a for loop get its values?
    from the list of values after in
  11. What is the augmented assignment operator that multiples the value on its right by the value of the variable on its left and assigns that new value to the variable?
    *=
  12. What do you call a variable that is defined inside a function?
    a local variable
  13. If a variable is defined inside a function, can the code outside all functions see the value of that variable?
    no
  14. What do you call the variables that are listed inside the parentheses of a function header?
    parameters
  15. Where do parameters get their values?
    from the corresponding argument in the function call
The following questions require you to write code.
Each question is worth 10 points.

  1. Write ONE if statement that prints "A" if the variable called score is 90 or more.
    Prints "B" when score is between 89 and 80.
    Prints "C" when score is between 79 and 70.
    Prints "D" when score is between 69 and 60.
    Otherwise it prints "F".
    if score >= 90:
        print("A")
    elif score >= 80:
        print("B")
    elif score >= 70:
        print("C")
    elif score >= 60:
        print("D")
    else:
        print("F")
  2. Write a while loop that prints the numbers 5, 10, 15, each on a separate line.
    You CANNOT use the range function here.
    You also CANNOT use a list.
    You will need to give a starting value to a variable which will be tested in the while loop.
    num = 5
    while num < 15:
        print(num)
        num += 5
  3. Write a for loop that prints the odd numbers from 3 to 11.
    You must use the range function with 3 arguments.
    for num in range(3, 12, 2):
        print(num)
  4. Define a function named is_divisible_by_seven which takes the single parameter named num.
    If num is evenly divided by 7 it should print "True", otherwise it should print "False".
    def is_divisible_by_seven(num):
        if num % 7 == 0:
            print("True")
        else:
            print('False')