PrevIndexNext

Sample MidTerm Test

100 points total.

True/False

(2 points each)
  1. Use == to see if $name is equal to "Joe".
  2. Variable names can be longer than 64 characters.
  3. chomp does exactly the same thing that chop does.
  4. The index function returns 0 if it cannot find the string it is searching for.
  5. To open a file for writing precede the filename with "<".

Multiple Choice

(2 points each)
  1. Which of these is an invalid Perl keyword or function name?
    1. while
    2. chomp
    3. substring
    4. elsif
  2. (4**3) / 8 is equal to:
    1. 64
    2. 1
    3. 1.3333
    4. 8
  3. If $s = "Hello there." what is length($s)?
    1. 10
    2. 2
    3. 12
    4. 11
  4. Which of these are not valid?
    1. $x = 5_100_341;
    2. $x = 3.14159;
    3. $x = 0xEF0H;
    4. $x = 45.6e-3;
  5. What is the value of this expression?
    substr("hello there how are you", 10, 3)
    1. "how"
    2. "re "
    3. "lo there h"
    4. "e h"

Fill In The Blanks

(2 points each)
  1. Embedding a variable inside a double quoted string is called ______________.
  2. You can replicate a string with the _____ operator.
  3. Instead of || and ! you can use the more readable ____ and _____.
  4. You can exit a while loop early with the ______ statement.
  5. 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)
  1. Compute the square of $x divided by the cube of $y-1.
  2. Open the file "names.txt" for output.
  3. Add 3 to $count.
  4. Write this in a more compact form:
    print "The count is ", $count, " and age is ", $age, "\n";
  5. 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
  1. 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.

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