Oct 19, 2021

PERL Interview Questions for VLSI Jobs : Part 3



1. State the Basic Difference between Array in C-Language and Array in PERL:

Arrays/List are variable length. 

Array/List can be declared without length.

This length can vary even after creation.

Array/List can contain any sorts of scalars (even a combination of strings, numeric, etc.) I.e it is heterogeneous.


2. Is the below is valid or invalid ?

my @array3 = (@array1, @array2); 

It merges the two array in RHS into a Single Array at LHS.


3. Pick Up The Invalid Array 

my @arr = (1, 2, 3);

my @arr = ("orange", 2.7)

my @arr = ($b, 21, $c);

my @arr = ()

my @arr = (1..5)

my @arr = (1..3, 7, 11..13)

my @arr = ($b .. $c)

my @arr = ("apple", "orange", "jackfruit", "grapes");

my @arr = qw(apple orange jackfruit grapes)

Run the following code snippet in your machine. See the video down below this article for explanation.


4. State the difference between  scalar(@array)  and  $#array 

scalar(@arr) ; #gives the array length

$#arr # gives the index of the last element of array


5. Please let us know if this invalid :

$arr[-1] 

$arr[-2]

Both gives the element of @arr from the backside.

6. Is @arr[2,4] is a Two Dimensional Array ?

No this is called Array Slicing.

my $arr = qw(apple orange jackfruit grape mango);

@arr[2,4] # is not a two dimensional array, it is ($a[2], $a[4]);

my @subArray1 = @arr[2,4] # actually is ("jackfruit" "mango") ;

my @subArray2 = @arr[2..4] # actually is ("jackfruit" "grape" "mango");


7. How Can you Sort an Array Numerical Ascending or Descending order  ?

@sorted = sort { $a <=> $b } @unsorted; # sorts integer/real (ascending Order) 

@sorted = sort { $b <=> $a } @unsorted; # sorts integer/real (descending Order)

# N.B = Here $a & $b are PERL reserved Variable , no need to declare them


8. Tell Me What are the LHS values in each below case :

my ($first, $second) = @array;

my ($first, @rest) = @array;

my (@copy, $dummy) = @array;

Ans :

my ($first, $second) = @array;

$first and $second are assigned the first and second elements of @array,

my ($first, @rest) = @array;

$first is assigned the first element of @array, and @rest is assigned everything else.

my (@copy, $dummy) = @array;

In this case $dummy is not assigned.

Arrays are variable-length, so @copy has no reason to stop.

Hence  $dummy ends up with undef since @copy “eats up” the entire available array.


9. How you can access Key/Value Pair at once i.e. using a single command ?


while (($key,$value) = each(%hash)) {

   printKey = $key has Value = $value \n”;

}


10. How you can covert Array into Hash and vise versa ?

my @arr = %hash; # converts a hash into a array

my %newHash = @arr; # converts array back to a hash

Provided in each case the elements in the array are even number.


11. For an unknown Reference How you can check what is the corresponding data type ?

We have to use a perl inbuilt function called  “ref “ in the following way :

print "\n ref($scalarref)";

print "\n ref($arrayref)";

print "\n ref($hashref)";

print "\n ref($constref)";


12. How do you De-Reference a scalar/array/hash Reference Variable ?

my $dRef = $$scalarref;   #De-Reference a Scalar

my @arr = @$arrayref;  #De-Reference a Array

my %hash = %$hashRef;  #De-Reference a Hash


13. How Do you De-Reference an Anonymous Array Elements?

# Accessing A Value 
print "$ArrayRef->[0]";
print "$HashRef->{"KEY"}";
# Assigning A Value
$ArrayRef->[0] = "January";   # Array element
$HashRef->{"KEY"} = "VALUE";  # Hash element


14. Without using any LOOP how can you print a complete data-structure in the STDOUT ?

use Data::Dumper
print Dumper($refDataStructure);


15. What is Globbing ?

Definition: The expansion of filename argument patterns into a list of matching filenames.

my @files = </home/docs/*.c>; # grabs list of files ending in .c
my @files = glob("/home/docs/*.c"); # same thing using glob operator
while (my $file = <abc*.html>) { 
# output each .html file starting with abc
print "File: $file \n"; 
}
foreach my $file (<abc*.html>) { 
# output each .html file starting with abc
print "File: $file \n";
 }

For detailed Explanation please watch the below video :