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 do you call a a sequence of characters?
    a string
  3. What is an expression?
    anything that can be turned into a value
  4. What is the result of the following calculation in Python's interactive mode?
    (2 + 3) * 5
    25
  5. Write the Python boolean literals.
    True and False
  6. If you typed the following in the Python interactive mode, what would the result be?
    True and False
    False
  7. If you typed the following in Python's interactive mode, what would the result be?
    True or False
    True
  8. What happens in a while loop when the boolean expression after the keyword while becomes false?
    the loop stops
  9. What do you call a loop that does not stop?
    an infinite loop
  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 a variable that is defined inside a function?
    a local variable
  13. What do you call the variables that are listed inside the parentheses of a function header?
    parameters
  14. Where do parameters get their values?
    from the corresponding argument in the function call
  15. What do you call the expressions (values), inside a function call?
    arguments
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 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_cubes, that takes two parameters, min and max, both integers.
    The function should print the cubes of the numbers between min and max.
    The cube of a number is the number raised to the 3rd power.
    The function must use a for loop with the range function.
    def print_cubes(min, max):
      for n in range(min, max +1):
        print(n * n * n)