IT 117: Intermediate Scripting
Answers to Questions on Final Exam - Even Columns

Each of the following questions are worth 3 points.

  1. What do you call the special method that is used to create an object?
    constructor
  2. What does Python call the process by which an object can be stored as a file on disk?
    pickling
  3. What is the name of the method that will return a string representation of an object?
    __str__
  4. Write the Python expression you would use to get a list of all entries in your current directory.
    os.listdir('.')
  5. Can you rewrite any recursive function using a loop instead of recursion?
    yes
  6. What do call the situation where a function calls itself directly?
    direct recursion
  7. Can the key in a dictionary entry be any data type?
    no. you can only use values that are immutable, that cannot be changed
  8. What do you call an object that is created from a class?
    an instance
  9. What string value does Python think of as False in an if statement?
    the empty string
  10. What are three things are found in a regular expression?
    ordinary characters, meta-characters, character classes
  11. What is a greedy match?
    a match with the greatest number of characters
  12. What do you call the special class of methods that do things like provide a string representation of an object and convert one data type into another?
    magic methods
  13. In Python what must the special method used to create an object be named?
    __init__
  14. What are the entries in a dictionary?
    key - value pairs
  15. To create a new kind of object, what must you define?
    a class
  16. Write a Python expression to see if the set s1 contains 5.
    5 in s1
  17. What must a recursive algorithm have in order to work?
    a condition under which the recursion will stop
  18. What is the name of the module that allows you to interact with the operating system?
    os
  19. What access mode would you use to open a pickled file for reading?
    rb
  20. What do you call the situation where a function calls another function, which then calls the original function?
    indirect recursion
The following questions require you to write code.
Each question is worth 10 points.
  1. Write the RECURSIVE function fibonacci which has one parameter named number.
    The function should compute the value of the nth Fibonacci number using recursion.
    The Fibonacci numbers are computed as follows
    F(1) = 1
    F(2) = 1
    F(n) = F(n - 1) + F(n - 2)
    def fibonacci(number):
        if number == 1 or number == 2:
            return 1
        else:
            return fibonacci(number - 1) + fibonacci(number - 2)
  2. Define the class Book with the following attributes
    title
    author
    publisher
    These attributes must be HIDDEN and have accessors.
    You MUST define a __str__ method.
    class Book:
        def __init__(self, title, author, publisher):
            self.__title     = title
            self.__author    = author
            self.__publisher = publisher
            
        def get_title(self):
            return self.__title
            
        def get_author(self):
            return self.__author
        
        def get_publisher(self):
            return self.__publisher
        
        def __str__(self):
            return self.get_title() + ' by ' + self.get_author() + ', ' + self.get_publisher()
  3. Define the function file_count, which takes no parameter.
    This function should return the number of FILES, not directories, inside the current directory.
    You will need to use os.path.isfile in this script.
    def file_count():
        count = 0
        entries = os.listdir()
        for entry in entries:
            if os.path.isfile(entry):
                count += 1
        return count
  4. Write the regular expression which matches each line below and extracts the name and score from each line.
    Joe     86
    Sally   100 
    Ben     78 
    I am looking for a SINGLE regular expression that matches all lines
    (\w+)\s+(\d+)