2005 Final Exam

PIC40A Final Exam

Instructions

  1. The exam consists of 17 pages(including this page). You are responsible for checking the number of pages
  2. Please read and answer all the questions carefully. The point value for each question in indicated. The total number of points available is 100. If you have trouble with a question, leave it and try another.
  3. Unless otherwise stated, you may assume when code is given that the code compiles and is correct. When you are asked to write code, you are expected to write code in good style and with proper indentation.
  4. Good luck!

Pleaes use blue pen or black pen to write down your name and student ID. You can use pencil to answer the questions of the midterm.


Last Name:_________________ First Name:________________
Student ID:_________________












Qs Q1.(8) Q2.(12) Q3.(10) Q4.(5) Q5.(10)Q6.(15) Q7.(15)Q8.(15) Q9.(10) Total
Pts




















Q1(8pts)
(a) What is the output of the following program(without -w option):?

$name = "Charles";
print "$name is the instructor of PIC40A.\n";
$name = "Amy";
print '$name is the instructor of PIC40A.\n';

Answer:





(b)Find out the output of the following program(without -w option)?

$a = 1;
$b = 2;
$r = &fun;
print "out1: $r\n";
$a = -2;
$b = -3;
$r = &fun;
print "out2: $r\n";

sub fun {
    if($a < $b) {
        $c = 1;
    } else {
        $c = 0;
    }
    $a+$b+$c;
}

Answer:




(c)What is the output of the following program(without -w option)?

$a = "3";
$b = "2abc";
$c = "def";
print "out1: ", $c.$b, "\n\n";
print "out2: ", $a + $b, "\n\n";
print "out3: ", $c x $a, "\n\n";
print "out4: ", $b * $c, "\n\n";

Answer:

out1: 

out2:

out3:

out4:

(d)What is the output of the following program(without -w option)?

@array = qw(aXaXa bXabXa aXkXa abcXXXabc);
foreach (@array) {
    if(/X[^abcde]X/) {
        print "$`-$&-$'\n";
    }
}

Answer:





Q2(12pts)

(a)What is the output of the following program(without -w option)?

@a = (a..c, 1..2);
print "out1: ";
print @a;
print "\n";

print "out2: ";
print pop @a;
print "\n";

print "out3: ";
print shift @a;
print "\n";

print "out4: ";
print @a;
print "\n";

print "out5: ";
push(@a, "h");
unshift(@a, 5);
print @a;
print "\n";

print "out6: ";
print reverse @a;
print "\n";

Answer:

out1: 

out2:

out3:

out4:

out5:

out6:

(b)What is the output of the following program(without -w option)?

#     012345678901234567890123456
$a = "PIC40A, Perl. PIC20A, Java.";
print "out1: ";
print index $a, "A";
print "\n\n";

print "out2: ";
print rindex $a, "P";
print "\n\n";

print "out3: ";
print index($a, "abc");
print "\n\n";

print "out4: ";
print index($a, "PIC", 10);
print "\n\n";

print "out5: ";
print substr($a, -5, 2);
print "\n\n";

print "out6: ";
substr($a, 8, 4) = "P";
print $a;


Answer:

out1: 

out2:

out3:

out4:

out5:

out6:


Q3(10pts)
(a) Write down the regular expression that matches apple or orange
Answer



(b) Write down the regular expression that matches a regular california car license plate. It has 7 characters, the first one is a digit, then follow by 3 uppercase English letters(A to Z), then 3 more digits. Example, 2ABC987, 3EFG123.
Answer



(c)Write a regular expression that matches a character, a digit, the same character, the same digit, and the same character.
example, a3a3a, or B9B9B, #1#1#

Answer



(d)Write down a regular expression that matches :

Example
It matches

begin code something... end code 
something begin HTML some html ... end HTML something

but it doesn't match

beginABC something end ABC 
begin ABC something end DEF

Answer




Q4 (5pts)

Suppose I have a hash(named %hash), the key are names of graduate students. Value of a given student is the name of his advisor. Some advisors may have more than 2 students.
Create a new hash (named %students). The keys are the name of their advisors, the value for a given advisor is the number for students he has.
Example, if

%hash = ("Charles", "Prof A", "Mary", "Prof B",  "Lori", "Prof A",
        "Amy", "Prof A", "Ben", "Prof C", "Dan", "Prof D", "Ken", "Prof B");

Then, $students{"Prof A"} is 3 and $students{"Prof C"} is 1.

Fill in your codes below

# here is a sample hash, 
# you shouldn't assume that the hash is always same as the given one
%hash = ("Charles", "Prof A", "Mary", "Prof B",  "Lori", "Prof A",
          "Amy", "Prof A", "Ben", "Prof C", "Dan", "Prof D", "Ken", "Prof B");
# your codes
































Q5(10pts)

I have a file named data.txt. The file consists of a list of numbers in several lines. The numbers are seperated by a comman (,) and then a white space. I want to find out the average of the numbers on each line and print out the average.
Here is my file (save as data.txt):
1.5, 3.78, 9.2, 2.4
1.56, 1.56, 1.59, 3.1, 4.5
1.5, 2.9, 1.5, 1.1     
1, 2, 30
5.5, 5.9
Here is the output on DOS prompt
4.22
2.462
1.75
11
5.7

You can assume that all the numbers are in correct format and the file exists.

# your codes
 







































Qt(15pts)
(a)Write down the regular expression that matches an E-mail address,

Answer:




