PrevIndexNext
Sample MidTerm Test
100 points total.
True/False
(2 points each)
- Use == to see if $name is equal to "Joe".
- Variable names can be longer than 64 characters.
- chomp does exactly the same thing that chop does.
- The index function returns 0 if it cannot find
the string it is searching for.
- To open a file for writing precede the filename with "<".
Multiple Choice
(2 points each)
- Which of these is an invalid Perl keyword or function name?
- while
- chomp
- substring
- elsif
- (4**3) / 8 is equal to:
- 64
- 1
- 1.3333
- 8
- If $s = "Hello there." what is length($s)?
- 10
- 2
- 12
- 11
- Which of these are not valid?
- $x = 5_100_341;
- $x = 3.14159;
- $x = 0xEF0H;
- $x = 45.6e-3;
- What is the value of this expression?
substr("hello there how are you", 10, 3)
- "how"
- "re "
- "lo there h"
- "e h"
Fill In The Blanks
(2 points each)
- Embedding a variable inside a double quoted string
is called ______________.
- You can replicate a string with the _____ operator.
- Instead of || and ! you can use the more readable ____ and _____.
- You can exit a while loop early with the ______ statement.
- The index function returns -1 if ______________________.
Finding Errors
Find the 10 syntax and indentation errors in the
following program fragment (2 points each):
while ($z == 3 and $x << 4)
if ($y+3)*4/5) {
$sum_of_all_entries += 4;
print "hello, how are you?"
print "goodbye! and good luck!/n";
break;
}
if (45 < $x and $x < 60) {
print "names" "values", 45;
open OUT, "> $fname" or die "oops cannot open: $!';
}
}
Write program fragments
(2 points each)
- Compute the square of $x divided by the cube of $y-1.
- Open the file "names.txt" for output.
- Add 3 to $count.
- Write this in a more compact form:
print "The count is ",
$count, "
and age is ",
$age, "\n";
- Add 3 to $total_count_of_the_complex_entries.
Create Programs
Create programs to do the following tasks.
Submit a printout along with your test paper.
Each is worth 25 points graded like so:
proper function - 15 points
syntactic correctness - 5 points
proper indentation and formatting - 5 points
- Ask the user for their name.
If their name begins with an "S"
ask for two numbers and print the user's name
followed by the sum of the numbers.
If their name does not begin with an "S"
ask for a number and print "Congratulations!"
that many times.
- Read lines from the file "names.txt".
Skip lines longer than 10 characters (excluding the newline).
If a line has a 'Z' in it print it into a file named "ZZZ.txt".
Otherwise print the line to standard output.
At the end print how many lines there were in all.
Extra credit
(15 extra points)
Calculate the value of pi with this formula:
pi = 4 * (2/3 * 4/3) * (4/5 * 6/5) * (6/7 * 8/7) * (8/9 * 10/9) * ...
See the pattern? Ask if you don't.
This is clearly a place for an infinite loop
but when will you stop? This formula is called the
Wallis Product
and was discovered by
John Wallis in 1656.
PrevIndexNext