Perl Variables  «Prev  Next»

Hash Functions in Perl - Quiz

Each question is worth one point. Select the best answer for each question.
1. What is the difference between a hash and an ordinary array?
Please select the correct answer:
  A. Ordinary arrays cannot contain string values, while hashes can contain any type of scalar values
  B. Ordinary arrays use a numeric index, while hashes use a string index
  C. Ordinary arrays use a string index, while hashes use a numeric index

2. In what order is a hash stored?
Please select the correct answer:
  A. The order defined by the number of values in the hash
  B. The order defined by the internal hashing algorithm
  C. The order in which the values were stored in the hash

3. The hash %person contains a key called name. Which expression returns the single scalar value stored under that key?
Please select the correct answer:
  A. $person{'name'}
  B. %person{'name'}
  C. @person{'name'}
  D. person{'name'}

4. The hash %config contains a key called timeout whose value is undef. What do exists $config{'timeout'} and defined $config{'timeout'} return?
Please select the correct answer:
  A. Both return true
  B. Both return false
  C. exists returns true and defined returns false
  D. exists returns false and defined returns true

5. How do you remove a key and its value from a hash entirely?
Please select the correct answer:
  A. Assign undef to it: $h{'k'} = undef;
  B. undef $h{'k'};
  C. Assign the empty string: $h{'k'} = '';
  D. delete $h{'k'};

6. In scalar context, as in my $n = keys %config;, what does keys return?
Please select the correct answer:
  A. A list of the keys in the hash
  B. The number of keys in the hash
  C. The first key in the hash
  D. A reference to a list of the keys

7. In my %color = (apple => 'red', lime => 'green'); what does the fat comma => do that an ordinary comma would not?
Please select the correct answer:
  A. It automatically quotes a bareword on its left, so apple becomes the string 'apple'
  B. It marks the item on its left as a hash key rather than an array element
  C. It creates a reference to the value on its right
  D. It prevents the list from being flattened

8. Given the hash %color, what does @color{'apple','lime'} return?
Please select the correct answer:
  A. The two keys, 'apple' and 'lime'
  B. A syntax error, because a hash must be written with the % sigil
  C. A list containing the values stored under both keys
  D. The number of keys that matched

9. You write my %opts = (%defaults, %user); and both hashes contain a key called timeout. Which value ends up in %opts?
Please select the correct answer:
  A. The value from %defaults, because it appears first
  B. Both values, stored as a list under the one key
  C. Neither; the assignment fails with a duplicate key error
  D. The value from %user, because a later pair overwrites an earlier one for the same key

10. After my $ref = \%config; which expression retrieves the value stored under the key host?
Please select the correct answer:
  A. $ref{'host'}
  B. @ref{'host'}
  C. $ref->{'host'}
  D. $ref['host']