(b)You are a spammer, you want to get all the E-mail addresses in a file. Write a perl script named get_email.pl. the script reads in a file named email.html and append all the possible E-maill addresses(one on each line) to a file named list.txt.
You can assume that there is only one E-mail address on each line.

Answer:





























(c)UCLA tries to fight back spammer by changing the format of E-mail addresses, example, ccli@ucla.edu is changed to ccli AT ucla DOT edu. The script only changes the E-mail addresses that ends with ucla.edu . It ignores all other E-mail addresses.
Generally, the email user@ucla.edu is changed to user AT ucla DOT edu
The format of the user name follows the rule given in (a).
Write a script such that perl change.pl infile >outfile changes the E-mail addresses in file infile and write it to the outfile.
Example

<table>
<tr><th>Name</th><th>E-mail</th></tr>
<tr><td>Charles</td><td>charles@ucla.edu</td></tr>
<tr><td>Amy</td><td>amy-123@ucla.edu</td></tr>
<tr><td>Ben</td><td>Ben_123@ucla.edu</td></tr>
<!-- ignore, only change E-maid address that ends with ucla.edu -->
<tr><td>Dave</td><td>Dave@abc.com</td></tr>
</table>

is changed to

<table>
<tr><th>Name</th><th>E-mail</th></tr>
<tr><td>Charles</td><td>charles AT ucla DOT edu</td></tr>
<tr><td>Amy</td><td>amy-123 AT ucla DOT edu</td></tr>
<tr><td>Ben</td><td>Ben_123 AT ucla DOT edu</td></tr>
<!-- ignore, only change ends with ucla.edu -->
<tr><td>Dave</td><td>Dave@abc.com</td></tr>
</table>

Answer:




































Q7(15pts)

(a) Write a subroutine called price_after_sale_tax,

Fill in your codes below(on page 12).

(b) Write a CGI program named tax.cgi. The program asks the user to input the price of an item, where does he/she live. The CGI program calculates the price after adding the state sale tax.
You should use the hash that I provided to you.
No error check. The html files generated by the script should follows W3C standard

When you access tax.cgi, you can seee the webpage. Notice the names of the states are sorted.

price:

state:

Suppose I input 15 for the price and I choose California, then the output is: (I don't care about how many decimal places you use). Notice that no error check is necessary.

The price is $16.0875.

#!/usr/local/bin/perl -wT
use CGI qw(:standard);
# not the complete list of states
# key is the name of the states, value is the tax rate(in %)
%state_sale_tax = 
(Alabama => 4, Alaska => 0, Arizona => 5.6,
Arkansas => 5.125, California => 7.25, Colorado => 2.9, 	
Connecticut => 6, Delaware => 0, Florida => 6, 	
Georgia => 4, Hawaii => 4, Idaho => 5, 
Illinois => 6.25, Indiana => 6, Iowa => 5, 	
Kansas => 5.3, Kentucky => 6, Louisiana => 4);

if(param()) {
   @names = param();
   foreach $name (@names) {
      @item = param($name);
      if ($#item > 0) {
         $formdata{$name} = join(', ', @item);
      }
      else {
         $formdata{$name} = $item[0];
      }
   }

   # your codes
   



























} else { # your codes



























} # answer for part (a)





























# other codes (if any)
























sub header() { print "Content-type: text/html\n\n"; print qq(<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n); print qq(<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">); print "<head>\n<title>\n"; print "$_[0]\n</title>\n</head>\n<body>\n"; } sub footer() { print "</body>\n</html>\n"; }

Q8(15pts)

You are going to write a CGI program guess.cgi, such that when the user accesses guess.cgi, he sees the webpage:
At the beginning:

Welcome to the Number Guess game.

I'm thinking of a number between 1 and 100.

What's your guess?

When the user inputs a number that is smaller than the correct answer

Good guess, but nope. Try higher.

I'm thinking of a number between 1 and 100.

What's your guess?

When the user inputs a number that is bigger than the correct answer

Good guess, but nope. Try lower.

I'm thinking of a number between 1 and 100.

What's your guess?

When the user inputs a string that contains non-digit:

Good guess, but nope. Try a number next time.

I'm thinking of a number between 1 and 100.

What's your guess?

When the user makes a correct guess. There is a hyperlink links back to guess.cgi.

Congratulations! You got it.

Care to try again.

The html files generated by the script should follows W3C standard

For simplicity, the answer is always 31

Answer: fill in your code below

#!/usr/local/bin/perl -wT
use CGI qw(:standard);


# $answer is  31
$answer = 31;

if(param()) {
   @names = param();
   foreach $name (@names) {
      @item = param($name);
      if ($#item > 0) {
         $formdata{$name} = join(', ', @item);
      }
      else {
         $formdata{$name} = $item[0];
      }
   }

   # your codes
   


































} else { # your codes





























} # other codes


































sub header() { print "Content-type: text/html\n\n"; print qq(<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n); print qq(<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">); print "<head>\n<title>\n"; print "$_[0]\n</title>\n</head>\n<body>\n"; } sub footer() { print "</body>\n</html>\n"; }

Q9 (10pts): For each of the following program, there will be some error messages when you run the program with -w options. Circle the errors and write down the corrections.

(a)

@array = qw/1 abc 2 def 3/;
%hash = @array;
print $hash{"1"};

(b)

use strict;
$abc = "567";
$abc++;
print $abc;

(c)

$name = "Charles";
hello;
print $name;

sub hello {
    print "hello";
}

(d)

$str="Regular expression uses /pattern/";

# matches /pattern/
if($str =~ //pattern//) {
    print $str;
}

(e)

x = 5;
if($x) {
  print $x;
}