CS 290W Exam 2 Key

Fall 1999

Answers are in RED. Questions 12 and 40 were not graded (discarded).

JavaScript

  1. So that the saySomething function below executes properly, you must use the return function on the event handler as demonstrated below.
  2. (a) true

    (b) false

    The function:

    saySomething(message) {

    alert(message)

    }

    The call to the function:

    <INPUT TYPE=BUTTON VALUE="Hello" onClick="return(saySomething('HI'))">

  3. Regarding the saySomething function above, suppose that instead of using the alert method the confirm method is used. You must use the return function on the event handler as demonstrated above.
  4. (a) true

    (b) false

  5. The confirm_submit() function, demonstrated in class and used in assignment 2, utilized the alert method.
  6. (a) true

    (b) false

  7. The confirm_submit() function utilized return statements in the if-else block to return properly from the confirm_submit function once the appropriate message was displayed to the user.
  8. (a) true

    (b) false

  9. The onMouseover and onMouseout event handlers can be used in the following tags:
  10. (a) Anchor

    (b) INPUT

    (c) both (a) and (b)

    (d) neither (a) nor (b)

    Perl

  11. $_ = "purdue university";
  12. $twk = tr/udin/1234/;

    What is $twk after the tr statement?

    (a) "p1r21e 143vers3ty"

    (b) "p1r2e 1vers3ty"

    (c) "purdue university"

    (d) 7

  13. What will the statement tr/0-9//d; do to the character string in $_?
  14. (a) change all occurences of 0-9 to d

    (b) delete all digits 0-9

    (c) change all occurences of 0-9 to a blank character

    (d) count the number of digits 0-9, but leave the string unchanged

  15. If you see the line
    &fRent;
  16. What should be in the Perl program somewhere?

    (a) sub Rent {...}

    (b) sub &fRent {...}

    (c) sub fRent {...}

    (d) sub &Rent {...}

  17. What keyword declares a function in Perl?
  18. (a) func

    (b) sub

    (c) subrtn

    (d) &

  19. So that your cgi script can be interpreted properly on mentor.cc.purdue.edu, all calls to a subroutine must contain the ampersand before the subroutine name (&someFunction) when the subroutine is called before the subroutine is defined.
  20. (a) true

    (b) false

     

  21. $pres = "Abraham Lincoln";
  22. $sub = "aha";

    $idx = index($pres, $sub);

    What is $idx?

    (a) -1

    (b) 3

    (c) 4

    (d) 5

     

  23. $old = "antibacterial";
  24. $new = substr($old, 3, 9);

    What is $sNew?

    (a) "ibacter"

    (b) "tibacte"

    (c) "ibacte"

    (d) "bacte"

  25. Which of the following is equivalent to
    if ($iReb <= 100)?
  26. (a) if ($iReb >= 100)

    (b) if ($iReb < 100)

    (c) unless ($iReb > 100)

    (d) unless ($iReb <= 100)

  27. @lst = ('p', 'm', 'j', 'd');
  28. $smthg = @lst;

    What is now in $smthg?

    (a) ('p', 'm', 'j', 'd')

    (b) 'd'

    (c) 3

    (d) 4

  29. @lst from question 14. above is identical to ("p", "m", "j", "d");
  30. (a) true

    (b) false

  31. Why do we say that Perl is a "scripting" language?
  32. (a) Perl programs cannot have any functions.

    (b) Perl programs are used for cgi applications.

    (c) Perl is NOT an object-oriented programming language.

    (d) It is interpreted at run time.

  33. Suppose that you are working in a company where the Perl interpreter is named "perlin" and is in the directory /usr/perl/bin. What should be the first line of all Perl programs?
  34. (a) #! /usr/perl/bin/perlin

    (b) #! /usr/perl/bin

    (c) #! /perlin

    (d) #! /usr/local/bin/perl

  35. You have created a Perl script to be used with a Website. Which command should be used to set its file permissions?
  36. (a) chmod a+rx search.cgi

    (b) chmod a+rw search.cgi

    (c) chmod a+r search.cgi

    (d) chmod u+rx search.cgi

  37. By default, which character(s) are removed by Perl's chomp function?
  38. (a) blank(s)

    (b) newline(s) \n

    (c) .

    (d) digit(s) (0-9)

  39. Which of the following correctly tests if one integer is equal to another?
  40. (a) ($frst eq $secnd)

    (b) ($frst = $secnd)

    (c) ($frst == $secnd)

    (d) ($frst.$secnd)

    (e) none of the above

  41. (4) Which of the following would reference one character string from an array of character strings?
  42. (a) $anArray

    (b) @anArray

    (c) $anArray[$iWhich]

    (d) none of the above

  43. Which of the following files is most likely a Perl script that is NOT a cgi application?
  44. (a) findauthor.txt

    (b) findauthor.pl

    (c) findauthor.class

    (d) findauthor.perl

  45. Which best characterizes a Perl script that is to be used as a Web cgi application in your career account?
  46. (a) Must be in a cgi-bin directory.

    (b) Must have the suffix .pl.

    (c) Must have the suffix .cgi.

    (d) Must be in a cgi-bin directory and have the suffix .cgi.

  47. Which of the following concatenates two strings in Perl?
  48. (a) $sBase+$sAssignment

    (b) $sBase,$sAssignment

    (c) $sBase.$sAssignment

    (d) $sBase cat $sAssignment

  49. $sReason = "My dog ate it";
  50. chop($sReason);

    What will now be in $sReason?

    (a) "My dog ate it"

    (b) "My dog ate i"

    (c) "My dog ate"

    (d) "dog ate it"

  51. Which of the following correctly sets $iFlag to 0 if there are less than 100 students, to 1 if there are 100 or more but less than 500, and does not change $iFlag otherwise?
  52. (a) if ($iNum < 100) {$iFlag=0;} elsif ($iNum < 500) {$iFlag=1;}

    (b) if ($iNum < 100) {$iFlag=0;} else {$iFlag=1;}

    (c) if ($iNum < 100) {$iFlag=0;} elsif ($iNum >= 100) {$iFlag=1;}

    (d) if ($iNum < 100) {$iFlag=0;} elsif ($iNum < 500) {$iFlag=1;} else {$iFlag=2;}

  53. $sButter = "Land-o-Lakes";
  54. print "My favorite butter is $sButter. Do you like it? \n";

    What will be printed?

    (a) My favorite butter is $sButter. Do you like it?

    (b) My favorite butter is Land-o-Lakes. Do you like it?

    (c) My favorite butter is $sButter. Do you like it? \n

    (d) none of the above

  55. $sButter = "Land-o-Lakes";
  56. print 'My favorite butter is $sButter. Do you like it? \n';

    What will be printed?

    (a) My favorite butter is $sButter. Do you like it?

    (b) My favorite butter is Land-o-Lakes. Do you like it?

    (c) My favorite butter is $sButter. Do you like it? \n

    (d) none of the above

  57. $sButter = "Land-o-Lakes";
  58. print "My favorite butter is \$sButter. Do you like it? \\n";

    What will be printed?

    (a) My favorite butter is $sButter. Do you like it?

    (b) My favorite butter is Land-o-Lakes. Do you like it?

    (c) My favorite butter is $sButter. Do you like it? \n

    (d) none of the above

  59. @A = (200, 255, 146, 307, 199, 312);
  60. Which of the following prints the single number 146?

    (a) print "$A[2] \n";

    (b) print "$A \n";

    (c) print "@A \n";

    (d) none of the above

  61. @A = (200, 255, 146, 307, 199, 312);
  62. $B = @A;

    What is $B?

    (a) 5

    (b) 6

    (c) (200, 255, 146, 307, 199, 312)

    (d) 200

  63. @myArray = (200, 255, 146, 307, 199, 312);
  64. Which of the following loops uses each of the @myArray values in consecutive order?

    (a) foreach @myArray ($wide) {...}

    (b) foreach $wide (@myArray) {...}

    (c) useach $wide (@myArray) {...}

    (d) for $wide (@myArray) {...}

  65. How can a value be returned from a Perl function?
  66. (a) Put the value on the last line of the function followed by a semi-colon

    (b) return (value);

    (c) Put it into $_

    (d) both (a) and (b)

  67. $sEmailAddress = "elton\@biology.purdue.edu";
  68. Which of the following will evaluate to true?

    (a) if ($sEmailAddress =~ m/BIO/i)

    (b) if ($sEmailAddress =~ m/biological/)

    (c) if ($sEmailAddress == m/BIO/)

    (d) if ($sEmailAddress =~ m/BIO/)

     

  69. Which of the following opens a file for writing so that any previous information in the file is over-written?
  1. open (SRCHFOR, "srchterms.txt");
  2. open (SRCHFOR, ">srchterms.txt");
  3. open (SRCHFOR, "<srchterms.txt");
  4. open (SRCHFOR, ">>srchterms.txt");

 

