COMP111: Unix and Script Programming: The Hong Kong University of Science & Technology

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 20

THE HONG KONG UNIVERSITY OF SCIENCE & TECHNOLOGY

COMP111: Unix and Script


Programming
Final Exam
Fall 2006
Tuesday 19 Dec 2006, 16:30-19:30

Student Name: ___________key_________________________________

Student ID: _________________________________________________

Lab Section: ________________________________________________

Instructions:

1. This is a closed-book, closed-notes examination.


2. Write your name, student ID, lab section, and TA name on this page.
3. Please circle your answer to make it clear.
4. Answer all questions in the space provided. Rough work should be done on the back pages.

For Grading Purposes Only:

Problem 1 ______________/ 11
Problem 2 ______________/ 5
Problem 3 ______________/ 5
Problem 4 ______________/ 9
Problem 5 ______________/ 10
Problem 6 ______________/ 18
Problem 7 ______________/ 10
Problem 8 ______________/ 11
Problem 9 ______________/ 6
Problem 10 ______________/ 10
Problem 11 ______________/ 5

Total: ______________/ 100

COMP111 Fall 2006 HKUST 1 of 20


1) Perl Hashes [11 Marks]
No partial marks for this question.

a) Point out ONE mistake in the following code. (2 marks)

#!/usr/local/bin/perl5 w
@b{"one","two",three} = qw(three two one);
print $b{one}\n;

Answer: Need backslash for nested double quotes: \one\. (0 or 2 marks)

b) Fill in the blank so that the program lists out all the key-value pairs in %b (2
marks)

#!/usr/local/bin/perl5 w
$b{a} = b;
$b{111} = a;
foreach __________________________________________ {
print key: $i, value: $b{$i}\n;
}
Answer: foreach ($i (keys(%b)) (0 or 2 marks)

c) Fill in the blank so that the program lists out all the key-value pairs in %b (2
marks)

#!/usr/local/bin/perl5 w
$b{a} = b
$b(111) = a
while _____________________________________ {
print key: $i, value: $k \n;
}
Answer: while (($i,$k) = each(%b)) (0 or 2 marks)

d) Fill in the blank so that the program lists out all the key values in %b (2
marks)
P.S. you can only use at most one @b, and you cant use any $b and %b in your
answer.

#!/usr/local/bin/perl5 w
$b{a} = b
$b(b) = a
print The values are: ____________________________ \n;

Answer: @b{qw(a b)} (0 or 2 marks)

e) Fill in ONE WORD/VARIABLE only for each of the three blanks, so that the
hash %b ends up with 2 key-value pairs only where the keys are Fruit and
Fruits. (3 marks)

#!/usr/local/bin/perl5 -w
$b{"Apple"} = "Fruit";
$b{Oranges} = FruitFruit;
$b{"Orange"} = "Fruit";

COMP111 Fall 2006 HKUST 2 of 20


$b{Apples} = Fruits;
$b{Oranges} = Fruits;
___________ = ________________ ___________________ ;

Answer: %b = reverse %b; (0 or 3 marks)

COMP111 Fall 2006 HKUST 3 of 20


2) Perl I/O [5 marks]
No partial marks will be given for this question.

(a) What is the output of this perl program? (2 marks)

$ cat perlio1
#!/usr/local/bin/perl5 -w

@lines = <STDIN>;
@a = ();
$a = @lines;
print "$a\n";
$ perlio1
aaa
bbb
ccc
<CRTL-D>

Answer:
3

(b) Which of the following commands can correctly execute the following Perl program? (3
marks)

$ cat perlio2
#!/usr/local/bin/perl5 -w
while (<>)
{
print;
}
$ cat input
aaa
bbb
ccc

A) perlio2 input
B) perlio2 < input
C) cat input | perlio2
D) All of the above

Answer:
D

COMP111 Fall 2006 HKUST 4 of 20


3) Makefile [5 marks]

The program abc has the following dependencies,

a.cpp a.h c.h b.cpp b.h

a.o b.o

