=========================preview======================
(COMP111)final-Fall2006.pdf
Back to COMP111 Login to download
======================================================
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
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 Cw
@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 Cw
$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 Cw
$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 Cw
$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";
$b{Apples} = Fruits;
$b{Oranges} = Fruits;
___________ = ________________ ___________________ ;
Answer: %b = reverse %b; (0 or 3 marks)