Mid-term Exam (100 points)
Write next to # sign in of the following statements the output of each statement:
1. 'silly\\me' # silly, followed by backslash, followed by me
2. "coke\tsprite" # a coke, a tab, and a sprite
3. 14 / 2 # 14 divided by 2, or 7
4. print (5.1 - 2.4, "\n"); # 2.7
5. "hello" . "world" # same as "helloworld"
6. "fred" . " " . "barney" # same as "fred barney"
7. "barney" x (4+1) # is "barneybarneybarneybarneybarney"
8. (3+2) x 4 # is 5 x 4, or really "5" x 4, which is "5555"
9. 72 / 12 / 3 # (72 / 12) / 3, or 6/3, or 2
10. "X" . (4 * 5) # same as "X" . 20, or "X20"
11. $a = 17;
12. $b = $a + 3; # give $b the current value of $a plus 3 (20)
13. $b = $b * 2; # give $b the value of $b multiplied by 2 (40)
14. $b = 4 + ($a = 3); # assign 3 to $a, then add 4 to that # resulting in $b getting 7
15. $a += 1; # with assignment operator
16. ++$a; # with prefix auto increment
17. $d = 17; #
$fred = "pay";
$fredday = "wrong!";
18. $barney = "It's $fredday"; # not payday, but "It's wrong!"
19. $barney = "It's ${fred}day"; # now, $barney gets "It's payday"
20. $barney2 = "It's $fred"."day"; # another way to do it
21. $barney3 = "It's " . $fred . "day"; # and another way
22. $a = <STDIN>; # get the text
23. chomp($a); # get rid of that pesky newline
24. chomp($a = <STDIN>);#
25. (1 .. 5) # same as (1, 2, 3, 4, 5)
26. (1.2 .. 5.2) # same as (1.2, 2.2, 3.2, 4.2, 5.2)
27. (2 .. 6,10,12) # same as (2,3,4,5,6,10,12)
28. ($a .. $b) # range determined by current values of $a and $b
29. (1.3 .. 6.1) # same as (1.3,2.3,3.3,4.3,5.3)
30. @a = qw(fred barney betty wilma); #
31. print("The answer is ",@a,"\n"); #
32. @fred = qw(one two);
33. @barney = (4,5,@fred,6,7); # @barney becomes
34. @barney = (8,@barney); # puts 8 in front of @barney
35. @barney = (@barney,"last");# and a "last" at the end
36. ($a,$b,$c) = (1,2,3); # give 1 to $a, 2 to $b, 3 to $c
37. ($a,$b) = ($b,$a); # swap $a and $b
38. ($d,@fred) = ($a,$b,$c); # give $a to $d, and ($b,$c) to @fred
39. ($e,@fred) = @fred; # remove first element of @fred to $e
40. @fred = (4,5,6); # initialize @fred
41. $a = @fred; # $a gets 3, the current length of @fred
42. $a = @fred; # $a gets the length of @fred
43. ($a) = @fred; # $a gets the first element of @fred
44. @fred = (@barney = (2,3,4)); # @fred and @barney get (2,3,4)
45. @fred = @barney = (2,3,4); # same thing
46. @fred = (7,8,9);
47. $b = $fred[0]; # give 7 to $b (first element of @fred)
48. $fred[0] = 5; # now @fred = (5,8,9)
49. $c = $fred[1]; # give 8 to $c
50. $fred[2]++; # increment the third element of @fred
51. $fred[1] += 4; # add 4 to the second element
52. ($fred[0],$fred[1]) = ($fred[1],$fred[0]); # swap the first two
53. @fred[0,1]; # same as ($fred[0],$fred[1])
54. @fred[0,1] = @fred[1,0]; # swap the first two elements
55. @fred[0,1,2] = @fred[1,1,1];# make all 3 elements like the 2nd
56. @fred[1,2] = (9,10); # change the last two values to 9 and 10
57. @who = (qw(fred barney betty wilma))[2,3];
58. @fred = (7,8,9);
59. $a = 2;
60. $b = $fred[$a]; # like $fred[2], or the value of 9
61. $c = $fred[$a-1]; # $c gets $fred[1], or 8
62. ($c) = (7,8,9)[$a-1]; # same thing using slice
63. @fred = (7,8,9); # as in previous example
64. @barney = (2,1,0);
65. @backfred = @fred[@barney];
66. push(@mylist,$newvalue); # like @mylist = (@mylist,$newvalue)
67. $oldvalue = pop(@mylist); # removes the last element of @mylist
68. @mylist = (1,2,3);
69. push(@mylist,4,5,6); # @mylist = (1,2,3,4,5,6)
70. unshift(@fred,$a); # like @fred = ($a,@fred);
71. unshift(@fred,$a,$b,$c); # like @fred = ($a,$b,$c,@fred);
72. $x = shift(@fred); # like ($x,@fred) = @fred;
73. @fred = (5,6,7);
74. unshift(@fred,2,3,4); # @fred is now (2,3,4,5,6,7)
75. $x = shift(@fred); # $x gets 2, @fred is now (3,4,5,6,7)
76. @a = (7,8,9);
77. @b = reverse(@a); # gives @b the value of (9,8,7)
78. @b = reverse(7,8,9); # same thing
79. @b = reverse(@b); # give @b the reverse of itself
80. @x = sort("small","medium","large");
81. # @x gets "large","medium","small"
82. @y = (1,2,4,8,16,32,64);
83. @y = sort(@y); # @y gets 1,16,2,32,4,64,8
84. @stuff = ("hello\n","world\n","happy days");
85. @a = <STDIN>; # read standard input in a list context
86. @fred = ("hello","dolly"); #
87. $y = 2; #
88. $x = "This is $fred[1]'s place"; # "This is dolly's place"
89. $x = "This is $fred[$y-1]'s place"; # same thing
90. @fred = ("hello","dolly"); # give value to @fred for testing
91. $fred = "right"; # we are trying to say "this is right[1]"
92. $x = "this is $fred[1]"; # wrong, gives "this is dolly"
93. $x = "this is ${fred}[1]"; # right (protected by braces)
94. $x = "this is $fred"."[1]"; # right (different string)
95. $x = "this is $fred\[1]"; # right (backslash hides it)
96. @fred = ("a","bb","ccc",1,2,3);
97. $all = "Now for @fred here!";
98. # $all gets "Now for a bb ccc 1 2 3 here!"
99. @fred = ("a","bb","ccc",1,2,3);
100.
$all = "Now for @fred[2,3] here!"; # $all gets "Now for ccc 1 here!"
© Copyright 2026 Paperzz