IT 116: Introduction to Scripting
Answers to Questions on Final Exam - Odd Columns

Each of the following questions are worth 3 points.

  1. What are the only values that a boolean expression can have?
    True and False
  2. If you want to stop a Python script running on Unix, what do you do?
    Control C
  3. What do you call the functions that are always available in Python and DO NOT have to be imported?
    built-in functions
  4. What do you call a variable that is defined inside a function?
    a local variable
  5. Name the four types of expressions.
    literals, variables, calculations, function calls
  6. What must you create to work with a file in Python?
    a file object
  7. What is the Python function that creates the thing you need to work with a file?
    open
  8. What is a syntax error?
    an error caused by code that does not follow the rules of the Python language
  9. What is a logic error?
    an error that causes the code to give incorrect results
  10. What is a runtime error?
    when a value in a Python statement causes the statement to fail
  11. What permissions does an account need on a Python script to be able to run it without typing the name of the Python interpreter on the command line? I want the NAMES of the permission.
    read and execute permission
  12. What do you all a collection of things stored one right after another?
    a sequence
  13. What is a list?
    a sequence object that can be changed
  14. What is a tuple?
    a sequence object that cannot be changed
  15. What operators work with lists?
    + and *
  16. Write an assignment statement that creates a list of the first 5 integers and assigns it to the variable l1.
    l1 = [1, 2, 3, 4, 5]
  17. Write a Python expression that evaluates to True if the list l1 contains the value 4.
    4 in l1
  18. Write a Python statement to add 6 to the list l1.
    l1.append(6)
  19. Write a Python statement that sorts the list l1.
    l1.sort()
  20. If l1 is a list, what is l2 after the following statement is executed?
    l2 = l1
    a new list variable pointing to the same list as l1
The following questions require you to write code.
Each question is worth 10 points.
  1. Define the function decimal_list_from_file which takes one parameter, file, a file object.
    The file is a list of decimal numbers, one to a line, that looks like this
    56.37
    45.133
    29.8145
    The function should convert each line to a float and add it to the list.
    It should return that list.
    def decimal_list_from_file(file):
        list = []
        for line in file:
            list.append(float(line))
        return list
  2. Define the function increment_list which takes two parameters, named list and increment.
    list points to a list of integers and increment is an integer.
    The function should use a for loop to change the list by adding the value of increment to each element.
    So if the original list was
    [1,2,3]
    and the increment was 2, the list would become
    [3,4,5]
    def increment_list(list, increment):
        for index in range(len(list)):
            list[index] += increment
  3. Define the function average_list which takes one parameter, num_list, a list of integers.
    The function should use a loop to add all the numbers and return the average.
    This average should be rounded to two decimal points.
    def average_list(num_list):
        total = 0
        for num in num_list:
            total += num   
        return round(total/len(num_list), 2)
  4. Define the function count_chars.
    This function takes two parameters, s, a string, and char, a character.
    This function should count the number of times char appears in s and return that value.
    If the function were called with the arguments s and "o" and s was the string "room" it should return 2.
    def count_chars(s, char):
        letter_count = 0
        for ch in s:
            if ch  == char:
                letter_count += 1
        return letter_count