36. Which of the following writes information into the file in the previous question?

(a) print ">srchterms.txt $sSrchtext $iEndtag \n";

(b) print "$sSrchtext $iEndtag \n";

(c) print srchterms.txt "$sSrchtext $iEndtag \n";

(d) print SRCHFOR "$sSrchtext $iEndtag \n";

37. Unix files are complicated in that they do not have to reside on disk. They can be any stream of ordered bytes that a program can access.

(a) true

(b) false

 

38. Perl mainly deals with binary files.

(a) true

(b) false

39. In Perl programs, it is convention to use all lower case letters with file handles.

(a) true

(b) false

40. The Perl open() function does two things: 1) opens a file, and 2) returns a boolean

(a) true

(b) false

41. The die function does two things: 1) prints a message, and 2) returns execution to the main program

(a) true

(b) false

42. File input operator: <FiLeNaMe>
(a) returns a line from named text file, including the newline character
(b) returns a line from named text file, excluding the newline character

(c) only opens the file for reading and writing

(d) creates a file

43. Which of the following is true?

(a) $_ is a default variable that gets assigned if you do not specify a scalar

(b) @_ is a default variable that gets assigned if you do not specify a scalar

(c) the m// operator searches the default variable

(d) both (b) and (c)

(e) both (a) and (c)

