MCQs on Advanced Data Structures | Perl

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)

  1. 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]];
  2. 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]
  3. 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
  4. 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
  5. 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
  6. 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
  7. What is the result of this code: @array = ([1, 2], [3, 4]); print $array[1][0];?
    A) 1
    B) 2
    C) 3
    D) 4
  8. 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];
  9. Can a Perl multi-dimensional array hold a mixture of data types (e.g., numbers, strings)?
    A) Yes
    B) No
  10. 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)

  1. 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'}};
  2. 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'}
  3. Can a Perl hash have arrays or other hashes as values?
    A) No
    B) Yes
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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)

  1. What is the correct way to create a reference to a scalar?
    A) $ref = \$scalar;
    B) $ref = &$scalar;
    C) $ref = @scalar;
    D) $ref = *scalar;
  2. What operator is used to dereference a reference?
    A) %
    B) @
    C) $
    D) *
  3. How do you create a reference to an array?
    A) $ref = \@array;
    B) $ref = &$array;
    C) $ref = %array;
    D) $ref = *array;
  4. 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
  5. How do you access an element of an array reference?
    A) $ref->[index]
    B) $ref->index
    C) $ref->(index)
    D) $ref->@index
  6. 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};
  7. 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
  8. 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
  9. Which syntax converts a reference back into the original variable?
    A) Dereferencing
    B) Unreferencing
    C) Deallocating
    D) Reassigning
  10. Can references in Perl be recursive?
    A) Yes
    B) No

Answers Table

QNoAnswer (Option with Text)
1A) @array = ([1, 2], [3, 4]);
2C) $array[0][1]
3C) Any data type
4B) As an array of references to arrays
5B) Returns undef
6D) Both A and B
7C) 3
8B) push @array, [new_row];
9A) Yes
10B) The number of rows
11A) %hash = ('key' => {'subkey' => 'value'});
12A) $hash{'key'}{'subkey'}
13B) Yes
14A) foreach my $key (keys %hash)
15A) undef
16D) All of the above
17C) Both A and B
18D) Both A and C
19B) Yes, but it creates a reference
20B) Prints the hash reference
21A) $ref = \$scalar;
22C) $
23A) $ref = \@array;
24B) They enable complex data structures
25A) $ref->[index]
26A) $ref = \%hash_of_arrays;
27D) All of the above
28B) A memory address
29A) Dereferencing
30A) Yes

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top