Приглашаем посетить
Чуковский (chukovskiy.lit-info.ru)

Workshop

Previous Table of Contents Next

Workshop

Quiz

1:

What's an efficient way to swap the values contained in two scalar variables $a and $b?

  1. $a=$b;

  2. ($a,$b)=($b, $a);

  3. $c=$a; $a=$b; $b=$c;

2:

What does the statement $a=scalar(@array); assign to the variable $a?

  1. The number of elements in @array

  2. The index of the last element of @array

  3. That syntax is not valid

3:

What does the statement $a=@array; assign to the variable $a?

  1. The number of elements in @array

  2. The index of the last element of @array

  3. That syntax is not valid

Answers

A1:

b. The first choice clearly will not work; the value contained in $a is destroyed. Choice c answers the question but requires a third variable to hold the data during the swap. Choice b swaps the data correctly, using no extra variables, and is fairly clear code.

A2:

a. Using an array in a scalar context returns the number of elements in the array. $#array would have returned the last index of the array. The use of scalar() in this example is unnecessary; having a scalar on the left side of the assignment operator is enough to put @array in a scalar context.

A3:

a. Trick question, it's actually the same as #2, except that the context is implied by the assignment instead of being explicitly given with the scalar operator.

Activities

  • Modify the Hangman game to print the hangman in an upright position.

    Previous Table of Contents Next