Docsity

29 Practice Problems for Midterm Exam - Perl Programming | CSCI 2230, Study notes of Computer Science

Toggle sidebar
bg1
Programming in Perl
CSCI 2230
MidTerm Exam
Read and follow ALL directions:
1) Turn off your cellular phones!
2) Clear your desks of all books, computers, calculators, PDAs, etc.
3) You are allowed one 8.5 x 11" sheet of paper of your own notes.
4) Clearly print your name and RCS ID (NOT your RIN) on the top of EACH page.
5) Sit one seat away from any other student. Please position yourself so that both ends of the
row can be used.
6) No talking or collaboration during this exam.
7) If you have a question, approach Paul or one of the TAs.
8) You have until 6:00pm to complete the exam.
9) When completed, bring all pages of the exam to the table in front.
10) Good luck.
11) There is no rule 12.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download 29 Practice Problems for Midterm Exam - Perl Programming | CSCI 2230 and more Study notes Computer Science in PDF only on Docsity!

Programming in Perl

CSCI 2230

MidTerm Exam

Read and follow ALL directions:

  1. Turn off your cellular phones!

  2. Clear your desks of all books, computers, calculators, PDAs, etc.

  3. You are allowed one 8.5 x 11" sheet of paper of your own notes.

  4. Clearly print your name and RCS ID (NOT your RIN) on the top of EACH page.

  5. Sit one seat away from any other student. Please position yourself so that both ends of the

row can be used.

  1. No talking or collaboration during this exam.

  2. If you have a question, approach Paul or one of the TAs.

  3. You have until 6:00pm to complete the exam.

  4. When completed, bring all pages of the exam to the table in front.

  5. Good luck.

  6. There is no rule 12.

True or False. For each statement, circle either TRUE or FALSE. (1 point each)

  1. To make your program executable, the shebang must be at the top of your Perl code, but may

come after any comments. TRUE FALSE

  1. $a = 5, $a = 7.5, $a = ‘1’, $a = “Hi!!”, and $a = ‘’ are all valid

assignments in Perl. TRUE FALSE

  1. The print statement must take at least one argument. TRUE FALSE

  2. The statements $foo = <>; and @foo = <>; do the same thing. TRUE FALSE

  3. The ‘default variable’ for many functions is $0. TRUE FALSE

  4. The keywords for and foreach mean the same thing, and are interchangeable.

TRUE FALSE

  1. 0, '', and '000' all evaluate to false in boolean context. TRUE FALSE

  2. 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

  1. Even if @cs has not previously been used as an array, you cannot safely print out a string

such as “My address is [email protected].” TRUE FALSE

  1. push and unshift mean the same thing, and are interchangeable, as are pop and

shift. TRUE FALSE

  1. What is the difference in output between these two statements?

print “Hello World!\n”;

print ‘Hello World!\n’;

  1. Describe the two strings $str1 and $str2 after executing the following code:

$str1 = “/home/36/lallip/public_html/”; $str2 = $str1; $str1 =~ s#/#\#; $str2 =~ s/\*g;

  1. if %my_hash contains

(‘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?

  1. List any six of the 'dirty dozen' – characters that don't naturally match themselves inside a

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 = ; @nums = split (/,/, $list); @sorted = sort @nums; print "The sorted numbers are: @sorted\n";

#!/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 = ; @words = split (/ /, $string); #(space between the /’s) $size = $words; print "There are $size words in the string\n";

What’s the output? For each code chunk below, give the output of the code.

  1. 6 points @foo = (1..10); foreach $bar (@foo){ if ($bar % 2 == 0){ $bar /= 2; } else { $bar *= 2; } } print "@foo\n";

  2. 7 points for ($i = 0; $i < 10; $i++){ $numbers[$i] = $i * 3; } $rev = reverse @numbers; print "Reversed numbers = $rev\n";

  3. 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):

  1. 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

  2. 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

  3. 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