Docstoc

Programming in Perl

Document Sample
Programming in Perl Powered By Docstoc
					Name_________________________________________

RCS Id__________________

Programming in Perl CSCI-2962 Midterm Exam
Rules: 1) TURN OFF CELULAR PHONES AND PAGERS 2) DO NOT BEGIN this exam, or turn to the next page, until Paul has said “Begin.” 3) Clearly print your Name and RCS Id (not your RIN!) on the top of EVERY page. 4) You may use ONE 8.5” x 11” piece of paper containing typed or handwritten notes. 5) Close all books and notebooks. Turn off all computers. Put away calculators, PDAs, etc. 6) Sit at least one seat away from any other student. Please position yourselves so that both ends of the row may be used. 7) No talking or collaboration during the exam. 8) If you have a question, raise your hand. Paul or one of the TAs will come to you. 9) Throughout the exam, “Write a program” or “Write a script” means to give an entire, complete program, starting with the shebang. “Write a chunk of code”, “Write a code fragment”, or “Write a piece of code” means to give just enough lines of code to complete the task at hand. 10) You have until 6:00pm EDT to complete this exam. 11) Bring the exam to the front desk when you are done, and leave as quietly as possible. 12) Solutions to this exam will be online after class today. 13) Grades will be posted as the exams are corrected. You may check your grades online periodically to see if yours has been graded yet. Also check the distribution script (off the check grades results) to see how many exams have been graded so far. 14) Good luck. 15) There is no rule 15.

Name_________________________________________ True/False – 1pt each For each question below, circle either TRUE or FALSE.

RCS Id__________________

1) TRUE FALSE For any array @foo, regardless of size, the value $#foo is numerically equivalent to the value scalar(@foo). 2) TRUE FALSE Because the regular expression modifiers /m and /s are opposites, it is illegal syntax to use both of the modifiers on one single pattern match. 3) TRUE FALSE The ++ operator is „magical‟, and will „increment‟ strings as well as numbers, but the -operator is not, and will work only on numbers.

4) TRUE FALSE The * quantifier on a regular expression will match one or more of the previous character. 5) TRUE FALSE The keys() and values() functions return the keys or values of a hash in the same order in which the key/value pairs were added to the hash. 6) TRUE FALSE If you provide no function to sort(), it sorts in ASCIIbetical order, not numerically. 7) TRUE FALSE Putting the ++ operator before or after the variable is syntactic sugar. ie, $a = $b++; is equivalent to $a = ++$b; == is a numerical operator, and eq is its string operator equivalent.

8) TRUE FALSE

9) TRUE FALSE Putting a statement before or after the if conditional is syntactic sugar. ie, print “yes” if $i > 0; is equivalent to if ($i > 0) { print “yes”; } 10)TRUE FALSE || is a numerical operator, and or is its string operator equivalent.

-1-

Name_________________________________________ RCS Id__________________ Multiple Choice – 2pts each For each question below, write the letter that corresponds to correct answer to the question. 1) How are $foo[1] and @foo[1] related? A They are not related at all B @foo[1] is a list containing only the element $foo[1] C Trick question - @foo[1] is illegal syntax D There is no difference at all 2) How are $foo and @foo related? A They are not related at all B $foo is the last index of @foo C $foo is the size of @foo D $foo is the first element of @foo 3) How are @foo and $#foo related? A They are not related at all B $#foo is the size of @foo C Trick question - $#foo is illegal syntax D $#foo is the last index of @foo 4) What is special about a regular expression delimited by question marks? A) The variables within the RegExp are interpolated only once within a loop. B) There is nothing special - ? is just another delimiter C) The regular expression itself is only done once within a loop. D) The regular expression is negated. 5) Which of these is the proper order of splice arguments? A) splice ARRAY, OFFSET, LENGTH, LIST B) splice LIST, ARRAY, OFFSET, LENGTH C) splice ARRAY, LENGTH, OFFSET, LIST D) splice OFFSET, ARRAY, LENGTH, LIST ______

______

______

______

______

-2-

Name_________________________________________ Short Answer – 2pts each Please write a short answer to each of the following questions.

RCS Id__________________