abc
The following are some standard output,
$ make
g++ -c a.cpp
g++ -c b.cpp
g++ -o abc a.o b.o
$ make
make: 'abc' is up to date.
$ make clean
rm *.o
$ ls
abc a.cpp a.h b.cpp b.h c.h Makefile

Please complete the Makefile. Use <TAB> to indicate a TAB character. No partial marks will
be given for each answer for this question. (5 marks)

Answer:

___________________________
g++ -o abc a.o b.o
___________________________
g++ -c a.cpp
___________________________
g++ -c b.cpp
___________________________

Answer:
abc: a.o b.o----(1)
g++ -o abc a.o b.o

a.o: a.h a.cpp c.h----(1)


g++ -c a.cpp

b.o: b.h b.cpp c.h----(1)


g++ -c b.cpp

clean: <TAB>rm *.o----(2)

COMP111 Fall 2006 HKUST 5 of 20


4) Perl Functions [9 Marks]

a) Briefly describe the function of use strict (2 marks)

Answer:
It forces all variables to require declaration with my() (i.e., it makes them all
local variables) (0 or 2 marks)

b) Describe how use strict can help in preventing typing mistake in


programming. (2 marks)

Answer:
All variables are local, so we will never reference wrong variable (belonging to
a different scope) because of typo. (0 or 2 marks)

c) Fill in the blank so that the output of the program is:


max: 5
max: 7
(5 marks)

#!/usr/local/bin/perl5 -w
$a = 1;
$b = 5;
$max = max($a, $b, 2);
print "max: $max\n";
$max = max($b, $b, 2, $b, 7, $b, 6);
print "max: $max\n";
_________________________{
$max = 0;
foreach ________________________{
if(______________________________){
______________________________;
}
}
return $max;
}
Answer:

$a = 1;
$b = 2;
$max = max($a, $b, 5);
print "max: $max\n";
$max = max($a, $b, 5, 6, 7);
print "max: $max\n";
sub max{ (0 or 1 mark)
$max = 0;
foreach $n (@_){ (0 or 2 marks)
if($n > $max){ (0 or 2 marks)
$max = $n;
}
}

COMP111 Fall 2006 HKUST 6 of 20


return $max;
}

COMP111 Fall 2006 HKUST 7 of 20


5) Perl File I/O [10 marks]
No partial marks will be given for each answer for this question.
(a) A Perl program, perlfile1, reads in a file removing any newlines, and produces the
following output: (8 marks)

Normal Case:
$ cat input
aaa
bbb
ccc
$ perlfile1 input output
$ perlfile1 input output
$ cat output
aaabbbcccc

Error Case:
$ perlfile1 input1 output
Error opening file input1:No such file or directory at
./perlfile1 line 6.
(Note: The program displays the error message Error opening
file input1, OS error message No such file or directory, and
the line the error occurs at ./perlfile1 line 6)
$ perlfile1 input output
Error opening file output
(Note: The program display error message Error opening file
output)

Complete the Perl program, perlfile1:

#!/usr/local/bin/perl5 -w
$infile = ____________________________________________;
$outfile = ____________________________________________;
unless(open(IN, ________________))
{
die ____________________________________________;
}
unless(open(OUT, ________________))
{
die ____________________________________________;
}
____________________________________________;
print OUT @lines;
close(IN);
close(OUT);

Answer:
$infile = $ARGV[0]; ----(1 mark)
$outfile = $ARGV[1]; ----(1 mark)
unless(open(IN, $infile)) ----(1 mark)
die "Error opening file $infile:$!"; ----(2 marks)
unless(open(OUT, ">$outfile")) ----(1 mark)
die "Error opening file $outfile\n"; ----(1 marks)
chomp(@lines = <IN>); ----(1 marks)

COMP111 Fall 2006 HKUST 8 of 20


(b) A Perl program, perlfile2, produces the following output: (2 marks)

$ perlfile2 dir
dir is a directory
$ perlfile2 /dev/null
/dev/null is not a plain file
$ perlfile2 input
aaa
bbb
ccc
$ cat input
aaa
bbb
ccc

