Sample Questions for Quiz 1

(Answers below)
  1. Describe the output of the following program when the user inputs the number 2? The number 3? The number 4? The number 5? The number 6?
    print "Input a number: ";
    $Num = <>;
    chomp $Num;
    if (($Num % 2)==0) {
      print "That's divisible by 2!\n";
    }
    if (($Num % 3)==0) {
      print "That's divisible by 3!\n";
    }
    if (($Num % 4)==0) {
      print "That's divisible by 4!\n";
    }
    
  2. Describe the output of the following program when the user inputs the number 2? The number 3? The number 4? The number 5? The number 6?
    print "Input a number: ";
    $Num = <>;
    chomp $Num;
    if (($Num % 2)==0) {
      print "That's divisible by 2!\n";
    } 
    elsif (($Num % 3)==0) {
      print "That's divisible by 3!\n";
    }
    elsif (($Num % 4)==0) {
      print "That's divisible by 4!\n";
    }
    
  3. Consider the following Perl program:
    print "Please input an animal: ";
    $Anim1 = <>;
    chomp $Anim1;
    
    print "Please input another animal: ";
    $Anim2 = <>;
    chomp $Anim2;
    
    if ($Anim1 gt $Anim2) {
      print "The $Anim1 ate the $Anim2!\n";
    } else {
      print "The $Anim2 ate the $Anim1!\n";
    }
    
    Describe the output of the program for the following pairs of inputs:
    (a) "lion" and "tiger"
    (b) "tiger" and "lion"
    (c) "fish" and "tiger"
    (d) "tiger" and "Ant"
    (e) "ant" and "Tiger"

  4. Describe the output of the following program:
    $Str = "";
    $Count = ord("A");
    while ($Count <= ord("G")) {
      $Let = chr($Count);
      $Str = "$Let$Str";
      $Count++;
    }
    print "$Str\n";
    
  5. Describe what happens if you run the following Perl program:
    $Count = 1;
    while ($Count<=10) {
      $CountSqr = $Count**2;
      print "$Count $CountSqr\n";
    }
    
  6. Describe what happens if you run the following Perl program:
    $Str = "";
    for ($Count=0; $Count<10; $Count++) {
      $Str = "$Str$Count";
    }
    print "$Str\n";
    
  7. Describe what happens if you run the following Perl program:
    $Str = "";
    for ($Count=0; $Count<10; $Count++) {
      $Str = "$Str$Count";
      print "$Str\n";
    }
    
  8. Write a perl program to print out (on the same line or on separate lines), the integers between 1 and 200 that are divisible by both 3 and 4.

  9. Write a perl program that asks for a positive integer as input from the user and outputs "Yes, it is prime!\n" if the number is prime and "No, it is not prime!\n" if it is not. Remember that a number N is prime if it is not divisible by any integer greater than 1 and less than N. (Equivalently, a number is not prime if it is evenly divisible by some (any) number greater than 1 and less than N.)

  10. Consider the listing of a directory below:
    [perkins] ls -l
    total 72
    -rw-r--r--   1 perkins  perkins  117 Feb  9 10:47 DivBy3And4.pl
    -rw-r--r--   1 perkins  perkins  227 Feb  9 11:19 Divisible1.pl
    -rw-r--r--   1 perkins  perkins  208 Feb  9 11:23 Divisible2.pl
    -rw-r--r--   1 perkins  perkins   91 Feb  9 10:51 InfLoop.pl
    -rw-r--r--   1 perkins  perkins  267 Feb  9 11:15 Prime.pl
    -rw-r--r--   1 perkins  perkins   91 Feb  9 10:58 Print0to9.pl
    -rw-r--r--   1 perkins  perkins   92 Feb  9 11:00 Print0to9Triangle.pl
    -rw-r--r--   1 perkins  perkins  133 Feb  9 10:45 ReverseChars.pl
    -rw-r--r--   1 perkins  perkins  245 Feb  9 11:36 WhoAteWho.pl
    [perkins]
    
    (a) What would be the result of the command "ls D*"?
    (b) What would be the result of the command "ls *i*"?
    (c) What would be the result of the command "ls Pri*> NewFile.txt"?