1) List all of the values which evaluate to false when used in a Boolean context. Note that for any string that does not contain variables, “string” is equivalent to „string‟, and should not be listed separately.

2) Write one single regular expression that will remove leading AND trailing white space from a string. Ex: $string = “\t This is a string with some spaces \n ”; $string =~ YOUR REGULAR EXPRESSION HERE print $string; #prints: “This is a string with some spaces”

3) Explain the difference between using a comma or a period in a print statement. For example: print “Hello ”, “World”, “\n”; vs print “Hello ” . “World” . “\n”; What is happening with each print statement? Is the output the same or different? (If different, how?)

4) Re-write the following code chunk without using any loops or any push, unshift, or splice statements. foreach (@lines){ push @Cap, $_ if /^[A-Z]/; push @Lower, $_ if /^[a-z]/; push @Dups, $_ if /(\w)\1/; }

5) What is the difference between running an external program using back-ticks and running the program using the system() function?

-3-

Name_________________________________________ RCS Id__________________ What‟s the Output? – 6 or 7pts each For each code fragment below, give the output of the code. Be sure it is clear whether or not there is a space between characters. Use  to indicate a newline. 1) 6pts @array = (5..15); while (++$i < $#array){ $array[$i] += 10; } print “Array now contains: @array\n”;

2) 6pts @nums = (1, 8..12, 20); @rev_nums = reverse @nums; $rev_nums = reverse @nums; print “@nums\n”; $, = “|”; $” = “\n”; print “@rev_nums\n”; print “$rev_nums\n”;

3) 7pts. For this program, assume the file “file1.txt” has been passed in on the command line. This file exists, and it contains the previous page of this exam. (ie, everything from “Short answer – ” to “system() function?”; it does not include your answers on that page). #!/usr/bin/env perl –w @lines = <>; $oneline = join („‟, @lines); @foo = $oneline =~ /(?:\$|\@)\w+/g; print “\@foo contains @foo\n”; foreach (@lines){ print if /^\d/; }

-4-

Name_________________________________________ RCS Id__________________ What‟s Wrong? – 5pts each Each code fragment below does not work. Either it does not compile, or it does not do what it is intended to do. For each, give the reason the code does not work, and explain how to correct the error. You may always presume the user does what he/she is told to do. 1) #This code reads two numbers from the user, compares them, #and prints a status message. @my_nums = (); print “Enter any two numbers, separated by newlines. ”; print “Press CTRL-D when done.\n”; @my_nums = <STDIN>; if ($my_nums[$#my_nums – 1] == $my_nums[-1]) print “You entered two identical numbers\n”; else print “You entered two different numbers\n”;

2) #Take in a list of words from a file, and print them out in sorted order, #along with the frequency of each word. #Sorting “ASCIIbetically” is fine open FILE, “words.txt” or die “Cannot open words file: $!\n”; while (<FILE>){ @temp = split / /; push @words, @temp; } @sorted = sort { $a cmp $b } @words; foreach (@sorted){ $words{$_}++; } foreach (keys %words){ print “$_ appeared $words{$_} times\n”; }

-5-

Name_________________________________________ RCS Id__________________ 3) #Read in the files on the command line. #These files consist of text from a book. #Print the number of times each lowercase letter has appeared in the file. while (<>){ @lower = /[^A-Z]/g; foreach $let (@lower){ $lower{$let}++; } } print “Here are the lowercase letters in the file\n”; while (($key, $val) = each %lower){ print “Letter $key appeared $val time” . ($val > 1 ? “s.\n” : “.\n”); }

-6-

Name_________________________________________ Code It! – 10 or 15pts each For each task below, write a program or code fragment to complete the task.

RCS Id__________________

1) 10 pts Write a program that will prompt the user for an arithmetic expression consisiting of two integer operands and one operator. The operator must be one of +, -, *, /, or %. The expression entered may or may not contain whitespace between the operands and operator. Once the expression has been entered, print the result of that operation. In your output, exactly one space should separate each operand and operator. If the user enters something that doesn‟t qualify as an expression as described above, print an error message. A negative number may be entered, provided the – sign immediately preceeds the number Here are three sample executions of your program (user input normal font, program output in bold font): Enter an arithmetic expression: 4+ 7 4 + 7 = 11 Enter an arithmetic expression: 5*3 5 * 3 = 15 Enter an arithmetic expression: 45 / -18 45 / -18 = -2.5