Complete the perl program, perlfile2,

#!/usr/local/bin/perl5 -w
$infile = $ARGV[0];
if (_______________________________________________)
{
die "$infile is a directory\n";
}
if (_______________________________________________)
{
die "$infile is not a plain file\n";
}
unless(open(IN, $infile))
{
die "Error opening file $infile:\n";
}
@lines = <IN>;
print @lines;

close(IN);

Answer:
if (-d $infile)----(1 mark)
if (! -f $infile)----(1 mark)

COMP111 Fall 2006 HKUST 9 of 20


6) Regular Expressions [18 Marks]
No partial marks will be given for each answer for this question.

(a) The following program gets a binary number from the standard input, flips the 0s and 1s,
and then outputs to standard output. You may assume the user will only input 1 or 0. The
following is the output of the program (the underlined text is the user input). (4 marks)

$ regexp1
100010001
011101110
$ regexp1
0111100010
1000011101

Please complete the following Perl program, regexp1,

#!/usr/local/bin/perl5 -w

chomp($line = <STDIN>);
$line =~ ___________________________________________;
$line =~ ___________________________________________;
$line =~ ___________________________________________;

print "$line\n";

Answer:
$line =~ s/1/a/g; ----(1 mark)
$line =~ s/0/1/g; ----(1 mark)
$line =~ s/a/0/g; ----(1 mark)
(Note: Other characters can be used instead of a except 1 and
0)

Please write down the regular expression (without using the &&, ! and || operators) that can
match the pattern described.

(b) 3 numbers which are less than 1000 and they are separated by 2 periods (.). (4 marks)
E.g. 123.456.789 (Match)
1234.344.532 (Not Match)
12.34.67 (Match)
12.445.6789 (Not Match)

Answer: /^\d{1,3}\.\d{1,3}\.\d{1,3}$/

COMP111 Fall 2006 HKUST 10 of 20


(c) A Perl program matches the string which contains both abc and xyz. The following is
the output of that program (the underlined text is the user input): (5 marks)

$ regexp3
abcdefxyz
match
$ regexp3
xyzjklabc
match
$ regexp3
abcijk
not match
$ regexp3
xyzttt
not match

Please complete the Perl program:


#!/usr/local/bin/perl5 -w
chomp($line = <STDIN>);
if (______________________________________________________)
{
print "match\n";
}
else
{
print "not match\n";
}

Answer:
if ($line =~ /abc/ && $line =~ /xyz/)

COMP111 Fall 2006 HKUST 11 of 20


(d) A Perl program matches strings that start and end with the same character. The following is
the output of the program (the underlined text is the user input): (5 marks)

$ regexp5
aba
match
$ regexp5
a
match
$ regexp5
abed
not match
$ regexp5
ahji
not match
$ regexp5
%abd%
match

Please complete the Perl program:


#!/usr/local/bin/perl5 -w
chomp($line = <STDIN>);

if (_________________________________________________________)
{
print "match\n";
}
else
{
print "not match\n";
}

Answer:
if ($line =~ /^(.).*\1$/ || $line =~ /^.$/)

COMP111 Fall 2006 HKUST 12 of 20


7) Perl Directory Access [10 Marks]

In this question, you are required to complete a Perl program that can recursively change the
extension of files from .txt to .abc. No partial marks will be given for each answer for this
question. Here are some requirements of the program:
The first argument of the program is the starting directory, and you may assume only
relative paths will be used
From the starting directory, the program should change all the file extensions in sub-
directories as well
For any file operation (including changing directory and renaming files), the program
should exit if any errors occur.
You may assume each file name contains only 1 .. E.g. there will not be any
filenames such as file.txt.txt or file.abc.bak.

The following are sample output:

$ ls -R test
test:
a admin basic network

test/admin:
df.txt mount.txt rpm.txt special umount.txt

test/admin/special:
grep.txt veryspecial

