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:
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:
Please write the HTML code here (just the form not the whole HTML document):http://tucotuco:19800/cgi-bin/simpleCheck
<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:The answer is: As in C#!/usr/bin/perl $i = 0; $j = 1; if ($i = 2) { print $i; } else { print $j; }==is the equality operator and=is used for assignment (as in the first two statements that assign0to$iand1to$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
2is returned, and2being different from0it is counted as being true, so theprint $ibranch is going to be executed.Since
$ihas been made2by 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 = 0in the condition for theifstatement.The answer is: 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#!/usr/bin/perl $i = 0; $j = 1; if ($i = 0) { print $i; } else { print $j; }1, because$jwill be printed.The reason behind this is that
$iwill be set to0but the returned value (which is0) will make theifstatement direct execution towards the else branch.$j, which is set to1in the beginning, will be printed.
3. Your Apache directory structure looks like this:
4. Match the following configuration files with roles listed:/---u---username---httpd---+---httpd* | +--logs | +--cgi-bin | +--conf | +--src | ...3.3 (1 point) What does
httpd.pidcontain?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.
5. (2 points) Suppose that you still have the default (initial) setting in your
httpd.conf file as: 6. (3 points) What's the output of the following CGI.pm function call:and a scriptUser nobodyscriptin yourcgi-bindirectory that looks like this:where username is your username. Assume-rwx------ 1 username students 192 Sep 15 01:32 scriptscripthas the following code:What will happen when you call this script with a web browser? Why?#!/usr/bin/perl print "Content-type: text/html\n\nHello world!";The server will run as user
nobodyand 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.
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
or something to that effect. if ($query->param('uname') =~ /^\s*admin\s*$/i &&
$query->param('pword') eq "letmein") {
print "Correct.";
} else {
print "Incorrect.";
}
8. (1 point) Assume this crontab entry:
and a properly configured0 * * * * /u/username/bin/starthttpd
starthttpd.
Then your server will be checked and if needed restarted
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).
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
kswitch onstarthttpdis 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";
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_STRINGThis 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: Write your answer here:./program "Wed Oct 28 15:59:59 EST 1998 Dow closed 5.93 higher"
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(1.: 28) (2.: 15) (3.: 59) (4.: 59) (5.: 1998) (6.: 5) (7.: 93)
$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_STRINGThis 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 (returnis required)
invoke thevalidateFormroutine
monitor the events and call the corresponding event handlerI 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
falsethe form does not get submitted.returnis 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>As discussed in class (
<applet param=scribble width=100 height=100> </applet>!) the
.classextension 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:
19. (1 point) Assume the following Perl program called one:The index of the last element of list#!/usr/bin/perl @a = (1, 2, 3, 4); print $#a;@ais3.
What is the output of the program when the program is called as follows:#!/usr/bin/perl print $ARGV[1];
./one my three arguments
The second command line argument will be printed.20. Here's a program called
database: The#!/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:20.1 (2.5 points) What output will be produced after the fourth call:
./database add camel desert./database add camel "new york"./database add camel desert./database search camel
.= 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?
Same reasoning as before only york gets lost.
20.4 (2.5 points) Add a functionality to this program by writing one or more lines of code to replace thedelete <animal>
comment in the program. Use the space below to write your answer:# this space for rent
delete $zoo{$animal}
We have covered this in the second part of homework 4