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

Each of the following questions are worth 3 points.

  1. What are the entries in a dictionary?
    key - value pairs
  2. Can the key in a dictionary entry be any data type?
    no. you can only use values that are immutable, that cannot be changed
  3. What string value does Python think of as False in an if statement?
    the empty string
  4. Write a Python expression to see if the set s1 contains 5.
    5 in s1
  5. What is the name of the module that allows you to interact with the operating system?
    os
  6. Write the Python expression you would use to get a list of all entries in your current directory.
    os.listdir('.')
  7. What is the name of the module that allows you to interact with the Python interpreter?
    sys
  8. Write the Python expression that gives the value of the first command line argument.
    sys.argv[1]
  9. What are three things are found in a regular expression?
    ordinary characters, meta-characters, character classes
  10. What does the . (dot) in a regular expression match?
    one occurrence of any character, except the newline
  11. What does the + in a regular expression match?
    1 or more occurrences of the character that comes before it
  12. Write the regular expression that is equivalent to * on the Unix command line.
    .*
  13. To create a new kind of object, what must you define?
    a class
  14. What do you call the special method that is used to create an object?
    constructor
  15. In Python what must the special method used to create an object be named?
    __init__
  16. What do you call an object that is created from a class?
    an instance
  17. 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
  18. What is the name of the method that will return a string representation of an object?
    __str__
  19. What do you call a function that calls itself?
    a recursive function
  20. What two things must be true about a recursive algorithm for it to work?
    there must be a condition under which recusion will stop
     the recursive call must approach this condition
     			
The following questions require you to write code.
Each question is worth 10 points.
  1. Write the regular expression you would use to both match and extract the value of an IP address from lines like the following
    205.236.184.22 
    2.2.3.4
    13.26.34.202
    ...
    (\d{1,3}\.\d{1,3}\.\d{1,3})
  2. Define the function python_count that takes a single parameter, dir_path, a path to a directory.
    The script should create a list of the entries in dir_path.
    Then it should loop through this list and count the number of entries with a ".py" extension.
    This should be done using the string method endswith.
    The function should return this count.
    def python_count(dir_path):
    file_list = os.listdir(dir_path)
    count = 0
    for file in file_list:
        if file.endswith('.py'):
            count += 1
    return count
  3. Define the class Employee with the following attributes
    first_name
    last_name
    id
    All attributes must be hidden and must have accessor methods.
    There must also be a method which will return a string representation of the object which should contain the value of all fields.
    class Employee:
    def __init__(self, first_name, last_name, id):
        self.__first_name = first_name
        self.__last_name  = last_name
        self.__id         = id
        
    def get_first_name(self):
        return self.__first_name
    
    def get_last_name(self):
        return self.__last_name
    
    def get_id(self):
        return self.__id 
    
    def __str__(self):
        return self.__first_name + " " + self.__last_name + ", ID: " + self.get_id()
  4. Define the RECURSIVE function count_down which takes a single parameter, num, an integer.
    The function should print the numbers, starting with num, down to 0, one number to a line.
    For example, when you call count_down(5), the output should be
    5
    4
    3
    2
    1
    0
    def count_down(num):
        print(num)
        if num > 0:
            count_down(num - 1)