test/admin/special/veryspecial:

test/basic:
cat.txt cp.txt ls.txt mv.txt rm.txt

test/network:
hostname.txt ifconfig.txt netstat.txt ping.txt route.txt
$ perldir test
$ ls -R test
test:
a admin basic network

test/admin:
df.abc mount.abc rpm.abc special umount.abc

test/admin/special:
grep.abc veryspecial

test/admin/special/veryspecial:

test/basic:
cat.abc cp.abc ls.abc mv.abc rm.abc

test/network:
hostname.abc ifconfig.abc netstat.abc ping.abc route.abc

COMP111 Fall 2006 HKUST 13 of 20


Complete the Perl program perldir,

#!/usr/local/bin/perl5 -w
#
use strict;
rlist($ARGV[0]);

sub rlist
{
my ($path) = @_;
#Change to the working directory
_____________________________________________________;

my @files _____________________________;
foreach my $file (@files)
{
if (-d $file)
{
rlist("$file");
#Change back to parent directory
______________________________________________;
}
if (-f $file)
{
my $oldname = $file;
if (___________________________________________)
{
____________________________________________;
}
}
}
}

Answer: (#2 marks each)


#Change to the working directory
chdir($path) || die "cannot change directory: $path\n";

my @files = <*>;

#Change back to parent directory


chdir("..") || die "cannot change directory\n";

if ($file =~ s/\.txt/\.abc/)
{
rename($oldname, $file) || die "cannot
rename\n";

COMP111 Fall 2006 HKUST 14 of 20


8) Perl Process Management [11 Marks]

(a) Write a Perl program which stores the result of the date command in a variable $now. [2
marks]

Answer:
$now = `date`; (2 marks)

(b)Write a Perl program which prints the lines containing the string abc in the file 123.txt. [2
marks]

Answer:
system("grep", "abc", "123.txt"); (2 marks)

(c) The following Perl program is supposed to display the first three
lines of each file in the current directory to the screen. What is
wrong? [4 marks]

#!/usr/local/bin/perl5 -w
foreach (<*>) {
open(IN, $_);
open(OUT, >head +3);
@data = <IN>;
print <OUT> @data;
close(IN);
close(OUT);
}

Answer:
open(IN, $_); (1 mark)
open(OUT, "|head -3"); (0, 1 or 2 marks)
print OUT @data; (1 mark)

(d)Fill in the blank so that the program below lists all users who are
logged on. [3 marks]

#!/usr/local/bin/perl5 w

_________________________________________{
($user, $loc, $time) = /(\S+)\s+(\S+)\s+(.*)/;
print "$user logged on $loc, at time $time\n";
}

Answer:
foreach $_ (`who`){
(3 marks for correct answer)
(1 mark only for any answer with $_ and who)

COMP111 Fall 2006 HKUST 15 of 20


COMP111 Fall 2006 HKUST 16 of 20
9) HTML [6 Marks]

Write a HTML document with the following characteristics:

(1) Homepage title: "COMP111 Final Exam QS9"


(2) Homepage background color: #666666
(3) Homepage Text color: white

(4) Homepage body:


- 1st line: A text :: HKUST ::", in bold text, font size of 8, aligned
at center.
- 2nd line: A picture located at
"http://www.ust.hk/en/index_img/logo.gif , aligned at center
- 3rd line: A link to http://www.ust.hk with text Link to HKUST
Homepage, aligned at center.
- 4th line: A horizontal rule.

A sample design view:

Answer:

<html>
<head>
<title>COMP111 Final Exam QS9</title>
</head>
<body bgcolor="#666666" text="white">
<p align="center"><B><font size = "8">:: HKUST ::</font></B></p>
<p align="center"><img
src="http://www.ust.hk/en/index_img/logo.gif"> </p>
<p align="center"><a href="http://www.ust.hk">Link to HKUST
Homepage</a></p>
<hr>
</body>
</html>

(0 or 0.5 mark for each of the below respectively)


<html></html>, <title></title>, <body></body>, bgcolor, text
color,
all <p></p>, <b>, <font size></font>, align=center,
<img src>, <a href></a>, <hr>

COMP111 Fall 2006 HKUST 17 of 20


10) CGI, CGI2 [10 Marks]

