Perl Wizard Quiz 
Author Message
 Perl Wizard Quiz

Published in a recent Perl Journal, albeit with fewer typos.
The questions are below the program.  Sharred up to protect
from wicked nntp dot sensitivity in the format statements.

--tom

#!/bin/sh
# This is a shell archive (produced by GNU sharutils 4.2).
# To extract the files from this archive, save it to some FILE, remove
# everything before the `!/bin/sh' line above, then type `sh FILE'.
#

# Source directory was `/home/ftp/pub/perl/misc'.
#
# Existing files will *not* be overwritten unless `-c' is specified.
# This format requires very little intelligence at unshar time.
# "if test", "echo", "mkdir", and "sed" may be needed.
#
# This shar contains:
# length mode       name
# ------ ---------- ------------------------------------------
#  31827 -rwxr-xr-x ranquiz
#
echo=echo
if mkdir _sh29883; then
  $echo 'x -' 'creating lock directory'
else
  $echo 'failed to create lock directory'
  exit 1
fi
# ============= ranquiz ==============
if test -f 'ranquiz' && test "$first_param" != -c; then
  $echo 'x -' SKIPPING 'ranquiz' '(file already exists)'
else
shar: Saving ranquiz (text)
  $echo 'x -' extracting 'ranquiz' '(text)'
  sed 's/^X//' << 'SHAR_EOF' > 'ranquiz' &&
X#!/usr/bin/perl -00
X
Xuse strict;
X
Xuse FileHandle;
X$| = 1;
X
Xmy $DELAY = 2;
X
Xmy $RANDOMIZE = 1;

X
Xwhile (<DATA>) {
X    next if /===/;
X    my %record;
X    (undef, %record) = split /^([^:\s]+):\s*/m;



X    else                     { die "unknown record $_" }
X}
X
Xrand_questions();
X
Xexit;
X
Xshow_full_quiz();
X
X####################################
X
Xsub rand_questions {

X
X    for (my $qnum = 0; $qnum < $#questions; $qnum++) {
X       system('clear');
X       $q = $questions[$qnum];
X       question("Q".(1+$qnum), $q->{Question});
X       my $anum = 'a';


X           answer($anum++, $a->{Answer});
X       }
X
X       delay(5);
X          
X       print "\nANSWERS:\n\n";
X       for ( my $i = 0; $i <= $#answers; $i++ ) {
X           my $a = $answers[$i];
X           explain( ( 'a' .. 'z' )[$i], $a->{Correct} . $a->{Why} );
X           print "\n";
X           delay(2 + length($a->{Why})/50);
X       }
X       delay(5);
X    }
X}
X
Xsub delay {
X    my $count = shift;
X    sleep(1 + $DELAY * $count);
X}
X
Xsub rand_question {

X    system('clear');
X    question('Q', $q->{Question});
X    my $anum = 'a';


X       answer($anum++, $a->{Answer});
X    }
X
X    delay(3);
X      
X    print "\nANSWERS:\n\n";
X    for ( my $i = 0; $i <= $#answers; $i++ ) {
X       answer( ( 'a' .. 'z' )[$i],
X               $answers[$i]{Correct} .
X               $answers[$i]{Why} );
X       print "\n";
X       delay(1);
X    }
X    print "\n";
X    delay(5);
X}
X
Xsub show_full_quiz {
X    my $qnum = '01';

X       fwrite ($qnum++, $q->{Question});
X       my $anum = 'a';

X           fwrite(' '.$anum++.':', $a->{Answer});
X       }
X       print "\n";
X    }
X}
X
Xsub scramble {


X    srand(time() ^ ($$ + ($$ << 15)));


X}
X
X
Xsub fwrite {

X
X    sub question { STDOUT->format_name("Question"); &fwrite; }
X    sub answer { STDOUT->format_name("Answer"); &fwrite; }
X    sub explain { STDOUT->format_name("Explain"); &fwrite; }
X

X$num, $text
X  ~~  ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
X      $text
X
X.
X
Xformat Answer =

X$num, $text
X  ~~        ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
X      $text
X.
X

X$num, $text
X~~    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
X      $text
X.
X
X    write;
X
X}
X
X__DATA__
X
XQuestion:   How do you produce a reference to a list?
XType:       References
XDifficulty: 6/7 (Hard)
X

XCorrect:    No.

X

XCorrect:    No.
XWhy:        That makes a reference to a newly allocated anonymous
X            array, and populates it with a copy of the contents

X

