Showing posts with label PERL Interview Questions. Show all posts
Showing posts with label PERL Interview Questions. Show all posts

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 :

Oct 13, 2021

PERL Interview Questions for VLSI Jobs : Part 2


1. What is interpolation in PERL ?

Double-quoted strings are “interpolated” : any variable names found inside the string will be replaced by their value. 

Example : 

2. Why we use strict module ?
Or
Why  “use strict” is a Must while using PERL Data Structures ?

Here is our PERL Code :


When we execute the code we get the below error message due to use of the STRICT Module :
Execution of ./accident.pl aborted due to compilation errors.
As a Corrective Action , Change  the print command to : print $aref->[2][2]

Global symbol "@aref" requires explicit package name (did you forget to declare "my @aref"?) at ./accident.pl line 9.


3. Tell/Show the  Difference between Chop and Chomp :

The chomp() function removes ONLY new line character from the end of the string. 
WHEREAS
The Perl chop() function removes last character from a string regardless of what that character is. 
Let us see the example below :


Produces the below output :

=======Mary Had a Little Lamb=============
=======Mary Had a Little Lamb=============
=======Mary Had a Little Lamb =============
=======Mary Had a Little Lamb=============
=======Mary Had a Little Lamb Once=============
=======Mary Had a Little Lamb Onc=============

Now you can spot the difference ..... or else watch the video well below this page.


4. Tell/Show The Difference between Concat & Join  :

  • Concat & Join both are used to stitch more than one strings together.
  • Concat is used in very straight forward & its a binary operator.
  • Join is a PERL function to join contents of a list/array.

Here goes our example to show the difference:

The output is :

 Happy Birthday
 Happy Morning 

Now you can spot the difference ..... or else watch the video well below this page.


5. How to local-ise a variable ?

This below example will help you to do that :

Which gives the output :

 Num=100 @ outside 
 Num=10 A
 Num=10 Quick
 Num=10 Brown
 Num=10 Fox

Watch the video well below this page for explanation.


6. How to read a file directly in to an array ?

Our input file contains : 
A quick 
Brown fox
Jumped Over
A Lazy
White Dog


Which we are reading through the below PERL Code:


Which gives output of the file being read:

$VAR1 = [
          'A quick ',
          'Brown fox',
          'Jumped Over',
          'A Lazy',
          'White Dog'
        ];



For detailed explanation of all above QnA, please watch this video :








Courtesy : www.pngegg.com

Oct 11, 2021

PERL Interview Questions for VLSI Jobs : Part 1


In this article we will discuss some popular interview questions in PERL.


1. #!/usr/bin/perl -w : What does the -w means ?

It means that per interpreter will issue warning wherever applicable during code execution.


2. Are the Variables in PERL Case Sensitive ?

Yes ! Perl is Case-Sensitive, i.e. $var is a completely different variable than $VAR.


3. How we can do the single and multi line comment in PERL ?

# is a single line comment .
=cut ends the Multi Line comment


8. What are the data types in PERL ?
Any thing an be  assigned in the value of a variable :
$pi = 3.14159265; # is a "real" number
$animal = "Camel"; # is a string
$ab = $a * $b; # an mathematical operation
$out = `pwd`; # output from a command stored in $out


9. State the difference among – my,local,our
our $bar;      # declares $Foo::bar for rest of lexical scope
$bar = 20;
package Bar;
our $bar = 30; # declares $Bar::bar for rest of lexical scope
print $bar;    # prints 30
our $bar;      # emits warning but has no other effect
print $bar;    # still prints 30


10. What are the Reserved Variables in PERL ?
$_  The default input and pattern-searching space. It is an global variable.
The following functions use $_ as a default argument:
abs, alarm, chomp, chop, chr, chroot, cos, defined, eval, evalbytes, exp, fc, glob, hex, int, lc, lcfirst, length, log, lstat, mkdir, oct, ord, pos, print, printf, quotemeta, readlink, readpipe, ref, require, reverse (in scalar context only), rmdir, say, sin, split (for its second argument), sqrt, stat, study, uc, ucfirst, unlink, unpack.
The pattern matching operations m//, s/// and tr/// (or, y///) in absence of =~ operator.
@_   Within a subroutine the array @_ contains the parameters passed to that subroutine. Inside a subroutine, @_ is the default array for the array operators pop and shift.

11. What is the multi line comment in PERL ?

=for starts the Multi Line comment


12. What values of a variable is considered as True & False in PERL ?

i. '0' Zero the number itself is false.

ii. The empty string ’ ’ and the string ’0’ are false.

iii. undef is false.

iv. Anything else is true.


13. How to execute a PERL program when you do not have permission of writing in a particular disk ?

perl -e 'print "\n Hello, world \n";'


14. What is the value by default associated to a scalar when is declared but not assigned ?

my $var; contain the value undef.

There is no separate type declaration for Integer, Real, Double/Float in PERL.

A my() declares the listed variables to be local (lexical) to the enclosing block, file, or eval. If more than one variable is listed, the list must be placed in parentheses.

A local() modifies the listed variables to be local to the enclosing block, file, or eval. If more than one value is listed, the list must be placed in parentheses. 

An our() creates a package variable: It declares an alias for a package variable that will be visible across its entire lexical scope, even across package boundaries. 

package Foo;

a & $b :  Special package variables when using sort(). $a and $b don't need to be declared (using use vars, or our()) even when using the strict 'vars' pragma. 

The default iterator variable in a foreach loop if no other variable is supplied.

%ENV : The hash %ENV contains your current system environment variables & their values can be manipulated by this hash/associative array. 

@INC : The array @INC contains the list of places that the do EXPR, require, or use constructs look for their library files. It initially consists of the arguments to any -I command-line switches, followed by the default Perl library, probably /usr/local/lib/perl.

@ISA : Each package contains a special array called @ISA which contains a list of that class's parent classes, if any. This array is simply a list of scalars, each of which is a string that corresponds to a package name. The array is examined when Perl does method resolution, which is covered in perl obj.

$$ The process number of the Perl running your present script itself.

$0 Contains the name of the Perl program being executed.

$<digits> ($1, $2, ...) : Contains the sub-pattern from the corresponding set of capturing parentheses from the last successful pattern match, not counting patterns matched in nested blocks that have been exited already.

$& : The string matched by the last successful pattern match (not counting any matches hidden within a BLOCK or eval() enclosed by the current BLOCK).

$ARGV : Contains the name of the current file when reading from <>.

@ARGV : The array @ARGV contains the command-line arguments intended for the script. 

Watch the Video for  detailed explanation :