





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Read and follow ALL directions:
Turn off your cellular phones!
Clear your desks of all books, computers, calculators, PDAs, etc.
You are allowed one 8.5 x 11" sheet of paper of your own notes.
Clearly print your name and RCS ID (NOT your RIN) on the top of EACH page.
Sit one seat away from any other student. Please position yourself so that both ends of the
row can be used.
No talking or collaboration during this exam.
If you have a question, approach Paul or one of the TAs.
You have until 6:00pm to complete the exam.
When completed, bring all pages of the exam to the table in front.
Good luck.
There is no rule 12.
True or False. For each statement, circle either TRUE or FALSE. (1 point each)
come after any comments. TRUE FALSE
assignments in Perl. TRUE FALSE
The print statement must take at least one argument. TRUE FALSE
The statements $foo = <>; and @foo = <>; do the same thing. TRUE FALSE
The ‘default variable’ for many functions is $0. TRUE FALSE
The keywords for and foreach mean the same thing, and are interchangeable.
TRUE FALSE
0, '', and '000' all evaluate to false in boolean context. TRUE FALSE
If you choose the front slash ( / ) as your delimiter, you do not need to type the ‘s’ in the
search-and-replace operator s///. TRUE FALSE
such as “My address is [email protected].” TRUE FALSE
shift. TRUE FALSE
print “Hello World!\n”;
print ‘Hello World!\n’;
$str1 = “/home/36/lallip/public_html/”; $str2 = $str1; $str1 =~ s#/#\#; $str2 =~ s/\*g;
(‘Jan’ => 31, ‘Feb’ => 28, ‘Mar’ => 31, ‘Apr’ => 30, ‘May’ => 31, ‘Jun’ => 30, ‘Jul’ => 31,
‘Aug’ => 31, ‘Sept’ => 30, ‘Oct’ = 31, ‘Nov’ => 30, ‘Dec’ => 31)
what are the contents of the array returned by keys %my_hash?
pattern match.
What’s Wrong Here? Each of the following chunks of code do not work (ie, either they do not compile, or do not have the intended effect). State why the code does not work as is, and explain how to correct it. You may assume the user does exactly what he/she is told to do. (5 points each)
#!/usr/bin/env perl –w #read a list of numbers from the user (assume the user only #enters valid numbers) and print them out sorted lowest to #highest. print "Enter a list of numbers, seperated by commas\n"; $list =
#!/usr/bin/env perl –w #This program gets a sentence from the user and prints #the number of words in that sentence print “Enter a sentence:\n”; $string =
What’s the output? For each code chunk below, give the output of the code.
6 points @foo = (1..10); foreach $bar (@foo){ if ($bar % 2 == 0){ $bar /= 2; } else { $bar *= 2; } } print "@foo\n";
7 points for ($i = 0; $i < 10; $i++){ $numbers[$i] = $i * 3; } $rev = reverse @numbers; print "Reversed numbers = $rev\n";
7 points @things = ('poster', 'desk', 'cat', 'dog', 'cat', 'poster', 'chair', 'catalog', 'manual', 'road', 'rodeo'); foreach $thing (@things){ $d++ if $thing =~ /^d.*/; $cats++ if $thing =~ /cat/; } print "There are $d 'D-Words', and $cats cats in the list\n";
Code it! Write a small program to do each of the following tasks (do not concern yourself with error checking):
Prompt the user for an input. Print out a status message informing the user if the input is a positive integer, a negative integer, or not a valid integer at all. 10 points
Write a simplified version of the Unix cat command. This command reads one or more file names from the command line, and prints their contents to the screen (with no additional information at all – just the contents of the first file, immediately followed by the contents of the 2 nd^ file, etc etc). 10 points
Prompt the user for one word at a time. User will enter the word done (case insensitive) when all words are finished being entered. Print the number of times each word was entered. 15 points