<%@ include file="../../ga4.jsp" %> Perl Operators[Types] - Quiz Explanation

Perl Operator - Quiz Explanation

The answers you selected are indicated below, along with text that explains the correct answers.
 
1. What is the result of this expression:
3 + 4 * 7 % 3
Please select the best answer.
  A. 4
  B. 7
  C. 1
 

The correct answer is A.

The expression is evaluated as if it had these parenthesis: 3 + ((4 * 7) % 3)

2. What is the meaning of the qq operator?
Please select the best answer.
  A. It delimits, but does not interpolate, a string
  B. It interpolates, but does not delimit, a string
  C. It delimits and interpolates a string
 

The correct answer is B.

The qq operator interpolates a string and is identical to using double quotes. The qq operator does not delimit a string, however. It requires a separate delimiting character, such as |.

3. What is the result of this expression:
$s = '.' x 75;
Please select the best answer.
  A. 75
  B. A string of seventy-five periods
  C. It cannot be evaluated
 

The correct answer is B.

The x operator is for repeating strings.

4. What is printed after the following statements are executed?
$i = 3;
print $i--;

Please select the best answer.
  A. 1
  B. 2
  C. 3

The correct answer is C.

The decrement operator -- will subtract 1 from the value of $i after the value of $i is printed. Remember that when the increment or decrment operator is post-fixed to a variable, the value of that variable is incremented or decremented after the variable is evaluated. To print 2, you could use the pre-fix form of the decrement operator in the print statement: print --$i;.

5. Give the value of $y after this code:
$x = '';
$y = $x ? "one" : "two"

Please select the best answer.
  A. "two"
  B. "one"
  C. "twelve"
 

The correct answer is A.

$x is false, which makes the ternary operator evaluate its later expression.

6. What will be the value of $z:
$z = -f "foo"
Please select the best answer.
  A. The contents of the file "foo"
  B. The integer 1
  C. True if "foo" exists
 

The correct answer is C.

-f is the file test operator.

7. What will be printed by this code:
@a = (5 ... 9);
print join ', ', @a, "\n";

Please select the best answer.
  A. 56789
  B. 5, 6, 7, 8, 9
  C. 5, 6, 7, 8, 9,
 

The correct answer is C.

The newline is part of the list that gets passed to join, so the comma at the end is really just separating the newline element. If you don't want the comma, use parenthesis like this: "print join (', ', @a), "\n";".

8. What is the value of $x after this code:
$a = 1; $b = 2; $c = 3; $x = $a ? $b : $c;

Please select the best answer.
  A. 1
  B. 2
  C. 3
 

The correct answer is B.

Since $a is true (1), $b (2) is assigned to $x.

9. What does this code do:
$filename = 'testfile';
open(FILE, ">>$filename");
print FILE "Hiya!\n";
close(FILE);

Please select the best answer.
  A. Writes "Hiya!\n" to the file
  B. Appends "Hiya!\n" to the file
  C. Reads "Hiya!\n" from the file
 

The correct answer is B.

The >> specifies that the file will be opened for "append".