Practice Exam Answers

Short Answer Questions [15 marks]

  1. [1 mark]For LZW compression, assume an initial dictionary of three elements:
    a0
    b1
    What stream of numbers would be sent for the string 'aabaabaab'?
    input charadded to dictoutput
    a  
    aaa=20=a
    bab=30=a
    aba=41=b
    a  
    baab=52=aa
    a  
    abaa=64=ba
    b  
    .aab=53=ab
  2. [2 marks] Very generically, what are <div> and <span> used for in HTML?

    div is a generic block display tag and span is a generic inline display tag. They are typically used in conjunction with CSS to format block text and inline text respectively.

  3. [1 mark] How does an HTTP client know that the HTTP server is sending back HTML as opposed to, say, JPEG data?

    The HTTP response header contains a header field "Content-Type" which indicates the MIME type of the data returned in the response body. The MIME type for HTML is "text/html", and that for a JPEG image is "image/jpeg".

  4. [2 marks] What does TCP add on top of what IP provides already?

    IP provides for unique worldwide network addresses and the protocols necessary to bridge different physical networks (hence the name Inter-net). IP, however, sends only small, fixed-size packets of data to a specific computer (not to a specific process running on that computer), may silently drop packets, and may have them arrive in a different order than they were sent. TCP uses IP to implement a reliable (it either works or it sends you an error saying it didn't), infinite byte-stream oriented networking capability that allows for multiple ports that may be read/written by separate processes on a computer.

  5. [1 mark] True or false: when using the Location: redirect in a header, the HTTP server includes the text of the new destination page in the body section after the header.

    Probably false. The Web server will typically parse a response header before sending it to the client browser. If it detects a redirect, it may implement it in many ways, including sending the text of the destination page, but this would be unusual. Typically, it would send a page indicating that the requested page has moved and giving the new URL. This is for the benefits of browsers that can't or won't handle the redirect header.

  6. [3 marks] Describe three ways of implementing the notion of a session in CGI.

    1. Cookies
      A cookie containing a unique session id is passed to the client browser. Each time the client comes back to the same server, the cookie containing the session id is passed back.
    2. URL Rewriting
      Each URL that refers back to other pages on the same server within the same "web application" is written with a "?" query-string parameter indicating the unique session id
    3. Hidden Form Variables
      Each form contains a hidden variable whose value is the unique session id.
  7. [1 mark] What is the issue with CGI program and caching? How can the issue be resolved?

    If a user follows a URL to a page that is dynamically generated, it may be that the information on that page is state dependent. If the client browser or proxies cache the generated page associated with a given URL, the server has no chance to re-generate the page. To avoid this, HHTP response header fields may be set to indicate that caching should be turned off.

  8. [1 mark] In JDBC, what are the differences between a Statement object and a PreparedStatement object? When is a PreparedStatement the better choice?

    A PreparedStatement is an SQL query written with "?" as placeholders for values to be filled in later. A simple Statement has no such facility. In addition to making the programming more convienient, a PreparedStatement may also be implemetned more efficiently using, for instance, stored procedures on the database server.

  9. [1 mark] Suppose you have just telnet'd to port 80 on the blah.com HTTP server, and you are initiating an HTTP/1.0 connection. Write the text that you would enter to initiate the retrieval of the document http://blah.com/doc/intro.html

    GET /doc/intro.html HTTP/1.0\n
    \n
    
  10. [2 marks] Why is &lt; defined as <!ENTITY lt "&#38;#60;">?

    The alternative, to define it as &#60, would not work. In the ENTITY tag definition, the first round of replacement would take place, replacing this by the character '<'. The expansion of &lt; would therefore result in the literal text '<' getting inlined, and this would start an XML tag.

    To solve the problem, &lt; expands to "&#60" in the XML text. These literal characters do not force a start tag but are drawn as a < character when displayed.

    Programming Questions [15 marks]

    1. [2 marks] Write an html document that displays three other html documents, "1.html", "2.html", "3.html" as a set of frames such that 1 occupies 3/4 of the top, and 2 and 3 occupy the bottom 1/4, with 2 using 100 pixels on the left, and 3 using the rest.
      <html>
        <frameset rows="75%,25%">
            <frame src="1.html">
            <frameset cols="100,*">
      	  <frame src="2.html">
      	  <frame src="3.html">
            </frameset>
        </frameset>
      </html>
      
    2. [2 marks] Write a css that defines a class called 'red' that colors the text red for tags of that class. Show an example HTML files that uses this stylesheet to color an <h2> element red.
      <html>
          <head>
      	<style type="text/css"><!--
      	    .red { color: red }
      	--></style>
          </head>
          <body>
      	<h2 class="red">This heading is red</h2>
          </body>
      </html>
      
    3. [5 marks] Write an XML DTD for storing information about groceries, including name, price, quantity on hand, and the name of its gif image. Include a small example XML file. Then write an XSL stylesheet that displays this information in an HTML table within a web page.
      grocery.xml:
          <?xml version="1.0" encoding="iso-8859-1"?>
          <?xml-stylesheet type="text/xsl" href="grocery.xsl"?>
          <!DOCTYPE grocery SYSTEM "grocery.dtd">
          <grocery>
            <item>
      	  <name>milk</name>
      	  <price>$1.12</price>
      	  <quantity>8023</quantity>
      	  <image>milk.gif</image>
            </item>
            <item>
      	  <name>beans</name>
      	  <price>$0.69</price>
      	  <quantity>218</quantity>
      	  <image>beans.gif</image>
            </item>
          </grocery>
      
      grocery.dtd:
          <!ELEMENT grocery (item+)>
      	<!ELEMENT item (name,price,quantity,image)>
      	    <!ELEMENT name (#PCDATA)>
      	    <!ELEMENT price (#PCDATA)>
      	    <!ELEMENT quantity (#PCDATA)>
      	    <!ELEMENT image (#PCDATA)>
      
      grocery.xsl
          <?xml version="1.0" encoding="iso-8859-1"?>
          <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      	<xsl:output method="html"/>
      
      	<xsl:template match="/grocery">
      	    <html>
      		<head>
      		    <title>Grocery List</title>
      		</head>
      		<body>
      		    <table border="1">
      			<tr>
      			    <th>name</th>
      			    <th>price</th>
      			    <th>quantity</th>
      			    <th>image</th>
      			</tr>
      			<xsl:apply-templates select="item"/>
      		    </table>
      		</body>
      	    </html>
      	</xsl:template>
      
      	<xsl:template match="item">
      	    <tr>
      		<td>
      		    <xsl:value-of select="name"/>
      		</td>
      		<td>
      		    <xsl:value-of select="price"/>
      		</td>
      		<td>
      		    <xsl:value-of select="quantity"/>
      		</td>
      		<td>
      		    <img>
      			<xsl:attribute name="src">
      			    <xsl:value-of select="image"/>
      			</xsl:attribute>
      		    </img>
      		</td>
      	    </tr>
      	</xsl:template>
          </xsl:stylesheet>
      
    4. [6 marks] Write a web application using HTML forms and using using first JavaScript, then CGI/Perl, then Servlets, that asks you to guess a number between 1 and 10, and then says "correct" if the number is 1, and "incorrect" otherwise.
      JS:
      <html>
          <head>
      	<!-- set script type for intrinsic event scripts -->
      	<meta http-equiv="Content-Script-Type" content="text/javascript">
      
      	<title>Guessing Game</title>
      
      	<script type="text/javascript" language="javascript">
      	    function doguess(g) {
      		if( g == 1 ) {
      		    alert("correct");
      		} else {
      		    alert("incorrect");
      		}
      	    }
      	</script>
          </head>
      
          <body>
      	<form name="guessform">
      	    <p>Guess a number between 1 and 10:
      	    <input name="guessentry" type="text"></input>
      	    <input type="button" onclick="doguess(document.guessfor.guessentry.value)"></input>
      	</form>
          </body>
      </html>
      
      CGI:
      #!/usr/bin/perl
      use CGI qw/:standard/;
      print
          header,
          start_html("Guessing Game"),
          start_form,
      	p, "Guess a number between 1 and 10: ", textfield('guessentry'), submit,
          end_form;
      if(param()) {
         if( param("guessentry") == 1 ) {
             print hr, p, "correct";
          else {
             print hr, p, "incorrect";
          }
      }
      print end_html;
      
      Servlets:
      import java.io.*;
      import javax.servlet.*;
      import javax.servlet.http.*;
      
      class GuessServlet extends HttpServlet {
          public void doGet(HttpServletRequest req, HttpServletResponse res) {
      	res.setContentType("text/html");
      	PrintWriter out = res.getWriter();
      	out.println("<head><title>Guessing Game</title></head>");
      	out.println("<body>");
      	    out.println("<form method='get' action='GuessServlet'>");
      		out.println("Guess a number between 1 and 10: ");
      		out.println("<input name='guessentry' type='text'></input>");
      		out.println("<input type='submit'></input>");
      	    out.println("</form>");
      
      	if( req.getParameterNames().hasMoreElements() ) {
      	    String guess = req.getParameter("guessentry");
      	    if( guess != null ) {
      		out.println("<hr><p>");
      		if( guess.trim().equals("1") )
      		    out.println("correct");
      		else
      		    out.println("incorrect");
      	    }
      	}
      	out.println("</body>");
      	out.println("</html>");
          }
      }