Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Open Source

Test Your Knowledge of Perl


Emmett Dulaney is the author of the several books on Linux/Unix and certification. Emmett's blog can be found at http://edulaney.blogspot.com and he can be reached at [email protected].


There is an interesting thing about the open-source acronym LAMP "- no one ever questions what the first three letters represent (Linux, Apache, and MySQL). The fourth letter, however, can mean Perl, Python, PHP, or "- in time "- another programming language that may come along.

Having already done PHP, my goal here is to test what you know about Perl. Following are 50 questions that are intended to test your knowledge of the programming language at a beginning to intermediate level. Good luck (answers are at the end of the article)!

  1. What is the simplest type of data that Perl can work with?
    1. element
    2. scalar
    3. vector
    4. component

  2. Which operator can be used to take the bottom item from an array?
    1. pop
    2. push
    3. pull
    4. plant

  3. Which operator is used to arrange items in character order?
    1. ascend
    2. sort
    3. arrange
    4. descend

  4. Rather than using print, what is often used in Perl when formatting is important?
    1. printf
    2. format
    3. align
    4. show

  5. Which modifier can be used when matching in Perl to ignore case?
    1. s
    2. v
    3. i
    4. c

  6. Which operator can be used to break up a string into more than one part based upon a separator?
    1. chop
    2. split
    3. divide
    4. parse

  7. What option do you use when starting Perl to tell it to run in warning mode? __________ (Fill in the blank.)

  8. Which control structure can be used to execute only a condition is false?
    1. until
    2. unless
    3. while
    4. without

  9. Which of the following commands should be used to open a filehandle named KAREN to an existing file named sw?
    1. open KAREN ">sw";
    2. open KAREN, ">sw";
    3. open "sw" KAREN;
    4. open ">sw", KAREN;

  10. Which file test can be done to see if a file has the sticky bit set on it?
    1. -s
    2. -l
    3. -S
    4. -k

  11. Within Perl, which operator is used to change the working directory?
    1. cd
    2. chdir
    3. pwd
    4. wd

  12. Which operator can be used to access the first item in an array?
    1. shift
    2. start
    3. right$
    4. left$
  13. Within a loop, which operator immediately ends execution of the loop?
    1. next
    2. last
    3. redo
    4. leave

  14. Which function can be used to make sure your program exits with a nonzero status even if there a standard error?
    1. hash
    2. noexit
    3. nozero
    4. die

  15. Which of the following operators work most like simple searching/pattern matching?
    1. /f
    2. /g
    3. s///
    4. m//

  16. Which keyword is used in Perl to define a subroutine?
    1. branch
    2. sub
    3. split
    4. make

  17. You want to close the directory handle EVAN. Which of the following should you use to accomplish this?
    1. close EVAN;
    2. closedir EVAN;
    3. close_directory EVAN;
    4. none of the above

  18. Which of the following lines of code accomplishes the same thing as $price = $price + 9;?
    1. $price +9;
    2. $price =+9;
    3. $price +=9;
    4. 9 + $price

  19. Which Perl operator works the same as the Linux rm command?
    1. rm
    2. del
    3. unlink
    4. bye
    5. 6
    6. 7

  20. Which function can be used to return such information about a file as its UID, GID, CTIME, etc.?
    1. file
    2. stat
    3. info
    4. mode

  21. You want your script to read in a value from the keyboard for a variable named $duo. What do you set $duo equal to in the program to have it read a keyboard value? __________ (Fill in the blank.)

  22. Which file test can be done to see if a file exists and has a nonzero size?
    1. -s
    2. -l
    3. -S
    4. -k

  23. Which Perl function is used to change the name of a file?
    1. nameas
    2. rename
    3. name
    4. now_known_as

  24. What value do variables have before they are first assigned?
    1. undef
    2. null
    3. 0
    4. nil

  25. Which Perl function can be used to launch a child process to run a program?
    1. split
    2. spin
    3. fork
    4. system

  26. Which Perl function can be used to identify the first found occurrence of a substring?
    1. find
    2. index
    3. locate
    4. allocate

  27. Which control structure can be used to loop as long as a conditional expression returns true?
    1. until
    2. unless
    3. while
    4. without

  28. Which operator can be used to create private variables within a subroutine?
    1. native
    2. limited
    3. my
    4. regional

  29. What Perl function is identical to printf only instead of printing a string, it just returns it and can thus be useful in storing it in a variable? __________ (Fill in the blank.)

  30. Which file test can be done to see if a file is a symbolic link?
    1. -s
    2. -l
    3. -S
    4. -k

  31. Within a loop, which operator jumps to the beginning of the loop but does not exit the loop?
    1. next
    2. last
    3. redo
    4. leave

  32. Which of the following operators is used to return a value in a subroutine?
    1. return
    2. send
    3. give
    4. show

  33. Your script has just read in a variable from the keyboard and you want to strip that variable of the newline character that came in. What operator should you use to perform this operation?
    1. tidy
    2. trim
    3. chomp
    4. slim

  34. To convert a timestamp into a format more readable by a user, which function should you use?
    1. bitwise
    2. localtime
    3. translate
    4. transform

  35. What is the default sort order in Perl?
    1. alphabetic
    2. numeric
    3. ASCII
    4. none of the above

  36. Which file test can be done to see if a file is a socket?
    1. -s
    2. -l
    3. -S
    4. -k

  37. Which function can be used to print a message to standard error, but not quit the program?
    1. warn
    2. alert
    3. tell
    4. notifiy

  38. Which command should be used to close the filehandle KAREN?
    1. close KAREN "sw";
    2. KAREN close;
    3. close KAREN;
    4. close &KAREN

  39. Which operator is the opposite of shift?
    1. replace
    2. restart
    3. unshift
    4. return

  40. Within a loop, which operator jumps to the end of the loop but does not exit the loop?
    1. next
    2. last
    3. redo
    4. leave

  41. Which of the following should be used to print values in an array that do not contain newlines and add them to the end of each entry?
    1. print @array;
    2. print @array\n;
    3. print "@array\n";
    4. print "$@array \\";

  42. Which string comparison operator would be equivalent to the numeric > operator?
    1. ne
    2. eq
    3. ge
    4. gt

  43. Which control structure can be used to loop as long as a conditional expression returns false?
    1. until
    2. unless
    3. while
    4. without

  44. Which function can be thought of as the opposite of split?
    1. link
    2. join
    3. unite
    4. bond

  45. Which of the following operators work most like substitution?
    1. /f
    2. /g
    3. s///
    4. m//

  46. Which modifier can be used when matching in Perl to match any character?
    1. s
    2. v
    3. i
    4. c

  47. Which of the following will call a subroutine named "mom"?
    1. &mom
    2. $mom
    3. mom$
    4. %mom%

  48. Which type of loop can be used to step through each value in a list?
    1. conv
    2. one_by_one
    3. separate
    4. foreach

  49. Which operator can be used to add an item to the bottom of an array?
    1. pop
    2. push
    3. pull
    4. plant

  50. Which of the following mathematical operations has the highest precedence in Perl?
    1. **
    2. ++
    3. +
    4. -

