Dive into the intricacies of Perl’s advanced data structures with this set of MCQs. Perfect for mastering multi-dimensional arrays, nested hashes, and the use of references for complex data handling.
Multi-dimensional Arrays (10 Questions)
Which syntax correctly initializes a multi-dimensional array in Perl? A) @array = ([1, 2], [3, 4]); B) @array = {1, 2, 3, 4}; C) @array = ([1, 2], (3, 4)); D) @array = [[1, 2], [3, 4]];
How do you access the element at the first row and second column in a multi-dimensional array? A) $array[0, 1] B) $array[1][0] C) $array[0][1] D) @array[0][1]
What type of data can multi-dimensional arrays in Perl hold? A) Scalars only B) Scalars and arrays only C) Any data type D) Hashes only
How are Perl multi-dimensional arrays internally stored? A) As a tree structure B) As an array of references to arrays C) As a linked list of arrays D) As a single flat array
What happens if you try to access an element in a multi-dimensional array that doesn’t exist? A) Throws an error B) Returns undef C) Appends the element D) Initializes the element to zero
Which statement is used to iterate through all elements of a multi-dimensional array? A) foreach my $element (@array) B) foreach my $row (@array) C) foreach my $element (@array[$i]) D) Both A and B
What is the result of this code: @array = ([1, 2], [3, 4]); print $array[1][0];? A) 1 B) 2 C) 3 D) 4
How do you dynamically add a new row to a multi-dimensional array? A) $array[] = [new_row]; B) push @array, [new_row]; C) @array[0] = [new_row]; D) splice @array, [new_row];
Can a Perl multi-dimensional array hold a mixture of data types (e.g., numbers, strings)? A) Yes B) No
What does scalar @array return for a multi-dimensional array? A) The total number of elements across all rows B) The number of rows C) The size of the first row D) Throws an error
Nested Hashes (10 Questions)
Which syntax initializes a nested hash in Perl? A) %hash = ('key' => {'subkey' => 'value'}); B) %hash = ('key' => ['subkey', 'value']); C) %hash = ['key' => {'subkey' => 'value'}]; D) %hash = {'key' => {'subkey' => 'value'}};
How do you access the value stored under 'key' and 'subkey' in a nested hash? A) $hash{'key'}{'subkey'} B) $hash{'key'}->'subkey' C) $hash{'key'}['subkey'] D) $hash->{'key'}{'subkey'}
Can a Perl hash have arrays or other hashes as values? A) No B) Yes
What function can you use to iterate through all keys of a nested hash? A) foreach my $key (keys %hash) B) foreach my $key (@keys %hash) C) foreach my $key ($hash) D) map %hash
What is the default value returned if a key does not exist in a hash? A) undef B) 0 C) Empty string D) Throws an error
What is a practical use case for nested hashes? A) Storing a multi-level configuration B) Representing database records C) Creating hierarchical data models D) All of the above
How can you check if a subkey exists in a nested hash? A) exists $hash{'key'}->{'subkey'} B) defined $hash{'key'}->{'subkey'} C) Both A and B D) None of the above
How do you delete a specific subkey in a nested hash? A) delete $hash->{'key'}->{'subkey'} B) undef $hash->{'key'}->{'subkey'} C) delete $hash{'key'}->{'subkey'} D) Both A and C
Is it possible to assign a nested hash to another variable directly? A) No, a deep copy is required B) Yes, but it creates a reference C) Yes, it duplicates the hash
What happens when you print a nested hash directly? A) Outputs the entire hash structure B) Prints the hash reference C) Throws an error D) Returns an empty string
Using References for Complex Structures (10 Questions)
What is the correct way to create a reference to a scalar? A) $ref = \$scalar; B) $ref = &$scalar; C) $ref = @scalar; D) $ref = *scalar;
What operator is used to dereference a reference? A) % B) @ C) $ D) *
How do you create a reference to an array? A) $ref = \@array; B) $ref = &$array; C) $ref = %array; D) $ref = *array;
Which of the following statements is true about Perl references? A) They can only point to scalars B) They enable complex data structures C) They are immutable D) They cannot be nested
How do you access an element of an array reference? A) $ref->[index] B) $ref->index C) $ref->(index) D) $ref->@index
How do you assign a complex structure (hash of arrays) to a reference? A) $ref = \%hash_of_arrays; B) $ref = \@hash_of_arrays; C) $ref = %hash_of_arrays; D) $ref = \%{hash_of_arrays};
What is the advantage of using references in Perl? A) Memory efficiency B) Simplifies handling complex data C) Easier function parameter passing D) All of the above
What is returned when you print a reference directly? A) The value it points to B) A memory address C) undef D) The data type of the reference
Which syntax converts a reference back into the original variable? A) Dereferencing B) Unreferencing C) Deallocating D) Reassigning