Learn Perl from Scratch Educative Quiz Answers

Get Learn Perl from Scratch Educative Quiz Answers

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.

Enroll on Educative

Quiz 1: Quiz Yourself on Comments and Printing Texts.

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?

  • Option 1
cout << "Hello World" << endl;
  • Option 2
echo "Hello World";
  • Option 3
print "Hello World";
  • None of the above

Q3. What is the error in the code below?

   print "This is a print statement"
  • # symbol is missing at the start of the code statement
  • Paranthesis are missing in the statement
  • Semicolon is missing at the end of the code statement

Q4. You can also use printf function instead of print function in Perl.

  • True
  • False

Challenge 1: Printing Text

Answer:

print "My first Perl challenge!";

Quiz 2: Quick Quiz on Variables

Q1. Which one is not a data type in Perl?

  • integer
  • float
  • Boolean
  • Null

Q2. Which is not a floating point type?

  • 1.234
  • 1920202.4443
  • 12.01
  • 13

Q3. Perl variables need to be declared before adding a value to it.

  • True
  • False

Q4. What is the data type of the value '5'?

  • int
  • char
  • boolean
  • double

Q5. Which symbol is used to create an array?

  • #
  • &
  • @
  • None of the above

Challenge 2: Make Variables

Answer:

$intNumber = 1000;
$charName = 'N';
$boolAccept = 1;
$floatNum = 10.292;

print $intNumber . ", ";
print $charName . ", ";
print $boolAccept . ", ";
print $floatNum;

Quiz 3: Quiz on Operators

Q1. What is the output of the following code?

$myvar = 5;
print $myvar++;
  • 6
  • 5
  • 7
  • None of the above

Q2. What is the output of the following code?

$aa = 5;
$bb = 3;
print ($aa**$bb);
  • 15
  • 243
  • 125
  • 75

Q3. What is the ouput of the following expression?

print "42 + 7 % 2 = ". (42 + 7 % 2) ;
  • 49
  • 43
  • 24
  • None of the above

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;
}
  • 0
  • 1
  • -1

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");
}
  • inside if
  • inside else
  • None of the above

Q6. What will the following program print?

$aa = 2;
$bb = 3;
$bb = ($aa += ($bb **= 5)); 
print "\$bb = ".$bb;
  • $bb = 3
  • $bb = 243;
  • $bb = 245

Quiz 4: Quiz on Conditional Statements

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++;
}
  • $x=3, $y=4, $z=6
  • $x=4, $y=4, $z=6
  • $x=3, $y=4, $z=7
  • none of the above

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--;
}
  • $x=2, $y=1, $z=3
  • $x=3, $y=2, $z=3
  • $x=3, $y=1, $z=4
  • none of the above

Q3. Ternary operator has one boolean expression, and returns one of two values depending on the value of a Boolean expression.

  • True
  • False

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;
  • 0
  • 20
  • 30
  • None of the above

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;
  }
  • $x=2, $y=2, $z=2
  • $x=1, $y=2, $z=4
  • $x=2, $y=5, $z=3
  • none of the above

Challenge 3: Even or Odd

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;
}

Challenge 4: Making a Calculator

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";
    }
}

Quiz 5: Quiz on Loops

Q1. Which of the loops is not a pretest loop?

  • for loop
  • while loop
  • do while loop
  • until loop
  • All of the above

Q2. Which of the following is not a nested loop?

  • Option 1
for($i=0; $i<10; $i++){
        for($j=1; $j<i+2; $j++){
        }
        }
  • Option 2
for($i=0; $i<10; $i++){
    print "i= $i";
}
for($j=1; $j<i+2; $j++){ 
        print "j= $j";
}
  • Option 3
for($i=0; $i<10; $i++){
        while($j%2!=0)
        { print"$j";
            $j++;}
}
  • Option 4
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?

  • Testing condition is checked before each iteration
  • The while loop statements must terminate with a semi-colon
  • The for loop can be duplicated using a while loop
  • The while loop is a post test loop

Q4. Foreach loops do not keep track of index.

  • True
  • False

Q5. In the code below, when will the line $i = $i + 1; execute?

while ( $i <= 10){
    $i = $i + 1;
}
  • When i is greater than ten
  • When i is equal to ten
  • When i is less than equal to ten
  • Executes randomly for some values of i

