Home Interview Questions and Answers Perl Interview Questions and Answers For Graduates Part-5

perl41.How will you add an element to a hash?
Adding a new key/value pair can be done with one line of code using simple assignment operator.

#!/usr/bin/perl

%data = (‘John Paul’ => 45, ‘Lisa’ => 30, ‘Kumar’ => 40);
@keys = keys %data;
$size = @keys;
print “1 – Hash size: is $size\n”;

# adding an element to the hash;
$data{‘Ali’} = 55;
@keys = keys %data;
$size = @keys;
print “2 – Hash size: is $size\n”;
This will produce the following result −

1 – Hash size: is 3
2 – Hash size: is 4

42.How will you remove an element from a hash?
To remove an element from the hash you need to use delete function as shown below in the example−

#!/usr/bin/perl

%data = (‘John Paul’ => 45, ‘Lisa’ => 30, ‘Kumar’ => 40);
@keys = keys %data;
$size = @keys;
print “1 – Hash size: is $size\n”;

# delete the same element from the hash;
delete $data{‘John Paul’};
@keys = keys %data;
$size = @keys;
print “2 – Hash size: is $size\n”;
This will produce the following result −

1 – Hash size: is 3
2 – Hash size: is 2

43.What is the purpose of next statement?
It causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. last statement.

44.What is the purpose of last statement?
It terminates the loop statement and transfers execution to the statement immediately following the loop. continue statement.

45.What is the purpose of continue statement?
A continue BLOCK, it is always executed just before the conditional is about to be evaluated again.

46.What is the purpose of redo statement?
The redo command restarts the loop block without evaluating the conditional again. The continue block, if any, is not executed.

47.What is the purpose of goto Label statement?
The goto LABEL form jumps to the statement labeled with LABEL and resumes execution from there.

48.What is the purpose of goto Expr statement?
The goto EXPR form is just a generalization of goto LABEL. It expects the expression to return a label name and then jumps to that labeled statement.

49.What is the purpose of goto &NAME statement?
It substitutes a call to the named subroutine for the currently running subroutine.

50.What is the purpose of ** operator?
Exponent − Performs exponential (power) calculation on operators. Assume variable $a holds 10 and variable $b holds 20 then $a**$b will give 10 to the power 20.

You may also like

Leave a Comment