COSC 465 Final Exam

  1. You have two hours to complete the exam
  2. Partial credit is awarded on the multiple answer questions as follows. If there are n correct answers for a particular question, and you choose i of them, then you will start with i points. Each incorrect answer you choose will be a 1 point deduction. For example, suppose that you get 3/4 of the correct answers on a multiple answer question and also choose 1 incorrect answer. Then you will receive 3 points for the 3 correct answers and a 1 point deduction for the incorrect answer. Hence you will receive 2 points for that question.
  3. Good luck!

    Multiple Choice Questions (2 points each): Circle only one answer for each of the following problems:

  1. Suppose I have two arrays in Perl named @a and @b and I want to pass them to a function named addArrays that will perform a pairwise addition on the elements of @a and @b. Which of the following function calls will successfully pass the two arrays intact to the function addArrays (i.e., addArrays will have two parameters)?

    1. @c = addArrays(@a, @b);
    2. @c = addArrays(\@a, \@b);
    3. @c = addArrasy($a, $b);
    4. @c = addArrays(&@a, &@b);

  2. Suppose I have the following JavaScript code fragment:
    for (index = 0; index < fruits.length; index++) { ... }
    
    What is a more compact way to write this for loop?

    1. for (index in fruits) { ... }
    2. for each (index fruits) { ... }
    3. for (fruits as index) { ... }
    4. for index in range(0, fruits.length) { ... }

  3. In Perl, why should you use my rather than local to declare a local variable?

    1. my uses static, lexical scoping, whereas local uses dynamic scoping which is more apt to break the program
    2. my indicates that the local variable should receive a reference to a parameter's value whereas local indicates that the parameter's value to should be copied into the local variable, thus leading to expensive copies for parameters that are objects, such as lists or hash tables
    3. local has a loophole that allows the variable to end up in the global namespace once the function exits
    4. local flattens a hash table parameter into a list of the hash table's keys and values whereas my keeps the hash table intact as an object

  4. If the server wishes to return its information to the client in a format that will allow the information to be easily converted to a Javascript object (i.e., associative array), which format should it use to transmit the information:

    1. plain text format
    2. JSON format
    3. XML format
    4. DOM format

  5. What is the purpose of the bless command in Perl's object system?

    1. It associates a package with a module, thus identifying the package as an instance of that module
    2. It associates a subclass with a class, thus allowing methods not defined in the subclass to be located in the superclass
    3. It associates a reference, which is usually a reference to a hash, with a class, thus identifying the object as an instance of that class
    4. It is the command used in place of a constructor to create a new object.

  6. If I want to create a Perl class named Rectangle which is a subclass of GraphicalObject, which of the following sets of commands will properly set up the inheritance hierarchy between Rectangle and GraphicalObject?

    1. class Rectangle extends GraphicalObject;
      
    2. class Rectangle : base Graphical Object;
      
    3. package Rectangle;
      derives GraphicalObject;
      
    4. package Rectangle;  
      use base("GraphicalObject");
      

  7. Why is it so important to validate your data on the server side, even if the web form has its own validation functions?

    1. You may have incorrectly written the validation functions on the web form
    2. The CGI transmission protocol is highly prone to transmit faulty data
    3. Hackers may be able to by-pass the web form by calling your script directly
    4. Hackers may have entered malicious data on the web form that javascript functions cannot detect

  8. What critical feature does Javascript lack that makes it inappropriate for general purpose computing but makes it secure/trusted for client-side computing?

    1. lack of pointers
    2. prototype-instance object system rather than class-instance object system
    3. inability to read/write files
    4. inability to read from stdin or write to stdout

    Multiple Answer Questions (36 points total): The following questions may have one or more correct answers. Circle all answers that are correct.

  9. Which of the following are advantages of prepared SQL queries in PHP?

    1. They execute faster because the query is pre-optimized by the query optimizer
    2. They prevent cross-site scripting attacks
    3. They reduce the amount of data traffic between the server and the client
    4. They provide a way to cache database results on the client side so that repetitive queries can be answered without sending the query to the server.
    5. They speculatively pre-fetch results from the SQL database so that data is potentially already cached in the server's memory when it is needed.
    6. They make it more difficult to carry out injection attacks against the database

  10. Which of the following are server side mechanisms for handling session information in PHP?

    1. cookies
    2. hidden form elements
    3. SESSION table
    4. database storage

  11. What are the advantages of cookies over session variables?

    1. They prevent hackers from corrupting the data that is sent to the server
    2. They can be preserved between web sessions, which means that the user can close their web browser, re-open it, and the data will still be available.
    3. They allow multiple servers to handle the same web session
    4. They allow client-side scripts to access the data, as well as server side scripts.
    5. They only have to be transmitted once per web session rather than with each page request
    6. They allow more session data to be stored because they are stored on the server rather than the client and hence are subject to the server's size limitations rather than the browser's size limitations.

  12. In which of the following situations should I use the POST method for submitting a form:

    1. When the form will be used to query the database but not modify it
    2. When you want to allow external web-crawlers to query your site without going through the web-interface for the site
    3. When the form will be used to modify the database, such as placing an order or a reservation
    4. When the amount of information to be transmitted is in the thousands of kilobytes or more.
  13. Which of the following strings match the following regular expression.
        /^\d{2,4}$/ 
      
    1. bz385sj
    2. 171
    3. 37920
    4. 25
    5. 4
    6. 5384
    7. 223444
    8. 324432

  14. Which of the following strings match the following regular expression.
     /^([A-Z]\.)|([A-Z][a-z]*)$/
     
    1. 146Brad
    2. M.
    3. Ma
    4. G
    5. asterisk
    6. M.Brad
    7. GeorgeB.
    8. AARDVARK

  15. Which of the following features typically distinguish scripting languages from traditional, compiled languages such as C or C++?

    1. built-in data types for lists and hash tables, instead of library support for these data types
    2. strong access protections for object-oriented programming
    3. interpreted execution
    4. support for high-level string manipulation, including pattern-matching constructs
    5. built-in high-level loop constructs for iterating through lists and arrays, instead of library support for these loop constructs.
    6. efficient execution of programs
    7. support for general-purposing computing (e.g., the ability to write programs to accomplish arbitrary tasks)
    8. concise, compact syntax that allows programmers to quickly write small but powerful programs

  16. In which of the followng ways might a hacker be able to attack your web site if you fail to escape incoming data with single and double quotes with \'s and to convert html special characters to their corresponding entity codes?

    1. The hacker could cause you to return links to the client-side that could cause a user to click to a malicious web-site
    2. The hacker could cause you to return javascript code that implants a virus on the client's hard drive
    3. The hacker could execute malicious code on your server if you pass the data as parameters to system commands
    4. The hacker could execute malicious queries on your database if you pass the data as parameters to database queries
    5. The hacker could cause you to return javascript code that searches the client's hard drive for passwords

  17. In which of the following ways does a prototype-instance model differ from a class-instance model.

    1. All prototypes are themselves objects.
    2. Prototypes cannot contain methods.
    3. Prototypes do not have constructors.
    4. Properties can be dynamically added or deleted from any instance object.
    5. Properties can be dynamically added or deleted from any prototype object.
    6. Prototypes are computationally more efficient than classes.
    7. Prototypes cannot support multiple inheritance.
    8. Prototypes allow web interfaces to be rapidly changed without shutting down the web-site and recompiling it.

  18. Which of the following features can Ajax support on a web-page that could not be supported by javascript or html alone?

    1. Incrementally add or delete email headers sent from a server without regenerating the entire page
    2. Validate form data and mark invalid form elements without re-generating the entire page
    3. Update parts of a web page based on local changes to form data
    4. Display forms and allow users to interact with the forms by clicking on form elements and entering information into text boxes
    5. Display stock quotes without re-generating the entire page
    6. Allow a user to move to a new page by clicking on a link
    7. Allow new images to be periodically downloaded from the server and displayed in the same location without re-generating the entire page
    8. Display static images that are downloaded from the server when the web-page is first loaded.

  19. Which of the following problems with updating content on a web page was AJAX designed to solve?

    1. An inability to dynamically generate html pages on the server side that would reflect changes user made to a form
    2. The loss of the state information of javascript variables when a new page was loaded
    3. An inability to transmit images in a web-page
    4. The browser would freeze until the server responded with a new page
    5. Bandwidth and latency issues caused by having to transmit an entire page of information.
    6. A malicious hacker could read information on the client's file system

  20. Which of the following advantages does XML have over a database?

    1. Given the same amount of data, it provides much more compact storage
    2. It can execute queries more efficiently
    3. Its data is human-readable
    4. It provides greater data security
    5. It is less likely to have redundant data that is subject to update anamolies
    6. It provides powerful query operators that allows end-users without programming experience to quickly combine data from different schemas and generate reports
    7. It supports semi-structured data that may have different attributes based on an element's type
    8. It provides better support for concurrent updating of its data

    Fill in the blank questions: Fill in the blanks for each of the following questions.

  21. (8 points) For each of the following tasks, indicate which scripting language/markup language was designed to handle it:

    1. ____________________ create dynamic web pages on the server side by embedding code islands in html mark-up.

    2. ____________________ use regular expressions, to extract data from files and format this data into reports.

    3. ____________________ modify parts of a web-page by modifying the page's html dom

    4. ____________________ encode data by marking it up with tags

    Javascript questions. The following html markup applies to questions 22-26. All of the questions are multiple choice except for the one question explicitly marked as multiple answer.

    <form name="application" onSubmit="validateForm()"> First Name: <input type="text" name="fname" /> <p> Last Name: <input type="text" name="lname" /> <p> Email: <input type="text" name="email" disabled="true"/> </form> <p> Your name is <b id="username">your name</b> and your email is <b id="emailAddr">email address</b>.

  22. (2 points) If I want to obtain the user's first name and assign it to the variable firstname, which of the following Javascript statements should I use?

    1. firstname = document.forms.application.fname;
    2. firstname = document.forms.application.fname.value;
    3. firstname = document.forms.application.fname.innerHTML;
    4. firstname = document.getElementById('fname').value;

  23. Multiple Answer (4 points) How should I assign the value in name to the html element named username in the above html form? Circle all answers that are correct.

    1. document.username.html = name;
    2. document.username = name;
    3. document.username.innerHTML = name;
    4. document.getElementById('username') = name;
    5. document.getElementById('username').innerHTML = name;
    6. document.getElementById('username').value = name;
    7. document.getElementById('username').firstChild.nodeValue = name;
    8. document.getElementById('username').firstChild = name;

  24. (2 points) Suppose I wanted to add a submit button to the above form. Which of the following markup elements correctly accomplishes this task?

    1. <input type="submit" />
    2. <submit value="Submit Query" />
    3. <input type="button" value="Submit" />
    4. <input validateForm() />

  25. (2 points) Suppose I wanted to add an event to the first name text box that calls a function named validateName when the user changes the text string in the box. The function should be passed a reference to the fname element as an argument. Which of the following attribute clauses should I add to the fname element?
    1. onClick = validateName(fname);
    2. onClick = validateName(this);
    3. onChange = validateName(fname);
    4. onChange = validateName(this);
    5. changeEvent = validateName(fname);
    6. changeEvent = validateName(this);

  26. (2 points) Suppose as part of the above form I wanted to ask a user to specify their occupation and there are 6 possible occupations that a user could specify. What type of form element should I use assuming that they can specify only one occupation?
    1. radio buttons
    2. menu
    3. text box
    4. check boxes

  27. (6 points) Insert 10 into the following B+-tree of degree 5. Assume that leaf nodes can hold a maximum of 4 values.

  28. (10 points) Write a Perl regular expression to represent each of the following patterns. Just write the pattern and nothing else. For example, if I told you to write a pattern for a date that has the form mm-dd-yy, with month and day being one or two digits and year being exactly two digits, I would expect your answer to read /^\d{1,2}-\d{1,2}-\d{2}$/.

    1. A name such that: 1) the name may consist of one or more words (e.g., Brad T Vander Zanden), 2) the first letter of each separate word in the name is capitalized and the remaining letters are lower case, 3) there may be 0 or more remaining letters after the first letter 3) each word in the name consists only of alphabetical letters, and 4) each word of the name is separated by exactly one blank space.
      
      
      
      
      
          
    2. A string that specifies an id as containing:
      • exactly 2 digits,
      • followed by a group that contains a dash followed by 4-6 digits,
      • followed by an optional group that contains a dash followed by 1-3 lowercase letters (a-z).
      For example, 37-9203-br and 43-485855 are legitimate zip codes.
      
      
      
      
      
          

  29. (12 points) Write a Perl function named duplicate that determines whether or not there are any duplicate lines in a file. The function should take the name of the file as a string and it should return 1 if the file contains duplicate lines and 0 otherwise. If the file has duplicate lines, duplicate should print the line numbers of the duplicated lines. For example, if the file contains the lines:
    hi
    how are you
    this is
    a test
    how are you
    brad
    brad
    how are you
    today?
    
    then duplicate should print:
    lines 2 and 5 are duplicates
    lines 6 and 7 are duplicates
    lines 2 and 8 are duplicates
    
    Note that if a line is duplicated more than once, such as "how are you", then duplicate prints out the line number of the first line in the file that it duplicates.