XCorrect:    No.
XWhy:        The backslash operator is distributive across a list, and

X            Well.  In list context.  In scalar context, it's a strange
X            way to get a reference to the function &c.
X
XAnswer:     You can't.
XCorrect:    Yes.
XWhy:        A list is not an array, although is many places one may be
X            used for the other.  An array has an AV allocated, whereas a
X            list is just some values on a stack somewhere.  You cannot
X            alter the length of a list, for example, any more than
X            you could alter a number by saying something like 23++.
X            While an array contains a list, it is not a list itself.
X
X========================================================================
X
XQuestion:   What happens when you return a reference to a private variable?
XType:      References
XDifficulty: 4/7 (Medium)
X
XAnswer:            You get a core dump later when you use it.
XCorrect:    No.
XWhy:       Perl is not C or C++.  
X
XAnswer:            The underlying object is silently copied.
XCorrect:    No.
XWhy:       Even though the reference returned is for all intents
X           and purposes a copy of the original (Perl uses return
X           by reference), the underlying referent has not changed.
X
XAnswer:            The Right Thing (tm).
XCorrect:    Yes.
XWhy:       Perl keeps track of your variables, whether dynamic or
X           otherwise, and doesn't free things before you're done using
X           them.
X
XAnswer:            The compiler doesn't let you.
XCorrect:    No.
XWhy:       Perl seldom stops you from doing what you want to do,
X           and tries very hard to do what you mean to do.  This
X           is one of those cases.
X
X========================================================================
X
XQuestion:   Why aren't Perl's patterns regular expressions?
XType:      Regular expressions
XDifficulty: 3/7 (Medium)
X
XAnswer:            Because Perl patterns have backreferences.
XCorrect:    Yes.
XWhy:       A regular expression by definition must be
X           able to determine the next state in the finite
X           automaton without requiring any extra memory
X           to keep around previous state.  A pattern /([ab]+)c\1/
X           requires the state machine to remember old
X           states, and thus disqualifies such patterns
X           as being regular expressions in the classic sense
X           of the term.
X
XAnswer:            Because Perl allows both minimal matching and maximal
X           matching in the same pattern.
XCorrect:    No.
XWhy:       The mere presence of minimal and maximal repetitions
X           does not disqualify a language from being "regular".
X
XAnswer:            Because Perl uses a non-deterministic finite automaton
X           rather than a deterministic finite automaton.
XCorrect:    No.
XWhy:       Both NFAs and DFAs can be used to solve regular
X           expressions.  Given an NFA, a DFA for it can be constructed,
X           and vice versa.  For example, classical grep uses an NFA,
X           while classical egrep a DFA.  Whether a pattern matches
X           a particular string doesn't change, but where the match
X           occurs may.  In any case, they're both regular.  However,
X           an NFA can also be modified to handle backtracking, while
X           a DFA cannot.
X
XAnswer:            Because Perl patterns can have look-aheads assertions
X           and negations.
XCorrect:    No.
XWhy:       The `(?=foo)' and `(?!foo)' constructs no more violate
X           whether the language is regular than do `^' and `$',
X           which are also zero-width statements.  
X
X========================================================================
X
XQuestion:   What happens to objects lost in "unreachable" memory,
X           such as the object returned by Ob->new() in
X           `{ my $ap; $ap = [ Ob->new(), \$ap ]; }' ?
XType:      Objects
XDifficulty: 4/7 (Medium)
X
XAnswer:            Their destructors are called when that interpreter thread
X           shuts down.
XCorrect:    Yes.
XWhy:       When the interpreter exits, it first does an exhaustive
X           search looking for anything that it allocated.  This allows
X           Perl to be used in embedded and multithreaded applications
X           safely, and furthermore guarantees correctness of object
X           code.
X
XAnswer:            Their destructors are called when the memory becomes unreachable.
XCorrect:    No.
XWhy:       Under the current implementation, the reference-counted
X           garbage collection system will not notice that the object
X           in $ap's array cannot be reached, because the array reference
X           itself never has its reference count go to zero.
X
XAnswer:            Their destructors are never called.
XCorrect:    No.
XWhy:       That would be very bad, because then you could have objects
X           whose class-specific cleanup code didn't get called ever.
X
XAnswer:            Perl doesn't support destructors.
XCorrect:    No.
XWhy:       A class's DESTROY function, or that of its base classes,
X           is called for any cleanup.  It is not expected to deallocate
X           memory, however.
X
X========================================================================
X
XQuestion:   How do you give functions private variables that
X           retain their values between calls?
XType:      Subroutines, Scoping
XDifficulty: 5/7 (Medium)
X
XAnswer:            Perl doesn't
...

read more »



Sun, 30 Jul 2000 03:00:00 GMT  
 Perl Wizard Quiz

[posted and mailed]

Quote:

> Question:   What's the difference between /^Foo/s and /^Foo/?
> Type:      Regular Expressions
> Difficulty: 5/7 (Medium)

> Answer:            There is no difference because /s only affects whether dot
>            can match newline.
> Correct:    No.
> Why:       /s does more than that.

             ^^^^^^^^^^^^^^^^^^^^^^

Quote:
> Answer:            The first would allow the match to cross newline boundaries.
> Correct:    No.
> Why:       /s only makes a dot able to cross a newline, and then  

             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Quote:
>            only if the string actually has a newline in it.

These two statements appear to contradict each other.  Could you clarify them?

thanks,
Chipmunk



Mon, 31 Jul 2000 03:00:00 GMT  
 Perl Wizard Quiz

 [courtesy cc of this posting sent to cited author via email]

:> Question:   What's the difference between /^Foo/s and /^Foo/?
:>
:> Answer:    There is no difference because /s only affects whether dot
:>            can match newline.
:> Correct:   No.
:> Why:       /s does more than that.
:
:> Answer:    The first would allow the match to cross newline boundaries.
:> Correct:   No.
:> Why:       /s only makes a dot able to cross a newline, and then  
:>            only if the string actually has a newline in it.
:
:These two statements appear to contradict each other.  Could you clarify them?

Well, I see what you mean.

The point I was trying to convey was that with /s, you don't have to
worry about any settings of the old $* variable, which deprecated though
it may be, still shows up in older scripts that no one can be bothered to
upgrade (me included :-).  That what the "does more than that" alludes to.

In the second case, I was trying to show that it doesn't matter
whether you can cross newline boundaries if your input data is
coming in just one line at a time.

--tom
--

    X-Windows: You'll envy the dead.
        --Jamie Zawinski



Mon, 31 Jul 2000 03:00:00 GMT  
 Perl Wizard Quiz

[E-mailed and posted]



Quote:
> Published in a recent Perl Journal, albeit with fewer typos.
> The questions are below the program.  Sharred up to protect
> from wicked nntp dot sensitivity in the format statements.

[...]

Quote:
> # ============= ranquiz ==============
> if test -f 'ranquiz' && test "$first_param" != -c; then
>   $echo 'x -' SKIPPING 'ranquiz' '(file already exists)'
> else
> shar: Saving ranquiz (text)

  ^^^^^^^^^^^^^^^^^^^^^^^^^^^

Quote:
>   $echo 'x -' extracting 'ranquiz' '(text)'
>   sed 's/^X//' << 'SHAR_EOF' > 'ranquiz' &&

I may be missing something extremely obvious.  At least, that's how I
was about to start this post.  But now I know what the problem is:
shar's message (probably on stderr) has been inadvertently included
in the /bin/sh script.
--

        Director of Operations, NetMonger Communications, Inc.



Mon, 31 Jul 2000 03:00:00 GMT  
 Perl Wizard Quiz


Quote:
># ============= ranquiz ==============
>if test -f 'ranquiz' && test "$first_param" != -c; then
>  $echo 'x -' SKIPPING 'ranquiz' '(file already exists)'
>else
>shar: Saving ranquiz (text)

 ^
 |---  Maybe I'm doing something wrong, but I think this line needs
       to be commented out.

Quote:
>  $echo 'x -' extracting 'ranquiz' '(text)'
>  sed 's/^X//' << 'SHAR_EOF' > 'ranquiz' &&
>X#!/usr/bin/perl -00

--
Shimpei Yamashita               <http://www.patnet.caltech.edu/%7Eshimpei/>
The address I have listed in the headers is a working address. It just
looks like it's an invalid address in order to fool spammers collecting
addresses from newsgroups.


Mon, 31 Jul 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. RFQ: Tk::Wizard - a wizard style widget for Perl/Tk

2. RFQ: Tk::Wizard - a wizard style widget for Perl/Tk

3. Perl Quiz of the Week

4. today's perl quiz :-)

5. Perl CGI multi screen Quiz program

6. quiz for perl professionals

7. Need questions for Perl quiz

8. On-line Perl quiz

9. A question to Perl wizards

10. A call to PERL wizards

11. One for the Perl Wizards...

 

 
Powered by phpBB® Forum Software