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

Each of the following questions are worth 3 points.

  1. What do you call the functions that are always available in Python and DO NOT have to be imported?
    built-in functions
  2. What is a syntax error?
    an error caused by code that does not follow the rules of the Python language
  3. If you typed the following in Python's interactive mode, what would the result be?
    not False
    True
  4. What is a runtime error?
    when a value in a Python statement causes the statement to fail
  5. If you want to stop a Python script running on Unix, what do you do?
    Control C
  6. Can a return statement return more than one value?
    yes
  7. What is the Python function that creates the thing you need to work with a file?
    open
  8. 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
  9. Name the four types of expressions.
    literals, variables, calculations, function calls
  10. What is the name of the special line we use to run a Python program without directly calling the interpreter?
    the hashbang line
  11. What is a logic error?
    an error that causes the code to give incorrect results
  12. What do you call a variable that is defined OUTSIDE any function?
    a global variable
  13. What do you call a loop that does not stop?
    an infinite loop
  14. What Python statements should you put inside the try block of a try/except statement.
    any statement that can cause a runtime error
  15. 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?
    read and execute permission
  16. What must you create to work with a file in Python?
    a file object
  17. Can there be different except clauses for different exceptions?
    yes
  18. Can a function that is given a list as a parameter and does not have a return statement change the list given as a parameter?
    yes
  19. If l1 and l2 are list variables, write a Python statement that creates the new list l3 that consists of all the elements in l1 followed by all the elements in l2.
    l3 = l1 + l2
  20. 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. Define the function operators_count, which takes a single parameter, str, a string.
    The function should count all the characters in a string that are the arithmetic operators +, -, *, / and return that count.
    You will need to use the list literal ["+", "-", "*", "/"] You should use the following algorithm
    set count to 0
       loop through the string
          if a character is in the list of operators:
             increment count
       return count
    			
    def operators_count(str):
    count = 0
    for ch in str:
        if ch in ["+", "-", "*", "/"]:
            count += 1
    return count
  2. Define the function count_positive which takes one parameter, num_list, a list of integers.
    The function should set a counter to 0 then loop through the list and increment the counter if the number is greater than 0.
    It should return the count.
    def count_positive(num_list):
    count = 0
    for num in num_list:
        if num > 0:
            count += 1
    return count
  3. 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
  4. 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