Explore the fundamentals of Perl data types with this quiz focused on scalars, arrays, and hashes. Assess your understanding of Perl variables, syntax, and common operations with practical examples designed for beginners.
Which of the following defines a valid scalar variable in Perl that stores the integer value 42?
Explanation: A scalar variable in Perl is always prefixed with a dollar sign, like $num. The at symbol (@) is for arrays, and the percent symbol (%) indicates hashes, so @num and %num are incorrect for scalars. The ampersand (u0026) is used for subroutines, not scalar variables.
Given the array @colors = ('red', 'green', 'blue'), how would you access the first element?
Explanation: Array elements in Perl are accessed with dollar sign, array name, and index in square brackets, such as $colors[0]. Using curly braces is for hashes, not arrays, so @colors{0} and $colors{0} are incorrect. %colors[0] is not valid syntax for accessing array elements.
Which option correctly declares a hash in Perl that maps 'apple' to 'red' and 'banana' to 'yellow'?
Explanation: Hashes in Perl are prefixed by the percent symbol (%) and use the '=u003E' operator for key-value pairs. @fruits would create an array, not a hash. $fruits denotes a scalar, and u0026fruits is for subroutines, making them incorrect for hash declarations.
How can you obtain the number of elements in the Perl array @days?
Explanation: The 'scalar' keyword when applied to @days returns the count of elements in the array. @days.length and length @days are incorrect as they do not represent valid Perl syntax. %days.count refers to a hash method which isn't applicable here.
Given %capitals = ('France' =u003E 'Paris', 'Italy' =u003E 'Rome'), which statement accesses the capital of France?
Explanation: To get the value for a specific key in a hash, use $capitals{'France'}. @capitals['France'] and $capitals[France] use incorrect variable types or missing quotes. %capitals['France'] is not valid to retrieve a value; it is just a reference to the hash itself.
What is the preferred operator in Perl for concatenating two scalar strings, such as $first and $last?
Explanation: The concatenation operator in Perl is the period (dot) symbol. The plus sign adds numeric values, not strings. The ampersand is used for calling subroutines, and the semicolon is a statement terminator, not an operator.
Which statement correctly initializes an array in Perl containing the numbers 1 through 4?
Explanation: Arrays use the @ symbol and are assigned with parentheses, as in @nums = (1, 2, 3, 4). $nums would be a scalar, %nums is for hashes and the square brackets are incorrect in this context, and u0026nums refers to a subroutine.
If $data = 'Sample', what type of data structure does $data represent in Perl?
Explanation: A variable with a dollar sign is a scalar, storing a single value such as a string or number. Arrays use @, hashes use %, and subroutines may be invoked with u0026. Thus, Array, Hash, and Subroutine are incorrect in this context.
Suppose @letters = ('a', 'b', 'c'); how would you change the second element to 'z'?
Explanation: Array elements are updated using the array name with a dollar sign and square brackets, so $letters[1] = 'z'; is correct. Using curly braces, percent sign, or parentheses as in the other answers does not follow correct Perl array syntax.
Which Perl function helps determine if a scalar variable $score is defined?
Explanation: The defined function checks if a scalar variable has a value assigned. The exists function is for hash keys, not scalars. has and finite are not valid Perl functions for this purpose. Thus, defined($score) is the correct method to check for variable existence.