-7-

Name_________________________________________ RCS Id__________________ 2) 10 pts Write a program that prompts the user for a file name. Open the file (print a suitable error message if it does not exist). Read all lines of the file. Create a new file, whose name is the old file name appended with “.new”. In this file, print all lines out in reverse order, and each line in reverse order. Also change all uppercase letters to lowercase and vise versa. Example: file.txt file.txt.new Hello World This is my sample file. Isn’t it nice? I thought so. .OS THGUOHT i ?ECIN TI T’NSi .ELIF ELPMAS YM SI SIHt DLROw OLLEh

-8-

Name_________________________________________ RCS Id__________________ 3) 15 pts Assume that some piece of code has set both @a and @b, both of which are proper sets. Write a code fragment to compute the Union and the Intersection of the two sets. For example, if @a = (3, 2, 6, 4, 1, 5) and @b = (4, 7, 9, 6, 5, 8), then when your code is done, @union  (3, 2, 6, 4, 1, 5, 7, 9, 8) and @intersection  (6, 4, 5). Your arrays need not be in any particular order.

-9-

Name_________________________________________ Bonus – 5pts

RCS Id__________________

Below is a perlscript based on the code on the back of my shirt. Even as horrendously formatted as it is, it still compiles and produces some output. For five bonus points, give the output of this program. #!/usr/bin/env perl –w @a=('',Ezqmdc,Jtrs,Hzud,Ynt, Pnhmsr); $b="Cnmfqzstzshnmr" ; $b=~y/a-z/b-za/; $c ="A" . "ppp".("o"x5)."vt"."tt ";for ($i=@a-2;$i>0;$i--){$a[$i]=~ y/a-z/b-za/;print "$a[$i] "; }$b .= "!\n";shift @a; print @a-0; $c=~y/A-Yb-z/B-Za-y/s; print " $c";$a[$#a]=~tr/a-x/ c-z/;print $a[-1],"\n\t",$b;

- 10 -

Do not write your name here!

CSCI 2962 Programming in Perl Midsemester Course Evaluation
When you have completed the exam, please fill out this evaluation. When you‟ve completed the evaluation, detach this page and bring it to the front of the room along with the exam. Put this evaluation on the left side of the desk, and the exam on the right side. 1) What is your major? _____________________________ 2) What is your class year (circle one)? Freshman Sophomore Junior Senior Graduate other________ 3) How much Perl did you know before registering for this course? Ο None at all Ο Less than we‟ve covered so far Ο More than we‟ve covered so far Ο I‟m an expert Ο Just about what we‟ve covered up to now

4) Please rate the following aspects of the class from 1 (not at all helpful) to 5 (very helpful), as it applies to your learning of the Perl language Class Lectures Class Notes posted online Homework Paul‟s office hours TAs‟ office hours email communication with Paul and/or TAs Paul‟s „virtual‟ (ie: AIM) office hours Course website The midterm exam you just took 5) How many of the first 6 lectures did you attend? _______ 6) What do you expect your grade in the course to be? A B C D F 7) How often does Paul … ? (1 = Never, 2 = Infrequently, 3 = Occasionally, 4 = Frequently, 5 = All the time) Cast a shadow on the screen as he‟s writing on the overhead Put you to sleep during lecture Refer to something on his computer that isn‟t projected on the screen Mumble or speak too quickly for you to understand Not understand or dismiss your question without answering it Other? _____________________________________________ 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 N/A N/A N/A N/A N/A N/A N/A N/A N/A 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5

Please write any comments you may have on the back of this page. Any criticisms, complaints, or compliments are welcome. These evaluations will not be looked at until after the exam has been graded.


				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:29
posted:11/29/2009
language:English
pages:12
Jun Wang Jun Wang Dr
About Some of Those documents come from internet for research purpose,if you have the copyrights of one of them,tell me by mail vixychina@gmail.com.Thank you!