IT 116: Introduction to Scripting
Answers to Midterm Questions - Even 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
    1
  2. If you want to stop a Python script running on Unix, what do you do?
    Control C
  3. If you typed the following in Python's interactive mode, what would the result be?
    not False
    True
  4. Will a while loop with the following header ever stop?
    while True:
    no
  5. What do you call one or more lines of code that perform a complete action?
    a statement
  6. Where do parameters get their values?
    from the corresponding argument in the function call
  7. What is an expression?
    anything that can be turned into a value
  8. If a variable is defined inside a function, can another function see the value of that variable?
    no
  9. If you typed the following in the Python interactive mode, what would the result be?
    True and False
    False
  10. What do you call the functions that are always available in Python and DO NOT have to be imported?
    built-in functions
  11. What arithmetic operators have the highest precedence (parentheses are NOT operators)?
    **
  12. If you typed the following in Python's interactive mode, what would the result be?
    True or False
    True
  13. What is the result of the following calculation in Python's interactive mode?
    7 // 2
    3
  14. What do you call a variable that is defined inside a function?
    a local variable
  15. Write the Python boolean literals.
    True and False
The following questions require you to write code.
Each question is worth 10 points.

  1. 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
  2. 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)
  3. 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
  4. 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")