Thu Oct 28 19:00:00 EST 1999

CSCI A348/A548

Mastering the World-Wide Web

Fall99 - Midterm Examination

Max number of points: 114 Time allotted: 75'
Write your name or username here: ___________________________________
Work as fast as you can, without being careless. Good luck and do well! The exam is open-book.

1. (10 points) Write a Perl program that opens a text file and prints the contents of the file to the standard output such that all vowels (uppercase or lowercase) are surrounded by parentheses. Here's an example assuming the program is called one and that it always opens the file called file, whose contents is also shown below. file
If a packet hits a pocket on a socket on a port,
and the bus is interrupted and the interrupt's not caught,
then the socket packet pocket has an error to report. 
      -- Programmer's traditional nursery rhyme.
(I)f (a) p(a)ck(e)t h(i)ts (a) p(o)ck(e)t (o)n (a) s(o)ck(e)t (o)n (a) p(o)rt,
(a)nd th(e) b(u)s (i)s (i)nt(e)rr(u)pt(e)d (a)nd th(e) (i)nt(e)rr(u)pt's n(o)t c(a)(u)ght,
th(e)n th(e) s(o)ck(e)t p(a)ck(e)t p(o)ck(e)t h(a)s (a)n (e)rr(o)r t(o) r(e)p(o)rt. 
      -- Pr(o)gr(a)mm(e)r's tr(a)d(i)t(i)(o)n(a)l n(u)rs(e)ry rhym(e).
2. (10 points) Write a Perl program that accepts two command-line arguments, a string and a filename, opens the file specified by the filename and lists all the lines in the file that contain the string. When the lines are printed the occurrences of the string in the line should be surrounded by parentheses. (This is almost a grep kind of program). Here's an example assuming the program is called two and we invoke it on the file file looking for the lines that contain the string packet: packet file
If a (packet) hits a pocket on a socket on a port,
then the socket (packet) pocket has an error to report.
3. (15 points) Assume you have a datafile that looks like this:
A348:dgerman:Adrian German:Franklin M005:5-2510
A346:kinzler:Steve Kinzler:LH 430B:5-3478
A202:asengupt:Jit Sengupta:LH 225B:5-3703
A592:sebroadl:Sean Broadley:Sycamore 910:5-6663
Write a CGI script that provides the following interface:
Course name (e.g., A348)

Press to search.

That is, an HTML form with a text field and a submit button labeled 'Proceed'. When 'Proceed' is pressed the script returns the line (or lines) that match the course name in the file:
Your search for A348 produced the following result:
  • A348
  • dgerman
  • Adrian German
  • Franklin M005
  • 5-2510
If more than one line matches the search string, you can separate the lists returned with <hr>'s.

4. (10 points) Draw a diagram of your Apache web server directory structure and clearly identify on the diagram the following files (or write the absolute path for each one of them):

5. (3 points) Suppose that you still have the default (initial) setting in your httpd.conf file as:
User nobody
and a script script in your cgi-bin directory that looks like this:
-rwx------   1 username  students     192 Sep 15 01:32 script
where username is your username. Assume that the script has the following code:
print "Content-type: text/html\n\nHello world!"; 
What will happen when you call this script from a web browser? Why?

6. (7 points) Explain what the following lines of perl do:

open (AB, "datafile");
@lines = <AB>; 
foreach $line (@lines) {
  ($a, $b) = split(/:/, $line, 2); 
  open (MAIL, "| mail $a\"); 
  print "$a:\n"; 
  print "This message is for you."; 
Can you guess the structure of the file datafile?

7. (2 point) Which file contains the port number on which the server runs?

8. (2 point) The CGI specifies an interface whereby a CGI script called with method POST should expect to receive the data from the server in _________________

9. (2 point) The CGI specifies an interface whereby a CGI script called with method GET should expect to receive the data from the server in _________________

10. (6 points) Assume the following program:

$x = $ARGV[0]; 
while ($x =~ s/([^a-z]+)//) {
  $i += 1; 
  print "(", $i, ".: ", $1, ")\n"; 
What is the program going to print when called as follows:
./program "Thu Oct 28 15:59:59 EST 1999 Dow closed 10.22 higher"
Now remove the caret (^) and run the program again, same way. What does it output this time?

11. (3 points) Explain what the following perl program (called prog) does:

print $ARGV[1] + $ARGV[0] + $#ARGV;
Consider the following cases:
./prog 1 2 3 4
./prog 5 10
./prog one two
and write down what the output is in each of the cases.

12. (10 points) Write the code for a JavaScript program that keeps track of how many times a button is pressed in a form on a web page and prints that to the user, somewhere where the user can see it.

13.1 (15 points) Write a simple calculator with JavaScript. The set up is as follows: the screen is split in two frames, one that has the add, sub, div and mul buttons, and a text field for the number (arg) that is to be added, subtracted, multiplied or used to divide the accumulator, and the other frame reports the current value of the accumulator (originally 0).

13.2 (10 points) When the user is done computing a Proceed button should generate (either in one of the frames or in a separate window) an HTML form that contains:

When the user pushes the submit button the information is sent to a perl script that mails the data to you and also outputs it on the screen for the user to see (and possibly print) the contents of what has been sent by e-mail. (Please provide the perl script as well).

14. (3 points) Let's say you want to schedule a certain command (starthttpd -r) to run every day at 5am, but on Fridays you want it to run every 4 hours, and on the 1st and the 15th of each month you want it to run every hour. How would you set your crontab file?

15. (3 points) Describe how you can start, stop, and/or restart the server without the use of starthttpd. The correct answer to this question would include

16. (3 points) Consider the following JavaScript code:

function a(x, y) {  
  return x(y, y);
function b(x, y) {  
  return x * y;
document.write(a(b, b(2, 3))); 
What will the browser display when you load the page in it?

Here is the exam with solutions