Question 1 This question was intended to be a quick way to pick up some points, but it turned out to be the hardest question on the test. I ignored the first incorrect answer given by everyone when grading. # print 1 # variable's value is modified in: $var1 = "15th " + "St" . "8th " . "St"; # + and . have the same precedence, so we simply # operate from left to right # "15th " + "St" # + is a numeric operator, so Perl converts both operands to numbers # "15th " becomes 15 (makes as much of string as possible numeric. # ignores the rest) # "St" becomes 0 (since it doesn't start with a valid number) # thus we have 15 . "8th " . "St" # both periods are concatenation operators (expect strings) # 15 converted to "15" and concatenated with "8th " # we then have "158th " . "St", so the print gives us 158th St # print 2 # variable's value is modified in: $var2 = 2; # prints 2 # print 3 # variable's value is modified in: $var3 = "N = " . 13 x $var2; # x has higher precedence than . # 13 x $var2 is done first # x is repetition (expects a string on the left and a number on the # right), so we effectively have # "13" x 2 (this expression means repeat "13" twice) # now $var3 = "N = " . "1313", so the print gives us N = 1313 # print 4 # variable's value is modified in: $var4 = 'camel\n'; chomp($var4); $var5 = chop($var4); # note first of all that the assignment involved a single-quoted # string; thus, \n is not newline it is simply '\n' # Because of this chomp does nothing to $var4; however chop always # removes a single character, in this case, the 'n' at the end # Hence, the print gives us camel\ # print 5 # variable's value is modified in: $var5 = chop($var4); # as stated in print 4, the 'n' is removed from the string stored in # $var4 and this is what is returned by chop and stored in $var5 # The print gives us n # prints 6, 7, and 8 # variables' values determined by: @a1 = (1, 2, 3, 4); @a2 = reverse(@a1); # @a2 is (4, 3, 2, 1) ($var6, $var7, $var8) = @a2[0,1]; # the reverse saves the reverse of array @a1 in @a2 as shown above # @a2[0,1] is an array slice which equals (4, 3) # since we have a two element array slice and three scalar variables # to assign to, the last variable is given the value undef # The three prints thus give us 4 3 # print 9 # print's value determined by: @a1 = (1, 2, 3, 4); @a2 = reverse(@a1); # @a2 is (4, 3, 2, 1) # $a1[$a2[$#a1]] may be broken down as follows # $#a1 gives the last INDEX of @a1 which is 3 because @a1 has 4 # elements and indexing starts at 0 # $a2[$#a1] = $a2[3] which is the last element of @a2 -- 1 # $a1[$a2[3]] = $a1[1] which is the second element of @a1 -- 2 # The print thus gives us 2 # print 10 # print's value determined by: $a1 = 1; @a1 = (1, 2, 3, 4); @a2 = reverse(@a1); # ${a1}[$a2[-2]] # ${a1} tells Perl that this involves a simple scalar variable, not an # array access; thus it uses the value of $a1 and treats [ as a # literal square bracket # so far we have 1[$a2[-2]] # -2 tells Perl to access the second element from the end of @a2 which # is 2 # The final result is then 1[2]