Answer to Question #22427 in Perl for Sandeep Saxena

Question #22427
Ques 1:
my $a == <stdin>;
#if input is given Pardeep
then what is the output of following script?

if($a == "Pardeep")
{
print "match";
}
elsif($a == "pardeep")
{
print "ok"
}
else{
print "not match";
}

a)match
b)not match
c)ok
d)none



Ques 2:
our $a = 8;
our $b = 7;

sub first
{
my $a =4;
local $b = 9;
&second;
}

print " $b ";

sub second
{
print $a;
print$b;
}
&first;


what is the result of above print command?

a)8
b)7
c)4
d)9
1
Expert's answer
2013-01-21T10:00:53-0500
Question 1:
correct answer is
a) match

However, the first command
my $a ==<stdin>;
does not assign to $a the value from stdin, since herecomparison operator "==" is used instead of "="
Moreover, in if/elseif/else statement it is usedNUMERICAL comparison operator "=="
is used for comparing strings.
In this case boths strings $a and "Pardeep" areconverted to zeros 0, and then these zeros are compared.
As they are equal,
($a =="Pardeep")
is true, and so the program prints "match"

If we'd use "eq" instead of "==": if($a eq"Pardeep")
{
print"match";
}
elsif($a eq"pardeep")
{
print"ok"
}
else
{
print"not match";
}

then the output will be "not match"Ques 2:The print command
print " $b";
will print the value of $b defined at line
our $b = 7;
since in true subroutine "first" the variable $bwith the same name is defined as "local"
So the correct answer is b) 7

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS