=========================preview======================
(COMP111)comp111_99s_final_sol.pdf
Back to COMP111 Login to download
======================================================
THE HONG KONG UNIVERSITY OF SCIENCE & TECHNOLOGY
COMP111: Software Tools

Final Exam
Spring 1999
31 May 1999
Student Name: _______________________________________________

Student Number: _____________________________________________

Lab Section: ________________________________________________

TA: _________________________________________________________

Instructions:
1.
There are 20 problems worth 100 points total.

2.
Check that you have all 13 pages.

3.
Answer all questions in the space provided, and circle your answer. Rough work can be done only on the back pages.

4.
Leave all pages stapled together.

5.
The examination period will last for 120 minutes.

6.
Stop writing immediately when the time is up.



For Grading Purposes Only:
Page 2: Problem 1-2 ______________ / 5
Page 3: Problem 3-4 ______________ / 6
Page 4: Problem 5 ______________ / 5
Page 5: Problem 6-8 ______________ / 12
Page 6: Problem 9-10 ______________ / 6
Page 7: Problem 11-12 ______________ / 7
Page 8: Problem 13-14 ______________ / 8
Page 9: Problem 15-16 ______________ / 9
Page 10: Problem 17 ______________ / 13
Page 11: Problem 18 ______________ / 7
Page 12: Problem 19 ______________ / 14
Page 13: Problem 20 ______________ / 8

Total: ______________ /100

Array basics:
1) (2 points) What is the value of $nafter the following Perl statements are executed?

$n = 4;
@a = (1,2,3);
for($i=0; $i<=$#a; $i++){

$n += $a[$i];
}

Answer: 10
List Basics:
2) (3 points) What is the value of @aafter the following Perl statements are executed?

@b = (5,6,7);
foreach (reverse @b){

do{
push(@a, $_);
$_--;

}until($_==4);
}

Answer: 7 6 5 6 5 5 1 point: left 7 6 5 1 point: middle 6 5 1 point: right 5
Hash:
3) (3 points) What is the output?

#!/usr/local/bin/perl5 -w
$n = 2;
$bill{"Gates"} = "rich and famous";
$bill{"Mr."} = "too excited";
$bill{1} = "very rich";
$bill{$n} = "very excited";
$bill{3} = 67;
@a = (3,2,1);
print "@bill{@a}\n";

Answer:
67 very excited very rich 1 point: left 67 1 point: middle very excited 1 point: right very rich
Hash:
4) (3 points) What is the output?

#!/usr/local/bin/perl5 -w
$bill{"Gates"} = "rich";
@bill{"Clinton","Mr."} = qw(very nervous);
@bill{4..7} = qw(very famous + nervous);
delete($bill{5});
delete($bill{6});
@a = sort values %bill;
%bill = reverse %bill;
print "@a\n";

Answer:
nervous nervous rich very very

2 points: correct items (-1 for each wrong item, up to max -2) 1 point: correct order
Perl I/O:
5) (5 points) Write a Perl program like cat, but that reverses the order of all the lines in the file
or files specified on the command line.

For example, cat f1 f2, where f1and f2 have the following contents: f1 f2 13 24
would print the following:
4
3
2
1

Answer:
#!/usr/local/bin/perl5 -w
2 while(<>){
1 push(@lines, $_);

#or: @lines = (@lines, $_);
}
2 print reverse @lines;

or:
#!/