Q6. The until loop:

  • Runs as long as the condition specified is true
  • Runs as long as the condition specified is false

Challenge 5: Multiplication Table of a Number

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;

Challenge 6: Fibonacci Sequence

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;

Challenge 7: Printing Pyramid

Answer:

for($i = 1; $i <= $rows; $i++) {
  for($j = 1; $j <= $i; $j++)
  {
    print "a ";
  }
  print "\n";
}

Quiz 6: Quiz on Subroutines

Q1. A subroutine needs a subroutine definition to perform a specific task.

  • True
  • False

Q2. A local variable declared in a subroutine is usable outside that subroutine.

  • True
  • False

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);
  • 4
  • 6
  • 16
  • Run time error

Q4. What is the correct way to create a subroutine in Perl?

  • create mySubroutine()
  • make mySubroutine{ }
  • sub mySubroutine { }
  • None of the above

Challenge 8: Sum of Two Numbers

Answer:

sub sum {
   return @_[0] + @_[1];
}

Challenge 9: Letter Grade to GPA

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;}
  }

Quiz 7: Quiz on Strings

Q1. Single characters can be extracted using array (square brace) syntax by using split function.

  • True
  • False

Q2. What will be the output of the following code?

print index("machinelearning", "learning");
  • 0
  • 14
  • 7
  • None of the above

Q3. What is the output of the following code?

$str = "This is a string.";
print length($str);
  • 14
  • 17
  • 19
  • None of the above

Q4. What will the following code print?

$var1 = "Myname";
$var1 .= "is";
$var1 .=  "James";
print $var1;
  • My name is James
  • Myname is James
  • MynameisJames
  • Myname isJames

Q5. What is the output of the following code?

$str = "Learning is fun!";
print substr($str, 12, 4);
  • is f
  • fun!
  • fun
  • None of the above

Challenge 10: Check Substring

Answer:

sub stringCheck{
$str1 = @_[0];
$str2 = @_[1];

  if(index($str1, $str2) >= 0) {
    return 1;
  }
  else {
    return -1;
  }
}

Challenge 11: Find the Maximum Value

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()

Challenge 12: Print a Matrix

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";
  }
}

Quiz 8:

Q1. Size of arrays in Perl

  • is fixed and needs to be defined
  • can change dynamically when need be

Q2. In Perl arrays, the keys can be

  • strings and integers
  • only strings
  • only integers

Q3. An array $cars is defined as follows

@cars = ("honda","toyota","mercedes", "BMW")

Which of the following line will print “mercedes” only

  • Option 1
print $cars[3];
  • Option 2
print $cars[2];
  • Option 3
print $cars["mercedes"];

Quiz 9:

Q1. What does a shift() function do?

  • Deletes value from the front of an array.
  • Deletes value from the end of an array.

Q2. What does a push function do?

  • Adds passed key at the front of an array.
  • Adds passed value at the end of an array.

Q3. What will be the output of the following code:

  @arr = (2,4,5,9);
    unshift(@arr,14,15);
    print "@arr";
  • 15 14 2 4 5 9
  • 14 15 2 4 5 9
  • 2 4 5 9 14 15

Q4. What will be the output of the following code:

  @arr=(2,4,5,9);
    push(@arr,14,15);
    print "@arr";
  • 15 14 2 4 5 9
  • 2 4 5 9 15 14
  • 2 4 5 9 14 15
  • 14 15 2 4 5 9

Q5. What will be the output of the following code:

@arr=(1,3,5,7,9,11);
shift(@arr);
print "@arr";
  • 1 3 5 9 11
  • 1 3 5 7 9
  • 3 5 7 9 11

Q6. What will be the output of the following code:

@arr=(1,3,5,7,9,11);
pop(@arr);
print "@arr";
  • 3 5 7 9 11
  • 1 3 5 7 9
  • 1 3 5 9 11

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";
  • -22 0 1 4 20 -44
  • -44 -22 0 1 4 20
  • 20 4 1 0 -22 -44
  • None

Quiz 10: Quiz on Packages

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;
}
  • name and $self
  • bless and $class
  • name and address
  • None of the above

Q2. The new keyword can be used to create an object of a package?

  • True
  • False

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");
  • Alex
  • Name is Alex
  • Alex Canada

Challenge 13: Calculating Area

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();
Conclusion:

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 >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.