Answers

  1. For 2, the program prints that it's divisible by 2. For 3, it prints divisible by 3. For 4 it prints divisible by 2 and by 4. For 5 it prints nothing. For 6 it prints divisible by 2 and by 3.

    Explanation: The program tests whether the input number is divisible by 2, 3 and 4. For each of those that divide the input number, it prints out.

  2. For 2, the program print that it's divisible by 2. For 3, it prints divisible by 3. For 4 it prints divisible by 2. For 5, it prints nothing. For 6, it prints divisible by 2.

    Explanation: This program is similar to the previous one, but the second and third tests are "elsif" rather than just "if". That means, the conditions are tested only if the previous conditions have failed. So, for example, when the input is 2, the program finds that 2 is divisible by 2 and it never tests whether 2 is divisible by 3 or 4. Similarly when the input is 4 -- the program finds 4 is divisible by 2, and never tests either of the other conditions.

  3. The results are:
    (a) The tiger ate the lion!
    (b) The tiger ate the lion!
    (c) The tiger ate the fish!
    (d) The tiger ate the Ant!
    (e) The ant ate the Tiger!

    Explanation: Refer to the ASCII table to determine which strings are greater than which.

  4. This program prints out the string "GFEDCBA" on a line.

    Explanation: The while loop is used to count the $Count variable from the ASCII index of "A" to the ASCII index of "G". For each of these indeces, the ASCII letter is added to the start of $Str. So, $Str starts as "", then after one time through the while loop is "A", then after another time is "BA", and so on. At the end, $Str is printed out.

  5. It prints out: "1 1" on separate lines repeatedly, without end. Like this:

    1 1
    1 1
    1 1
    1 1
    1 1
    (and so on...)
    
    Explanation: Although at first it might appear that the while loop is counting $Count from 1 to 10, $Count is not changed inside the while loop. (Ordinarily, we would have a line like "$Count++;" inside the while loop.) As such, $Count never changes from 1, and the while loop keeps repeating forever.

  6. It prints out "0123456789" on a line.

    Explanation: The for loop steps $Count from 0 to 9. More precisely, it initially sets $Count to 0. After each time through the loop, it adds 1 to $Count, and it stops when $Count==10. Each time through the loop, $Count is added to the end of $Str, which thus ends up being 0123456789.

  7. It prints out:
    0
    01
    012
    0123
    01234
    012345
    0123456
    01234567
    012345678
    0123456789
    
    Explanation: This program is very similar to the previous one, except that the string $Str is printed out each time through the for loop. In otherwords, it is print out when it is just "0", when it is "01", when it is "012", and so on.

  8. There are, of course, many ways to answer this question. One straightforward way is below.

    for ($Count=1; $Count<=200; $Count++) {
      if ((($Count % 3)==0) && (($Count % 4)==0)) {
        print "$Count\n";
      }
    }
    
    Explanation: The for loop counts from 1 to 200. The "if" checks if the number is divisible by 3 and by 4. If so, the number is printed out.

  9. Here is one way the program could be written:
    print "Please input a positive integer: ";
    $Num = <>;
    chomp $Num;
    $IsPrime = 1;
    for ($Count = 2; $Count<$Num; $Count++) {
      if (($Num % $Count)==0) {
        $IsPrime = 0;
      }
    }
    if ($IsPrime) {
      print "Yes, it is prime!\n";
    } else {
      print "No, it is not prime!\n";
    }
    
    Explanation: The program first takes the number as input. Then it sets $IsPrime to 1. This indicates that, as far as the program has checked so far, the input number is prime. The "for" loop counts from 2 to the number minus one. If it finds a number that divides the input number, it sets $IsPrime to 0. Thus, when the "for" loop ends, if no number was found that divides the input number, $IsPrime is still 1. Conversely, if a divisor was found for the input number, then $IsPrime is 0. The final "if" statement prints the correct output, depending on $IsPrime.

  10. (a) This lists all files in the directory beginning with "D", which produces:
    DivBy3And4.pl   Divisible1.pl   Divisible2.pl
    
    (b) This lists all files in the directory that have an "i" in them anywhere, producing:
    DivBy3And4.pl           Divisible2.pl           Print0to9.pl
    Divisible1.pl           Prime.pl                Print0to9Triangle.pl
    
    (c) This lists all files in the directory beginning with "Pri" and puts the results in a new file called "NewFile.txt". The contents of that file is thus:
    Prime.pl                Print0to9.pl            Print0to9Triangle.pl