Thu Oct 29 1998
A348 Midterm Examination (results)

Available time: 75 min.

Total points: 47

Work as fast as you can without being careless. Good luck and do well!

Please write your name or username here: _________________________________

Correct answers are written in blue

1. (3 points) Write the HTML for a form like this:

Username
Password

Note: the form contains a text field and password field inside a 2x2 table. Call the two fields uname and pword respectively. Make the form POST the data to the following script:
http://tucotuco:19800/cgi-bin/simpleCheck
Please write the HTML code here (just the form not the whole HTML document):
<form method="POST" 
       action="http://tucotuco:19800/cgi-bin/simpleCheck">
  <table border>
    <tr> <td> Username </td> 
         <td> <input type="text" name="uname"> </td> 
    </tr>
    <tr> <td> Password </td> 
         <td> <input type="password" name="pword"> </td> 
    </tr>
  </table>
  <input type="submit" value="Proceed">
</form> 
2. Keeping in mind what Perl considers as true and false specify the output for each of the following two Perl programs:
2.1 (1.5 points) The first program is shown below:
#!/usr/bin/perl
$i = 0; $j = 1;
if ($i = 2) { print $i; }
else { print $j; } 
The answer is:
0 1 2 3
As in C == is the equality operator and = is used for assignment (as in the first two statements that assign 0 to $i and 1 to $j).

The question now boils down to the value returned by an assignment statement: an assignment statement returns the value of its right hand side. So in this case 2 is returned, and 2 being different from 0 it is counted as being true, so the print $i branch is going to be executed.

Since $i has been made 2 by this very test, the program will print this number. Therefore the right answer to this question is: 2.

Admittedly this is a trick (or treat?) question. But it's also a classic.

2.2 (1.5 points) Same program as above with only $i = 0 in the condition for the if statement.

#!/usr/bin/perl
$i = 0; $j = 1;
if ($i = 0) { print $i; }
else { print $j; } 
The answer is:
0 1 2 3
This is a variation on the previous question but it's not easier even if you have solved the first one right. The correct answer is 1, because $j will be printed.

The reason behind this is that $i will be set to 0 but the returned value (which is 0) will make the if statement direct execution towards the else branch. $j, which is set to 1 in the beginning, will be printed.

3. Your Apache directory structure looks like this:

/---u---username---httpd---+---httpd*
                           |
                           +--logs
                           |
                           +--cgi-bin
                           |
                           +--conf
                           |
                           +--src
                           |
                           ... 
3.1 (1 point) What's in /u/username/httpd/httpd?

The server binary, of course.

3.2 (1 point) Where is httpd.pid located:

/u/username/httpd/logs
/u/username/httpd/cgi-bin
/u/username/httpd/conf
/u/username/httpd/src

3.3 (1 point) What does httpd.pid contain?

The process id of the server.

3.4 (1 points) What's the use of the contents of httpd.pid? (Describe ways in which you have used it, if any.)

I use it to stop and restart the server.

4. Match the following configuration files with roles listed:
4.1 (1 point) httpd.conf
controls the behaviour of the server after it starts up
provides directory-by-directory control over what hosts and users can retrieve documents from your server
controls settings that are used when the server first starts up
none of the above

The answer to this question is on page 76 in the textbook.

4.2 (1 point) srm.conf
controls the behaviour of the server after it starts up
provides directory-by-directory control over what hosts and users can retrieve documents from your server
controls settings that are used when the server first starts up
none of the above

Same page, a few lines below the answer to the previous question.

4.3 (1 point) access.conf
controls the behaviour of the server after it starts up
provides directory-by-directory control over what hosts and users can retrieve documents from your server
controls settings that are used when the server first starts up
none of the above

The answer is on page 77 in the textbook.

