Multiple choice technology programming languages

How many times does the following loop iterates? %list = ("first", "1", "second", "2", "third", "3"); foreach $word (keys(%list)) { last if ($word == "second"); }

  1. four

  2. three

  3. two

  4. one

  5. Unpredictable

Reveal answer Fill a bubble to check yourself
E Correct answer
Explanation

The code is fundamentally broken - keys() is used on a @list ARRAY, but keys() only works on HASHES (%hash). Converting the list to a hash first would create key-value pairs, making the hash keys unpredictable. Even if we assume this was meant to be a hash, iteration order in Perl hashes is not guaranteed (randomized since Perl 5.18), so when the loop would hit 'second' is unpredictable. The code won't run as written.