The Perl CGI program below is supposed to implement the game "Paper, Scissors,
Rock", with the user playing against the computer. Fill in the blanks.

(1) The initial screen is as follow:


- a heading text Paper, Scissors, Rock
- Horizontal rule
- a text What to pick?, then a popup menu with 3 choices paper scissors
rock
- a submit button with text select
- Horizontal rule

(2) The result screen is as follow:


- a heading text Paper, Scissors, Rock
- Horizontal rule
- a text User picks ______(Users guess), Computer picks _____ (Computers
guess)
- a text User ______ (Wins or loses)
- Horizontal rule

(3) You must force all the variables to be declared as local variables.

COMP111 Fall 2006 HKUST 18 of 20


#!/usr/local/bin/perl5 -w
use ____________________________________;
use ____________________________________;
srand;
___________________________________, print start_html("");
______________________________________________________________;
print hr;
if(param()){
my $guess = ____________________________________;
my $comp = ____________________________________;
print p("User picks $guess, Computer picks $comp");

if($comp eq $guess){
____________________________________;
}elsif((________________________________________________)
||
(________________________________________________)
||
(________________________________________________)){
print p("User wins!");
}else{
print p("Computer wins!");
}

}else{
print start_form();
_________________________________________________________;
_________________________________________________________;
print end_form;
}
print hr;
print end_html();

Answer:
use strict; (0.5 mark)
use CGI qw(:standard); (0.5 mark)
print header(), print start_html(""); (0.5 mark, with end_html)
print h1("Paper, Scissors, Rock"); (0.5 mark)
my $guess = param("choice"); (1 mark)
my $comp = (qw(paper scissors rock))[rand(3)]; (1.5 marks)

if($comp eq $guess){
print p("Draw"); (0 or 1 mark)
}elsif(($guess eq "paper" && $comp eq "rock") || (0 or 2
marks)
($guess eq "scissors" && $comp eq "paper") ||
($guess eq "rock" && $comp eq "scissors")){

print p("What to pick? ", popup_menu("choice", ["paper",


"scissors", "rock"])); (1.5 mark)
print p(submit("select")); (1 mark)

COMP111 Fall 2006 HKUST 19 of 20


11) Server Push & Client Pull [5 Marks]
The following Server-Push program is supposed to:
Update for 25 times:
- in the 1st update, the screen prints outputs 0 only,
- in the 2nd update , the screen outputs 00 only,
- in the 3rd update , the screen outputs 000 only,

- in the 24th update , the screen outputs 24 zeros,
- At the last screen (25th update), the screen prints out Done

Fill in the blanks.


P.S. assume the $counter value is 1 at the 1st update.

#!/usr/local/bin/perl5.00404 -w
use ___________________________ qw(:standard);
_______(-next_page=>__________, -last_page=>________,
-delay=>0.1);

sub next_page {
my($q, $counter) = _______________________________;
my $zero = ______________________________________;
if($counter >= 25) {_______________________________;}
______________________________________________________________;

}
sub last_page {
______________________________________________________________;

Answer:

#!/usr/local/bin/perl5 -w
use CGI::Push qw(:standard); (0.5 mark)
do_push(-next_page=>\&next_page, -last_page=>\&last_page,
-delay=>0.1);
(0.5 mark) (0.5 mark) (0.5 mark)

sub next_page {
my($q, $counter) = @_; (0.5 mark)
my $zero = "0" x $counter; (0.5 mark)
if($counter >= 25){return undef; } (0.5 mark)
return start_html(), $zero, end_html();
}
sub last_page {
return start_html(), DONE, end_html();
}

(ALL start_html and end_html 0.5 mark, zero 0.5 mark, DONE 0.5
mark)

COMP111 Fall 2006 HKUST 20 of 20

You might also like