5. (2 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 script has the following code:
#!/usr/bin/perl
print "Content-type: text/html\n\nHello world!"; 
What will happen when you call this script with a web browser? Why?

The server will run as user nobody and the script is executable only by you. So the server won't be able to start your script and the user will see an error message in her browser.

6. (3 points) What's the output of the following CGI.pm function call:
print $query->radio_group(-name=>'type',
                   -values=>['radio', 'select', 'check', 'open'],
                   -default=>'radio',
                   -linebreak=>'true',
                   -labels=>{
             'radio'  => ' Create a RADIO question', 
             'select' => ' Create a SELECT question', 
             'check'  => ' Create a CHECK all that apply question', 
             'open'   => ' Create an OPEN-ended (short answer) question'
                             });
Assume that $query is a CGI object and that CGI.pm is used.
    <INPUT TYPE="radio" NAME="type" VALUE="radio" CHECKED> Create a RADIO question 
<BR><INPUT TYPE="radio" NAME="type" VALUE="select"> Create a SELECT question 
<BR><INPUT TYPE="radio" NAME="type" VALUE="check"> Create a CHECK all that apply question 
<BR><INPUT TYPE="radio" NAME="type" VALUE="open"> Create an OPEN-ended (short answer) question 
<BR>
7. (4 points) Write the part that's missing from the simpleCheck script presented below:
#!/usr/bin/perl
use CGI;
$query = new CGI;
print $query->header, $query->start_html; 
if ($query->request_method eq 'GET') {
  &show_form; # don't write this 
} else { 
  # write this part
} 
print $query->end_html; 
The script is supposed to check that the username typed by the user is admin and the password letmein. It should print a simple message that states whether the check was or was not successful. Note: do not write subroutine show_form (which outputs the HTML that you have produced in problem 1). Refer to problem 1 in this exam (above) for the names of the two fields and use the space below for the code you write:

A correct answer could be

if ($query->param('uname') =~ /^\s*admin\s*$/i &&
    $query->param('pword') eq "letmein") { 
  print "Correct."; 
} else { 
  print "Incorrect."; 
} 
or something to that effect.

8. (1 point) Assume this crontab entry:

0 * * * * /u/username/bin/starthttpd 
and a properly configured starthttpd.

Then your server will be checked and if needed restarted

every Sunday at midnight
every Sunday
every hour
every hour on Mondays only

There was a similar question in homework 6 that had different settings. This particular setting was also the recommended setting for our servers, so this question was easy.

9. (1.5 points) From /u/username/httpd/logs which of the following commands can be used to start your server (check all that apply).

starthttpd -k
/u/username/httpd/httpd -d /u/username/httpd
./httpd -d .
/u/username/httpd/httpd -d ..
none of the above

The second option is correct because it points to both the binary and its location in an absolute way. There's no ambiguity about this. So the last option falls. That leaves us with three more to check.

The k switch on starthttpd is used to kill the server, as explained in the lab and lecture notes, and that takes care of that option too.

The fourth one is equivalent to the second one (given the location) while the third one can't be (for the same reason).

So only two answers are correct and the rest are incorrect.

10. (1.5 points) What's the output of this simple Perl program:

#!/usr/bin/perl
$a = 1 + "camel" + "bobac"; 
@a = ("camel", "bobac"); 
%a = ("camel" => "desert", "bobac" => "burrow"); 
print $a{$a[$a]}, "\n"; 
camel desert bobac burrow an error will be reported

Why?

Perl is a typeless language and gives you a lot of freedom. Some of this freedom can come back to haunt you if you're not careful enough. Contrived as it may be this example points out that if you add a number to a string by default Perl will not complain. It will try to do what it thinks you may have intended. It looks at the string and tries to figure what number it could be. For example

$x = 1 + "2" + "3";
will set $x to 6. It's a good thing I don't have to convert the strings into numbers.

However "camel" will be 0 as a number because it has no digits inside. In fact any /^[a-zA-Z]*$/ is 0 as a number, for that matter, if we want to be precise.

That means that $a is set to 1 + 0 + 0 which means 1. From here on the question is easy. Since $a is 1, $a[$a] is "bobac" and $a{$a[$a]} is "burrow".

11. (1 point) Which of the following files contains the number of the port on which the server runs?
httpd.pid
httpd.conf
access.conf
srm.conf
none of the above

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

STDIN
$ENV{CONTENT_LENGTH}
$ENV{QUERY_STRING}
STDOUT
$QUERY_STRING

This is explained in the textbook page 482, section 3.

13. (3 points) Assume the following program:

#!/usr/bin/perl
$x = $ARGV[0]; 
while ($x =~ s/(\d+)//) {
  print "(", ++$i, ".: ", $1, ") "; 
} 
What is the program going to print when called as follows:
./program "Wed Oct 28 15:59:59 EST 1998 Dow closed 5.93 higher"
Write your answer here:
(1.: 28) (2.: 15) (3.: 59) (4.: 59) (5.: 1998) (6.: 5) (7.: 93)
There was a similar problem on homework 6, and it's the same process that we made use of in the URL finder in homework 5. All contiguous sequences of numbers are detected and printed. We print them all on one line, but we also count them, and since $i is first incremented and then printed (++$i) the count starts at 1.

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

STDIN
$ENV{CONTENT_LENGTH}
$ENV{QUERY_STRING}
STDOUT
$QUERY_STRING

This is explained in the textbook page 482, section 1.

15. (1 point) In the JavaScript lab (the phonebook) the following code appears:

<FORM NAME="phonebook" 
       METHOD="POST" 
       ACTION="http://yourHost.cs.indiana.edu:yourPort#/cgi-bin/process"
       onSubmit="return validateForm()">
The purpose of return is to:
prevent the form from being submitted in certain circumstances
guarantee JavaScript syntax is being followed strictly (return is required)
invoke the validateForm routine
monitor the events and call the corresponding event handler

I mentioned in the lab when I demonstrated how the form works that if you want your check to have any impact at all you'd want to make sure that when the result of the check is false the form does not get submitted. return is forcing this value to be propagated up the hirearchy, and thus it helps prevent the form from being submitted when it shouldn't. It is the link between the functions that check if the form is properly filled in and the submission mechanism of the form.

16. (2 points) Which of the following is a valid way to embed an applet class named scribble into a web page? Select all answers that look valid to you:

<applet class=scribble.class width=100 height=100>
</applet>
<applet code=scribble width=100 height=100>
</applet>
<applet code=scribble.class width=100 height=100>
</applet>
<applet param=scribble.class width=100 height=100>
</applet>
<applet param=scribble width=100 height=100>
</applet>
As discussed in class (!) the .class extension is assumed so if it's missing there's no harm. No harm either if it's specified, since that's the expected standard format anyway. The other three options are blatantly wrong so I won't spend more time with them here.

17. (1 point) You're using GD.pm to draw pictures. What does the following code do (if anything):

$image->line(0, 10, 20, 30); 
draws a line from (0, 20) to (10, 30)
draws a line from (0, 10) to (20, 30)
draws the outline of a box whose left, top corner is at (0, 10) and that is 20 pixels wide and 30 pixels high
nothing - this code does not compile because it does not provide the correct number of arguments
nothing - this code does not compile because the arguments are in the wrong order

The documentation says a color needs to be specified as a fifth argument. This library is pretty strict and the method doesn't work without that parameter. A few of you have tried this out for homework 6 and have observed it directly.

18. (1 point) What is the output of the following Perl program:

#!/usr/bin/perl
@a = (1, 2, 3, 4); 
print $#a; 
-1 0 1 3 5 an error will be reported The index of the last element of list @a is 3.
19. (1 point) Assume the following Perl program called one:
#!/usr/bin/perl
print $ARGV[1]; 
What is the output of the program when the program is called as follows:
./one my three arguments 
my three arguments 2 no output
The second command line argument will be printed.
20. Here's a program called database:
#!/usr/bin/perl
$function = $ARGV[0]; 
$animal   = $ARGV[1]; 
$habitat  = $ARGV[2]; 
dbmopen($zoo, "/u/username/databases/zoo", 0644); 
if ($function eq 'add') { 
  if ($zoo{$animal}) { $zoo{$animal} .=  ":$habitat"; } 
  else { $zoo{$animal} = $habitat; }  
  print "Thanks for adding ($animal, $habitat)"; 
} elsif ($function eq 'search') {
  print $zoo{$animal}; 
} elsif ($function eq "delete") { 
  # this space for rent 
} else { print "Command $function not implemented yet."; } 
dbmclose(%zoo); 
Assume that the database is empty and the script is called four times in this sequence:
  1. ./database add camel desert
  2. ./database add camel "new york"
  3. ./database add camel desert
  4. ./database search camel
20.1 (2.5 points) What output will be produced after the fourth call:

desert
new york
new york:desert:desert
desert:new york:desert
:desert:new york:desert:
The .= operator works for strings like += for numbers. This is the reason for which the entry for $zoo{$animal} is going to keep growing. Now notice how the entry is checked to make sure it grows only if there is already something in there. If this is the first habitat we're storing then = will be used instead of .=. So the last option is incorrect, because of the leading :. The order in which we grow the sequence of habitats is the order of the calls, so new york is in the middle so the right answer can't be the third one either.

20.2 (1 point) Do we need the double quotes around new york in the second call? Why?

Yes, we need to protect the blank space from being interpreted as a delimiter when @ARGV is created.
20.3 (1 point) What's the final output of the program if we do not use double quotes at all?
desert
new
new:desert:desert
desert:new:desert
desert:new york:desert Same reasoning as before only york gets lost.
20.4 (2.5 points) Add a
delete <animal>
functionality to this program by writing one or more lines of code to replace the
# this space for rent
comment in the program. Use the space below to write your answer:
delete $zoo{$animal}
We have covered this in the second part of homework 4
21. (2 points) The following lines of code:
#!/usr/bin/perl
open (AB, "file"); 
@x = <AB>;
will produce an error
will read a line of input from file file
will place the lines of file file in @ARGV
will read the entire file placing it in a unidimensional list of lines

The unidimensional list that we are alluding to is called @x. No error will be produced, all lines will be read, not just one, and the destination is not @ARGV.