Perl Variable - Quiz Explanation

The correct answers are indicated below, along with the text that explains the correct answers.
 
1. What is a scalar?
Please select the best answer.
  A. A one-dimensional variable
  B. A one-dimensional value
  C. A numeric value
  The correct answer is B.
A scalar is any single, or one-dimensional value. A scalar value becomes a variable only when it has a name. Scalar values can be of any one-dimensional data type, such as strings or numbers.


2. What is a list in Perl?
Please select the best answer.
  A. An ordered series of scalars
  B. An unordered series of scalars
  C. An array that uses a string index
  The correct answer is A.
A list (in Perl) is an ordered series of scalars. An array that uses a string index instead of a numeric index is called a hash or an associative array.

3. Why is the $ character used to access array members?
Please select the best answer.
  A. Because an array is a scalar value
  B. Because array members are scalar values
  C. Because array members are non-scalar values
  The correct answer is B.
When accessing an array member, you are accessing a scalar value because arrays are simply aggregate structures made up of scalar values.


4. How do you get the number of elements in an array?
Please select the best answer.
  A. Test for a null; for example, if(@array[$i] == 0)
  B. Check the array in scalar context; for example, scalar @array
  C. Use a special function or macro; for example, sizeof @array
  The correct answer is B.
The value of an array in scalar context is the number of elements in the array.

5. Which one of the following pseudo code statements would you use to get an element from a hash?
Please select the best answer.
  A. $hash{key}
  B. %hash{key}
  C. @hash[key]
  The correct answer is A.
Because the element that you are extracting is a scalar value, the $ symbol is used to access it.


6. What does the split function do?
Please select the best answer.
  A. Removes pieces from a string
  B. Creates a string from a list
  C. Creates a list of parts of a string
  The correct answer is C.
The split function creates a list from parts of a string, using a regular expression to specify the pattern that separates the parts.

7. What does the join function do?
Please select the best answer.
  A. Adds pieces to a string
  B. Creates a string from a list
  C. Creates a list of parts of a string
  The correct answer is B.
The join function creates a string by joining the elements of a list together. A delimiter string is specified as the glue between the elements.


8. What is a stack?
Please select the best answer.
  A. A first-in/first-out (FIFO) data structure
  B. A last-in/first-out (LIFO) data structure
  C. A first-in/last-out (FILO) data structure
  The correct answer is B.
A stack is a LIFO (last-in/first-out) data structure--the most recently added data is always the first data retrieved.

9. What does the push function do?
Please select the best answer.
  A. Adds an item to an array at the beginning
  B. Adds an item to an array at the end
  C. Deletes all items in an array
  The correct answer is B.
The push function adds an item to the end of an array, in the manner of a stack.