IT 116: Introduction to Scripting
Answers to Midterm Questions - Odd Columns

Each of the following questions is worth 4 points.

  1. What do you call one or more lines of code that perform a complete action?
    a statement
  2. What is an expression?
    anything that can be turned into a value
  3. What is the result of the following calculation in Python's interactive mode?
    7 // 2
    3
  4. What is the result of the following calculation in Python's interactive mode?
    7 % 2
    1
  5. What arithmetic operators have the highest precedence (parentheses are NOT operators)?
    **
  6. Write the Python boolean literals.
    True and False
  7. If you typed the following in Python's interactive mode, what would the result be?
    not False
    True
  8. If you typed the following in the Python interactive mode, what would the result be?
    True and False
    False
  9. If you typed the following in Python's interactive mode, what would the result be?
    True or False
    True
  10. If you want to stop a Python script running on Unix, what do you do?
    Control C
  11. Will a while loop with the following header ever stop?
    while True:
    no
  12. What do you call the functions that are always available in Python and DO NOT have to be imported?
    built-in functions
  13. What do you call a variable that is defined inside a function?
    a local variable
  14. If a variable is defined inside a function, can another function see the value of that variable?
    no
  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".
    Assume that score already has a value.
    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 the variable num before entering the loop.
    num will be tested tested at the start of the while loop.
    num = 5
    while num < 15:
       print(num)
       num += 5
  3. Write a for loop that prints the odd numbers from 1 to 15.
    You MUST use the range function with 3 arguments.
    for num in range(1, 16, 2):
       print(num)
  4. Define the function print_grade that takes one parameter, score.
    This script should contain a SINGLE if statement which prints the letter grade for each range of scores according to the following table
    A    >= 90
    B    >= 80
    C    >= 70
    D    >= 60
    F    <  60