Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Perl is one of the most popular text processing languages in the world. It allows you to create extremely efficient text parsers for all sorts of applications.
This interactive course will help you get up to speed on Perl, starting off with basic concepts like loops, strings, and arrays. You’ll build your way up to more complex topics like data structures and packages.
No background knowledge is needed. Anyone willing to understand the principles and syntax of Perl to use it in the future will find this course useful.
Q1. Which symbol is used to add single line comment in Perl?
Q2. Which of the following is the correct Perl statement to display Hello World
on the screen?
cout << "Hello World" << endl;
echo "Hello World";
print "Hello World";
Q3. What is the error in the code below?
print "This is a print statement"
#
symbol is missing at the start of the code statementQ4. You can also use printf
function instead of print
function in Perl.
Answer:
print "My first Perl challenge!";
Q1. Which one is not a data type in Perl?
Q2. Which is not a floating point type?
Q3. Perl variables need to be declared before adding a value to it.
Q4. What is the data type of the value '5'
?
Q5. Which symbol is used to create an array?
Answer:
$intNumber = 1000;
$charName = 'N';
$boolAccept = 1;
$floatNum = 10.292;
print $intNumber . ", ";
print $charName . ", ";
print $boolAccept . ", ";
print $floatNum;
Q1. What is the output of the following code?
$myvar = 5;
print $myvar++;
Q2. What is the output of the following code?
$aa = 5;
$bb = 3;
print ($aa**$bb);
Q3. What is the ouput of the following expression?
print "42 + 7 % 2 = ". (42 + 7 % 2) ;
Q4. What is the result of the following code?
$aa = 5;
$bb = 2;
$cc = 4;
if ( $aa < $bb+$cc ) {
print $aa <=> ( $cc-$bb );
}
else {
print ( $cc-$bb ) <=> $aa;
}
Q5. What is the result of the following code?
$aa = true;
$bb = false;
$cc = true;
if ( $aa && $bb || $cc || !$cc) {
print ("inside if");
}
else {
print ("inside else");
}
Q6. What will the following program print?
$aa = 2;
$bb = 3;
$bb = ($aa += ($bb **= 5));
print "\$bb = ".$bb;
Q1. If $x=3 , $y=4, and $z=6, what is the value of $x, $y, and $z after executing the following code?
if ($y+2 == $z) {
$x = $x+1;
}
else {
$z++;
}
Q2. If originally $x=2, $y=1, and $z=3, what is the value of $x, $y, and $z after executing the following code?
if($y) {
$x = $x+1;
$z= $x+$y;
}
else {
$y--;
}
Q3. Ternary operator has one boolean expression, and returns one of two values depending on the value of a Boolean expression.
Q4. What will be displayed on the console when the code below is executed?
$x = 30, $y = 20;
$result = ($x > $y) ? $x : $y;
print $result;
Q5. If originally $x=1, $y=2, and $z=3, what is the value of $x, $y, and $z after executing the following code?
$x = 2;
given($x) {
when (1){
$x++;
$z=$x+1;
}
when (2) {
$y=$z+$x;
}
default {
$z=$z+$x;
}
}
Answer:
$temp=-1; #set temp to 0 if number is even
#set temp to 1 if number is odd
if($number % 2 == 0){
$temp = 0;
}
else{
$temp = 1;
}
Answer:
#temp will contain the final answer after the required operation is performed
$temp=-76575;
given($Operator) {
when ('+'){
$temp = $num1+$num2;
}
when ('-') {
$temp = $num1-$num2;
}
when ('*'){
$temp = $num1*$num2;
}
when ('/'){
$temp = $num1/$num2;
}
default{
print "Wrong operator";
}
}
Q1. Which of the loops is not a pretest loop?
Q2. Which of the following is not a nested loop?
for($i=0; $i<10; $i++){
for($j=1; $j<i+2; $j++){
}
}
for($i=0; $i<10; $i++){
print "i= $i";
}
for($j=1; $j<i+2; $j++){
print "j= $j";
}
for($i=0; $i<10; $i++){
while($j%2!=0)
{ print"$j";
$j++;}
}
while($j%2!=0){
for($i=0; $i<10; $i++){
print "$j";
$j++;
}
}
Q3. Which of the following statements about the while loop is not true?
Q4. Foreach loops do not keep track of index.
Q5. In the code below, when will the line $i = $i + 1;
execute?
while ( $i <= 10){
$i = $i + 1;
}
Q6. The until
loop:
Answer:
$ans = ""; #return the correct value in this string
$i = 1;
while($i <= 10){
$answer = $num * $i;
$ans .= $answer . " "; # .= operator append the result in $ans
$i++;
}
return $ans;
Answer:
$ans = "";
$first = 0;
$second = 1;
$fibonacci = 0;
for ($c = 0 ; $c < $range ; $c++ )
{
if ( $c <= 1 ){
$fibonacci = $c;
$ans .= $fibonacci . " ";
}
else{
$fibonacci = $first + $second;
$first = $second;
$second = $fibonacci;
$ans .= $fibonacci . " ";
}
}
$ans =~ s/\s+$//;
return $ans;
Answer:
for($i = 1; $i <= $rows; $i++) {
for($j = 1; $j <= $i; $j++)
{
print "a ";
}
print "\n";
}
Q1. A subroutine needs a subroutine definition to perform a specific task.
Q2. A local variable declared in a subroutine is usable outside that subroutine.
Q3. What is the value displayed at the end of the following subroutine?
sub fun {
$n = @_[0];
if ($n == 4) {
return $n;
}
else{
return 2 * $n;
}
}
print fun(3);
Q4. What is the correct way to create a subroutine in Perl?
Answer:
sub sum {
return @_[0] + @_[1];
}
Answer:
sub gpaPoint{
$grade = @_[0];
if($grade eq "A+")
{return 4;}
elsif($grade eq "A")
{ return 4;}
elsif($grade eq "A-")
{return 3.7;}
elsif($grade eq "B+")
{ return 3.3;}
elsif($grade eq "B")
{ return 3;}
elsif($grade eq "B-")
{ return 2.8;}
elsif($grade eq "C+")
{ return 2.5;}
elsif($grade eq "C")
{ return 2;}
elsif($grade eq "C-")
{ return 1.8;}
elsif($grade eq "D")
{ return 1.5;}
elsif($grade eq "F")
{ return 0;}
else
{return -1;}
}
Q1. Single characters can be extracted using array (square brace) syntax by using split
function.
Q2. What will be the output of the following code?
print index("machinelearning", "learning");
Q3. What is the output of the following code?
$str = "This is a string.";
print length($str);
Q4. What will the following code print?
$var1 = "Myname";
$var1 .= "is";
$var1 .= "James";
print $var1;
Q5. What is the output of the following code?
$str = "Learning is fun!";
print substr($str, 12, 4);
Answer:
sub stringCheck{
$str1 = @_[0];
$str2 = @_[1];
if(index($str1, $str2) >= 0) {
return 1;
}
else {
return -1;
}
}
Answer:
#Returns maximum value from Array passed as parameter
sub Find_Maximum {
my @list = @_;
$max = $list[0];
for ($i = 1; $i <= $#list; $i++) { #iterate over all the array elements
if ($list[$i] > $max) { #check if current element is greater than the already present elements
#stored max value
$max = $list[$i]; # if yes then update the max value to current element
}
}
return $max; #return the maximum value
}#end of Find_Maximum()
Answer:
sub printMat {
$n = @_[0];
@arr=();
for ($i=0;$i<$n;$i++) {
for ($j=0;$j<$n;$j++){
if ($i==$j){ # if row=column=> fill the matrix with 0
$arr[$i][$j] = 0;
}
elsif($i>$j){ # if row>columns=> fill matrix with -1
$arr[$i][$j] = -1;
}
else { # if row<columns=> fill matrix with 1
$arr[$i][$j] = 1;
}
}
}
for ($i=0;$i<$n;$i++) {
for ($j=0;$j<$n;$j++) {
print $arr[$i][$j]." ";
}
print "\n";
}
}
Q1. Size of arrays in Perl
Q2. In Perl arrays, the keys can be
Q3. An array $cars
is defined as follows
@cars = ("honda","toyota","mercedes", "BMW")
Which of the following line will print “mercedes” only
print $cars[3];
print $cars[2];
print $cars["mercedes"];
Q1. What does a shift()
function do?
Q2. What does a push
function do?
Q3. What will be the output of the following code:
@arr = (2,4,5,9);
unshift(@arr,14,15);
print "@arr";
Q4. What will be the output of the following code:
@arr=(2,4,5,9);
push(@arr,14,15);
print "@arr";
Q5. What will be the output of the following code:
@arr=(1,3,5,7,9,11);
shift(@arr);
print "@arr";
Q6. What will be the output of the following code:
@arr=(1,3,5,7,9,11);
pop(@arr);
print "@arr";
Q7. What will be output of the following code:
@numbers = (-22, 4, 1, 20, 0, -44);
@sorted_numbers = sort { $a <=> $b } @numbers;
print "@sorted_numbers";
Q1. What are the member variable names for the following package?
package person;
sub new { # constructor
my $class = shift;
my $self = {
name => shift,
address => shift,
};
bless $self, $class;
return $self;
}
Q2. The new
keyword can be used to create an object of a package?
Q3. What is the output of the following code?
package person;
sub new { # constructor
my $class = shift;
my $self = {
# member variables
name => shift,
address => shift,
};
print "Name is $self->{name}";
bless $self, $class;
return $self;
}
$object = new person("Alex", "Canada");
Answer:
package Triangle; # class name
sub new {
my $class = shift;
my $self = {
_length => shift,
_height => shift,
};
# Print all the values just for clarification.
print "Length is $self->{_length}\n";
print "Height is $self->{_height}\n";
bless $self, $class;
return $self;
}
sub area{
my ($self) = @_;
return ($self->{_length} * $self->{_height}) / 2;
}
1;
$object = new Triangle( 4, 5);
print "Area of Triangle: " . $object->area();
I hope this Learn Perl from Scratch Educative Quiz Answers would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>