44. Consider the following expression: @myFile = <A>;

(a) this reads a whole file into an array

(b) this is an illegal assignment

(c) this writes a whole array to a file

(d) is not an assignment at all, but a comparison expression

45. Using the select function is encouraged, since it allows you to have several files open at once, thereby cutting down on the number of lines of code

(a) true

(b) false

 

46. $_ = "Babs the cat decided to STAY at home";

$iMatched = s/..a.../fred/gi;

(a) $_ is now "Babs thefredecided to fredt home" and $iMatched is now 2

(b) $_ is now "fredthefredecided to fredt home" and $iMatched is now 3

(c) $iMatched is now "fredthefredecided to fredt home"

(d) none of the above

47. How are parameters passed to Perl functions?

(a) as scalars in the default variable: $_

(b) in the parameter array @_

(c) in the parameter array @$_

(d) the same as in any other language we've seen so far

48. What is true about variables in Perl?

(a) all Perl variables are global, unless you declare them local by using the my function

(b) all Perl variables are local, unless you use the global function

(c) Perl variables are considered local if defined in a function, just like in any other language

we've see so far, and global if declared outside the function

(d) Perl cannot have local variables

49. You can print an entire list (array) with this statement: print @someList;

(a) true

(b) false

50. Which environment variable can tell you how your data is being sent to the server?

(a) HTTP_REFERER

(b) HTTP_USER_AGENT

(c) REQUEST_METHOD

(d) SEND_REQUEST

(e) none of the above