Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Snarky comments on the ddj perl quiz

by ysth (Canon)
on Aug 23, 2007 at 00:26 UTC ( [id://634548]=perlmeditation: print w/replies, xml ) Need Help??

You'll find the quiz at http://www.ddj.com/linux-open-source/201801549 (assuming you use a browser that withstands all the "neat stuff" that site does).

Quizzes like this always seem to irritate me. It sometimes seems as if they go out of their way to be ambiguous and/or imprecise. So I decided to vent, with your cooperation. Note that if I didn't find something to complain about, I made something up. If you are the author of this quiz, please try not to take it too personally.

My answers and comments to questions 1-25:

  1. 2. scalar

    But scalar isn't a data type, or if it is, perl only has two data types, list and scalar. And there are fewer ways you can use a list than a scalar, so the correct answer is "list".

  2. 1. pop

    "Bottom" of an array is a horribly ambiguous term. But of the two, I would guess most would say "shift".

  3. 2. sort

    "items"? I thought we were calling them scalars. "character order"? Do you mean like sort { chr($_) cmp chr($_) } ? I'm used to seeing the term "lexicographic order". Trivia Q: when does $^O eq "linux" produce a tainted result?

  4. 1. printf

    My personal preference. But I think both printf and format are "often used".

  5. 3. i

    Actually, I'm told that on sufficiently old perls, /IBM/i is slower than /[Ii][Bb][Mm]/. But I won't share my thoughts about people who would do the latter.

  6. 2. split

    Often, a regex match is handier.

  7. -w

    Or -W, or -Mwarnings, or ...

  8. 2. unless

    Though it's used way too often in cases where "if" would be clearer. I've seen unless's that would be not much worse as

    EXPR and last while not(condition)
    .

  9. 2. open KAREN, ">sw";

    Why are we using a non-lexical filehandle KAREN? Blech.

  10. 4. -k

    I only knew this by process of elimation, knowing what -s, -l, and -S were. Guess I'm not enough of a unix geek.

  11. 2. chdir

    Change what about the working directory? I'd use chown $uid, $gid, "." or chmod $perm, ".", myself.

  12. 1. shift

    "access"? How about "remove and return"? I would have guessed "[0]". (Except when $[ is changed. But don't do that.)

  13. 2. last

    Except that last is not an operator. And doesn't work on do loops. And isn't immediate, since the usual end-of-scope processing applies.

  14. 4. die

    Um, no. POSIX::_exit(). What's a "standard error", and why "even if"? "make sure" isn't met if there's an eval, if it's in a thread, if it's in embedded perl, if a destructor resets $@, etc.

  15. 4. m//

    I guess. I'm not sure in what sense it's only "like" sime searching/ pattern matching.

  16. 2. sub

    Well, "declare" a subroutine. The definition part is optional, and can be done without sub.

  17. 2. closedir EVAN;

    Using "use Fatal 'closedir';", are you?

  18. 3. $price +=9;

    Yucky inconsistent whitespace, there. There's an important difference, at least under warnings.

  19. 3. unlink

    Perhaps you're thinking of the POSIX unlink command? rm does a whole lot more (in addition to having automatic generation of an error message, a feature shared by unlink(1)). Oh, perhaps you are doing

    use Fatal 'unlink';
    , too.

  20. 2. stat

    Function, huh. I thought we were referring to built-ins as operators. Counting back, I see the score so far, is 7 to 2 in "operator"'s favor.

  21. <> or <STDIN>

    What is "value" here? A line? A byte? And what is "keyboard"? If there were a common need to read from one, I'd assume there'd be a /dev/keyboard. But there isn't. Probably the answer to the intended question is something like

    do { open(my $tty, "/dev/tty") && readline($tty) or die "nope: $!" + }
  22. 1. -s

    Except that requires execute access to the directory the file is in and any other directories mentioned in the path given.

  23. 2. rename

    Or, more generically, File::Copy::move.

  24. 1. undef

    Counterexamples:

    print @x; # where's the "uninitialized value in print" warning? sub TIESCALAR { bless{} } sub FETCH { 42 } tie $x, "main"; print $ +x

  25. 4. system

    For those counting, the built-in score is now: operator: 7, function: 5. (Yes, I didn't count "last", which the doc (mostly) calls a "command", or m, which I don't count as a built-in.)

Half-time! All my guesses as to the intended correct answer were right so far!

Meta-comments: The bit where answers are given as letters A-D but the multiple-choice questions are given using a plain ol tag (rendered for me as 1-4) is a bit offputting. Especially since the first question has the answer "scalar, C", when scalar was answer 2. All the other answers so far do correspond as chr(64+$ans) (except under EBCDIC).

The bit where the answer "<STDIN>" disappears into the mists of HTML interpretation was cute. Entities, guys, entities.

Replies are listed 'Best First'.
Re: Snarky comments on the ddj perl quiz
by liverpole (Monsignor) on Aug 23, 2007 at 01:10 UTC
    I'm with you!

    One has to marvel at the lack of proofreading behind:

    What is the simplest type of data that Perl can work with? 1. element 2. scalar 3. vector 4. component and then (in the answer section): The simplest type of data that Perl can work with is called a scalar. +Answer: C.

    There wasn't any answer "C" ...?!  Oh, we're supposed to do a mental translation from numbers to letters!

    There's also a typo, in question #9:

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

    I didn't like any of them ... the only one that works (#2) overwrites the file "sw", whereas the question implies that the file is being opened for read access.

    As does the answer (which doesn't match):

    The command open KAREN, "sw"; should be used to open a filehandle name +d KAREN to an existing file named sw. Answer: B.

    I've only read the first 16 of them, but it's not a test I'd be likely to use in an interview session.

    Oh, and one final nit:  I always think of arrays as going horizontally from left to right, but if they are vertical, then wouldn't:

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

    Have it upside-down?  If anything, push and pop should work with the top of the array, I'd think...


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Oh, we're supposed to do a mental translation from numbers to letters!
      I think the questions and the answers are both ol lists, and somebody's browser rendered that as 1. A. B. C. D. 2. A. B. C. D. ...
      the only one that works (#2) overwrites the file "sw"
      I noticed the inconsistency between the question and answer, but somehow read the question as "<sw". That's pretty funny.
      Well for me, 'open a filehandle TO an existing file' (strongly) implies opening the file for output. 'open FROM an existing file' implies opening for input.
Re: Snarky comments on the ddj perl quiz
by samizdat (Vicar) on Aug 23, 2007 at 12:35 UTC
    I'm reminded of the New York State education department that approved a "Forth Grade" Reader for publication, then tried to blame their approval on "consultants".

    Of course, with Presidents scraped from the bottom of a barrel...

    <8^O

    Don Wilde
    "There's more than one level to any answer."
      then tried to blame their approval on "consultants"

      Bad move ... they would have been better advised to publish a photograph of the Grade twins (Forth and Fith :-)

      Cheers,
      Rob
Re: Snarky comments on the ddj perl quiz (nein)
by tye (Sage) on Aug 23, 2007 at 01:13 UTC

    You didn't notice that they provided no correct answer to question 9?

    - tye        

      Nonsense!
      open KAREN, ">sw"; is a correct, if unfortunate, answer to the question "Which of the following commands should be used to open a filehandle named KAREN to an existing file named sw?"
      Assuming you have to choose one, and don't prefer a syntax error to success.

        Update I misread the question for the third time, and thought it was trying to open a filehandle to an existing fileHANDLE, not an existing file.

        The correct way to open a filehandle to an existing filehandle is to duplicate it:

        open KAREN, ">&sw"

        Clint

Re: Snarky comments on the ddj perl quiz
by ruoso (Curate) on Aug 23, 2007 at 11:00 UTC
    2. scalar But scalar isn't a data type, or if it is, perl only has two data types, list and scalar. And there are fewer ways you can use a list than a scalar, so the correct answer is "list".

    You know, I usually see Scalar as a data type. It's even mapped to SV* when you go to C. And no, when you consider scalar as a data type, you still have Array, Hash, Glob and Code. And yes, all of them are SV* also (the SV struct is binary compatible with all of them). Or did I miss something?

    daniel
      I say "scalar isn't a data type" because all data is either a scalar or a list of scalars, so a scalar isn't really a type of data, just like dog isn't a type of poodle.

      Internally, yes, there are data types representing hashes, arrays, opcodes, etc., but at the Perl language level, arrays, hashes, globs, and scalars are variable types, not data types. And the only thing you can do with those variables is get data from them or put it into them, and that data is always a scalar or list of scalars.

        What do you mean a dog isn't a type of poodle? How else are you going to discriminate between Tory Bliar and all the other poodles?
Re: Snarky comments on the ddj perl quiz
by CountZero (Bishop) on Aug 23, 2007 at 20:56 UTC
    To call a subroutine named "mom", you precede the name with an ampersand: &mom. Answer: A.
    My mother will only "answer" if I precede her name with please.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Is your mom programmed in INTERCAL?
        I don't know in what she is programmed, but she programs in imperative languages!

        Jusk kiddin' mom! Yes I will clean-up my room now. May I then please go to YAPC::EU? Please mom, please?

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Snarky comments on the ddj perl quiz
by rvosa (Curate) on Aug 23, 2007 at 14:32 UTC
    To add a newline to each entry (that does not already have it) as that entry is printed print "@array\n";. Answer: C.
    Erm, no:
    $ perl -e '@array=qw(a b c d);print "@array\n"' a b c d $
      That was probably the best imho: no bad wording, just a lazy attitude. But I confess that I was scared for a split second that one of them would work!

      Flavio
      perl -ple'$_=reverse' <<<ti.xittelop@oivalf

      Don't fool yourself.
Re: Snarky comments on the ddj perl quiz
by ysth (Canon) on Aug 23, 2007 at 00:35 UTC
    Just in case they modify it, here're the questions in the quiz as I read it:
    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 warn +ing 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 se +t on it? 1. -s 2. -l 3. -S 4. -k 11. Within Perl, which operator is used to change the working director +y? 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 lo +op? 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/p +attern 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 followin +g 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 a +s $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 2 You want your script to read in a value from the keyboard for a vari +able named $duo. What do you set $duo equal to in the program to have + it read a keyboard value? __________ (Fill in the blank.) 21. Which file test can be done to see if a file exists and has a nonz +ero size? 1. -s 2. -l 3. -S 4. -k 22. Which Perl function is used to change the name of a file? 1. nameas 2. rename 3. name 4. now_known_as 23. What value do variables have before they are first assigned? 1. undef 2. null 3. 0 4. nil 24. Which Perl function can be used to launch a child process to run a + program? 1. split 2. spin 3. fork 4. system 25. Which Perl function can be used to identify the first found occurr +ence of a substring? 1. find 2. index 3. locate 4. allocate 26. Which control structure can be used to loop as long as a condition +al expression returns true? 1. until 2. unless 3. while 4. without 27. Which operator can be used to create private variables within a su +broutine? 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 b +ut 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 su +broutine? 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. Wh +at 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, whic +h 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, b +ut 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 doe +s 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 numeri +c > operator? 1. ne 2. eq 3. ge 4. gt 43. Which control structure can be used to loop as long as a condition +al 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 char +acter? 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 lis +t? 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 arra +y? 1. pop 2. push 3. pull 4. plant 50. Which of the following mathematical operations has the highest pre +cedence in Perl? 1. ** 2. ++ 3. + 4. -
Re: Snarky comments on the ddj perl quiz
by rvosa (Curate) on Aug 23, 2007 at 14:27 UTC
    Both $price =+9; and $price = $price + 9; accomplishes the same thing of adding 9 to the current value of the $price variable. Answer: C.
    Erm, shouldn't that be "$price += 9"?
Re: Snarky comments on the ddj perl quiz
by rvosa (Curate) on Aug 23, 2007 at 14:28 UTC
    Answer: Set $duo equal to in the program to have it read a keyboard value (standard input).
    I assume someone forgot to escape the "<" character?
Re: Snarky comments on the ddj perl quiz
by technojosh (Priest) on Aug 23, 2007 at 21:16 UTC
    2 favorite parts:

    • Converting numbers to letters to see if i got anything right...sure its easy to figure out 1=A, I just don't want to
    • The sweet <STDIN> disappearing act.
Re: Snarky comments on the ddj perl quiz
by Argel (Prior) on Aug 27, 2007 at 23:14 UTC
    FYI, there's another thread already discussing some of the questions: DDJ Test for Perl.
      I see my post above received a downvote. If you are the person who did it I would be curious to know why? This thread and the one I linked to seem to compliment each other almost perfectly so I am really baffled at the downvote.
      -- Confused
        It was an accident. I swear. You know, sometimes you aim for the ++, but some mysterious force draws your cursor over to the -- (not enough coffee maybe? too much coffee? Or Lagavulin?). Sometimes you even notice your mistaken vote and intend to hit the +=0 before you hit submit, but forget. In the nearly seven years I've been here, it's happened to me at least twice. But not on this thread. The point being, as tye said, a single downvote doesn't mean anything. Or maybe he didn't say that...

        I would guess that it is more to do with your wording. When i first read it I heard "dude there is already a post on this subject so why did you post another one?" Granted thats not what you actually said or probably what you meant. I think it stems from your use of the FYI, that can have a negative connotation. So 'FYI' and 'there's another thread already discussing' give your post a negative feel when read as if you are pointing out to the OP that they screwed up and there was already a post about this.

        English + Human Beings + Intonation Free Text Based Communication = Miscommunication.


        ___________
        Eric Hodges

        You might find it helpful to consult the site documentation (see the "Need Help??" link at the top of each page) which notes that a single downvote on a node likely has at least as much to do with the person casting the vote as with the node voted upon.

        At the time you wrote this reply, you only had a single down-vote on the prior node. Escalating a single downvote into a yet another discussion about voting reasons often changes the types of votes you get, so the single down-vote might be the only one actually due to (one person's reaction to) just the prior node.

        If you are really curious about why someone might downvote, besides the site documentation, you can read through many, many previous discussions on voting. It sounds like you, like most, don't downvote w/o a fairly serious reason. But that is just how most monks vote, not all of them. Some down-vote all of the time and almost never up-vote, so, obviously, they down-vote for less than serious reasons (or else they think the site is chock full of serious problems, I guess). Some monks have pet peaves. And, of course, everybody can have a bad day and take it out on somebody else.

        Are you really so conceited that you can't even imagine some reason why someone might dislike your prior node? :)

        Finally, some advise: Meta-discussion points such as your "link between the threads" are usually better received if they are accompanied by some non-meta-discussion points. That is, try to contribute to the main topic of the thread if you want to make some point about the discussion itself (not the topic of the discussion).

        - tye        

        Asking why in a separate node will get you more --
        A reply falls below the community's threshold of quality. You may see it by logging in.
      A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://634548]
Approved by kyle
Front-paged by gmax
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-20 14:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found