Perl Scalars in Context - Quiz Explanation
The correct answers are indicated below, along with the text that explains the correct answers.
1.
When is a scalar a number?
Please select the best answer.
A.
When it has no alphabetic characters
B.
When it's used in a numeric context
C.
When it's used in any context
The correct answer is B.
The
perl
program will convert the data between numeric and string representations as necessary, depending on the context.
2.
Given the line of code below, what is the type of the scalar variable
s
?
$s = "78"
Please select the best answer.
A.
$s
is a number
B.
$s
is a string
C.
$s
is a character
The correct answer is B.
The reason is really the same as the last question:
perl
will convert the data between numeric and string representations as necessary, depending on the context. In this case,
78
is enclosed in quotation marks, so it is a string, not a number. Therefore,
s
is a string.
3.
Which of the following is a valid Perl scalar data type?
Please select the best answer.
A.
Array (@array)
B.
Hash (%hash)
C.
Scalar ($scalar)
The correct answer is C.
Scalar ($scalar) is the correct answer, as scalar variables in Perl hold single values like numbers or strings. Arrays (@array) and Hashes (%hash) are collection types.