42
Scenario You have read a text file containing whitespace separated columns of numbers. Each line may contain leading and trailing whitespace. You must iterate through the file and, for each line, capture the numbers in an array. The elements of the array must not contain leading or trailing space and there may be no empty elements. Based on the scenario above, which split command might you use in your loop to meet the specification? Choice 1 my @fields = split / /, $string; Choice 2 my @fields = split /\s/, $string; Choice 3 my @fields = split /\s+/, $string; Choice 4 my @fields = split ' ', $string; Choice 5 my @fields = split /\s*/, $string; Perl 5.12, Question 1 of 20 -----------===== Sample Code $f = 4; for $f (1, 2, 3) { print $f; } print $f; Based on the sample code above, what is printed? Choice 1 123 Choice 2 4444

Perl Brain Bench Q&A

Embed Size (px)

Citation preview

Scenario You have read a text file containing whitespace separated columns of numbers. Each line may contain leading and trailing whitespace. You must iterate through the file and, for each line, capture the numbers in an array. The elements of the array must not contain leading or trailing space and there may be no empty elements. Based on the scenario above, which split command might you use in your loop to meet the specification? Choice 1 my @fields = split / /, $string; Choice 2 my @fields = split /\s/, $string; Choice 3 my @fields = split /\s+/, $string; Choice 4 my @fields = split ' ', $string; Choice 5 my @fields = split /\s*/, $string; Perl 5.12, Question 1 of 20 -----------===== Sample Code $f = 4; for $f (1, 2, 3) { print $f; } print $f; Based on the sample code above, what is printed? Choice 1 123 Choice 2 4444 Choice 3 1233 Choice 4 1234 Choice 5 Nothing; the script exits with an error. Perl 5.12, Question 2 of 20

Sample Code $x = (2, 4, 6); Based on the sample code above, what will $x equal after the assignment? Choice 1 2 Choice 2 3 Choice 3 6 Choice 4 undef Choice 5 An empty string Perl 5.12, Question 3 of 20

What statement would open a file for both reading and writing? Choice 1 open( my $fh, '>>', $filename ) or die $!; Choice 2 open( my $fh, '', $filename ) or die $!; Choice 3 open( my $fh, 'rw', $filename ) or die $!; Choice 4 open( my $fh, '+',undef) || die "Can't open"; Choice 5 open($fh,'>',\$variable) || die "Can't open" Perl 5.12, Question 20 of 20

What is one important difference between the "use" statement and the "require" statement?

Choice 1 Use statements can only be used to include modules; require statements cannot be used to include modules. Choice 2 Require statements can only be used to include modules; use statements cannot be used to include modules. Choice 3 Use statements are evaluated at run time; require statements are evaluated at compile time. Choice 4 Use statements are evaluated at compile time; require statements are evaluated at run time. Choice 5 Require statements can have an import list; use statements always import all the symbols exported by the module. Perl 5.12, Question 1 of 20

What do you use to create a class? Choice 1 A package Choice 2 A hash Choice 3 A subroutine Choice 4 An anonymous hash Choice 5 A typeglob Perl 5.12, Question 2 of 20 Which statement immediately exits a loop and continues execution with the first statement following the loop? Choice 1 exit Choice 2 next Choice 3 last Choice 4 goto Choice 5 die Perl 5.12, Question 3 of 20

Which items help in writing secure code? Choice 1 Enabling bounds checking, buffer overflow alerts, and lint checking Choice 2 Enabling profiling Choice 3 Enabling debugging Choice 4 Enabling the security module with "use Security;" Choice 5 Enabling taint checking, strict, and warnings Perl 5.12, Question 4 of 20

Sample Code

Based on the sample code above, what is the standard output? Choice 1 A list of all elements in the environment in sorted order Choice 2 A list of all elements in the environment in unsorted order Choice 3 A list of all elements in the environment in the order in which %ENV's keys were last accessed Choice 4 A stream of continuous output until the output is manually interrupted Choice 5 A syntax error is produced Perl 5.12, Question 5 of 20

What is returned when a call to system() executes successfully? Choice 1 -1 Choice 2 0 Choice 3 1 Choice 4

255 Choice 5 The pid of the subprocess. Perl 5.12, Question 6 of 20

