Multiple choice technology programming languages

What will be the value of \$keys after execution of the following code? my \$var; if (exists $var->{key1}->{key2}) { $var->{key1}->{key2} = 1; } my $keys = keys(%{$var});

  1. undef

  2. 0

  3. 1

  4. 2

  5. code will fail

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

When Perl evaluates exists $var->{key1}->{key2}, it autovivifies (auto-creates) the intermediate hash structure even though the key2 doesn't exist. This means $var->{key1} is created as an empty hash. The exists check returns false (key2 doesn't exist), so the assignment inside the if block doesn't execute. However, the autovivication already happened, leaving $var with one key (key1). keys(%{$var}) returns 1.