Answers

  1. The simplest type of data that Perl can work with is called a scalar. Answer: C.
  2. The pop operator can be used to take the bottom item from an array. Answer: A
  3. The sort operator is used to arrange items in character order. Answer: B.
  4. Rather than using print, the printf operator is often used in Perl when formatting is important. Answer: A.
  5. There /i modifier can be used when matching in Perl to ignore case. Answer: C.
  6. The split operator can be used to break up a string into more than one part based upon a separator. Answer: B.
  7. Answer: running Perl with the "w option tells it to print a warning about anything suspect it finds during operation "- debugging mode, if you will.
  8. The unless control structure can be used to execute only a condition is false. Answer: B.
  9. The command open KAREN, "sw"; should be used to open a filehandle named KAREN to an existing file named sw. Answer: B.
  10. The "k file test can be done to see if a file has the sticky bit set on it. Answer: D.
  11. Within Perl, the chdir operator is used to change the working directory. Answer: B.
  12. The shift operator can be used to access the first item in an array. Answer: A.
  13. Within a loop, the last operator immediately ends execution of the loop. Answer: B.
  14. The die function can be used to make sure your program exits with a nonzero status even if there a standard error. Answer: D.
  15. The m// operator works like simple searching/pattern matching. When a match is found, the match is printed, acted upon, etc.. Answer: D.
  16. The sub keyword is used in Perl to define a subroutine. Answer: B.
  17. The closedir EVAN; command is used to close the directory handle EVAN. Answer: B.
  18. Both $price =+9; and $price = $price + 9; accomplishes the same thing of adding 9 to the current value of the $price variable. Answer: C.
  19. The unlink Perl operator works the same as the Linux rm commanD. Answer: C.
  20. The stat function can be used to return such information about a file as its UID, GID, CTIME, etc.. Answer: B.
  21. Answer: Set $duo equal to in the program to have it read a keyboard value (standard input).
  22. The "s file test can be done to see if a file exists and has a nonzero size. Answer: A.
  23. The rename Perl function is used to change the name of a file. Answer: B.
  24. Before variables are assigned, they have the value of undef. Answer: A.
  25. The system Perl function can be used to launch a child process to run a program. Answer: D.
  26. The index Perl function can be used to identify the first found occurrence of a substring. Answer: B.
  27. The while control structure can be used to loop as long as a conditional expression returns true. Answer: C.
  28. The my operator can be used to create private variables within a subroutine. Answer: C.
  29. Answer: The sprintf function is identical to printf only instead of printing a string, it just returns it and can thus be useful in storing it in a variable.
  30. The -l file test can be done to see if a file is a symbolic link. Answer: B.
  31. Within a loop, the redo operator jumps to the beginning of the loop but does not exit the loop. Answer: C.
  32. The return operator is used to "return" a value in a subroutine. Answer: A.
  33. The chomp operator is used to strip a variable of the newline character. Answer: C.
  34. To convert a timestamp into a format more readable by a user, use the localtime function. Answer: B.
  35. The default sort order in Perl is ASCII order. Answer: C.
  36. The -S file test can be done to see if a file is a socket. Answer: C.
  37. The warn function can be used to print a message to standard error, but not quit the program. Answer: A.
  38. The command close KAREN; should be used to close the filehandle KAREN. Answer: C.
  39. The unshift operator is the opposite of shift. Answer: C.
  40. Within a loop, the next operator jumps to the end of the loop but does not exit the loop. Answer: A.
  41. To add a newline to each entry (that does not already have it) as that entry is printed print "@array\n";. Answer: C.
  42. Both the > and gt operators do the same function. The symbol > is used with numeric operations for "greater than", while gt is used with strings. Answer: D.
  43. The until control structure can be used to loop as long as a conditional expression returns false. Answer: A.
  44. Of the choices presented, the join function can be thought of as the opposite of split. Answer: B.
  45. The s/// operator in Perl is used to perform substitution. Answer: C.
  46. The /s modifier can be used when matching in Perl to match any character. Answer: A.
  47. To call a subroutine named "mom", you precede the name with an ampersand: &mom. Answer: A.
  48. The foreach loop can be used to step through each value in a list. Answer: D.
  49. The push operator can be used to add to the bottom of an array. Answer: B.
  50. The autoincrement (++) operation has the highest precedence of the choices offered. Next would be **, followed by + and -. Answer: B.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.