Which regular expression deletes all tags specified as text, enclosed by "", from a document stored in a string, but deletes nothing else. Choice 1 $string =~ s///sg; Choice 2 $string =~ s///sg; Choice 3 $string =~ s///sg; Choice 4 $string =~ s///sg; Choice 5 $string =~ s///sg; Perl 5.12, Question 7 of 20

Sample Code

Based on the sample code above, what is the output? Choice 1 1 Choice 2 135 Choice 3 357 Choice 4 11357 Choice 5 13571 Perl 5.12, Question 8 of 20

Sample Code

Based on the sample code above, what is the output of the code? Choice 1 20 10 20 Choice 2 20 20 20 Choice 3 30 10 20 Choice 4 30 20 20 Choice 5 30 10 10 Perl 5.12, Question 9 of 20 Which module is typically used to evaluate unknown or unsafe code within an optionally operator-restricted namespace? Choice 1 Opcode Choice 2 Taint Choice 3 Safe Choice 4 Overload Choice 5 Sigtrap Perl 5.12, Question 10 of 20

Sample Code

What does "$data" contain after the code is executed? Choice 1 The empty string Choice 2 The first byte in "file.dat" Choice 3 The first line in "file.dat" Choice 4 The first page in "file.dat" Choice 5 The entire file in "file.dat" Perl 5.12, Question 11 of 20 What expression is equivalent to pop(@data)? Choice 1 splice(@data,-1,1) Choice 2 splice(@data,0,1) Choice 3 splice(@data,1,1) Choice 4 splice(@data,-1,0,1); Choice 5 splice(@data,0,0,1); Perl 5.12, Question 12 of 20 What expression is equivalent to shift(@data)? Choice 1 splice(@data,-1,1) Choice 2 splice(@data,0,1) Choice 3 splice(@data,1,1) Choice 4 splice(@data,-1,0,1); Choice 5 splice(@data,0,0,1); Perl 5.12, Question 13 of 20

Sample Code undef $/; Based on the code sample above, what is the effect during input or output? Choice 1 It reads the entire file. Choice 2 It specifies to Perl not to buffer file output. Choice 3 It displays a line at a time in output. Choice 4 It reads a line at a time. Choice 5 It makes later processing case insensitive. Perl 5.12, Question 14 of 20

What statement would assign to $n the number of elements in the hash %h? Choice 1 $n = scalar %h; Choice 2 $n = $#h; Choice 3 $n = count keys %h; Choice 4 $n = scalar keys %h; Choice 5 $n = count(%h); Perl 5.12, Question 15 of 20

Scenario You need to check to see what version of Perl a module you have written is running. You know that Perl versions earlier than 5.005 do not work properly with your module. Based on the scenario above, how do you check that the version of Perl is new enough? Choice 1 Using "$PERLVER>=5.005" Choice 2 Using "$|>=5.005" Choice 3 Using "$$>=5.005" Choice 4 Using "$]>=5.005" Choice 5

Using "%INC{VERSION}>=5.005" Perl 5.12, Question 16 of 20

Sample Code Based on the sample code above, what prints? Choice 1 11 12 13 Choice 2 1 2 3 Choice 3 1..1 1..2 1..3 Choice 4 1 12 123 Choice 5 1 21 321 Perl 5.12, Question 17 of 20

How do you call a subroutine with a scalar value as an argument? Choice 1 $subname[$scalar]; Choice 2 $scalar=&subname; Choice 3 $scalar=&subname{}; Choice 4 subname{$scalar}; Choice 5 subname($scalar); Perl 5.12, Question 18 of 20 Which conditional statement is equivalent to "if (!)"? Choice 1

ifn () Choice 2 fails () Choice 3 unless () Choice 4 failure () Choice 5 require () Perl 5.12, Question 19 of 20

Sample Code

Based on the sample code above, in order to produce the total number of bytes used by all the items in the current directory, what replaces "????" ? Choice 1 map {my $size = -s $_; my $total += $size;} @files; Choice 2 map {my $size = -l $_; $total += $size;} @files; Choice 3 map {my $size = fsize $_; $total += $size;} @files; Choice 4 map {$total += -s $_;} @files; Choice 5 map {$total += fsize($_);} @files; Perl 5.12, Question 20 of 20