Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Perl Interview Questions and Answers

by Venkatesan M, on May 13, 2017 3:03:45 PM

Perl Interview Questions and Answers

Q1. Difference between the variables in which chomp function work ?

Ans:
Scalar: It is denoted by $ symbol. Variable can be a number or a string.
Array: Denoted by @ symbol prefix. Arrays are indexed by numbers.
The namespace for these types of variables is different. For Example: @add, $add. The scalar variables are in one table of names or namespace and it can hold single specific information at a time and array variables are in another table of names or namespace. Scalar variables can be either a number or a string

Q2. Create a function that is only available inside the scope where it is defined ?

Ans:

$pvt = Calculation(5,5);
print("Result = $pvt\n");
sub Calculation{
my ($fstVar, $secndVar) = @_;
my $square = sub{
return($_[0] ** 2);
};
return(&$square($fstVar) + &$square($secndVar));
};
Output: Result = 50

Q3. Which feature of Perl provides code reusability ? Give any example of that feature.

Ans:

Inheritance feature of Perl provides code reusability. In inheritance, the child class can use the methods and property of parent class
Package Parent;
Sub foo
{
print("Inside A::foo\n");
}
package Child;
@ISA = (Parent);
package main;
Child->foo();
Child->bar();

Q4. In Perl we can show the warnings using some options in order to reduce or avoid the errors. What are that options?

Ans:

The -w Command-line option: It will display the list if warning messages regarding the code.
strict pragma: It forces the user to declare all variables before they can be used using the my() function.
Using the built-in debugger: It allows the user to scroll through the entire program line by line.

Q5. Write the program to process a list of numbers.

Ans:

The following program would ask the user to enter numbers when executed and the average of the numbers is shown as the output:
$sum = 0;
$count = 0;
print "Enter number: ";
$num = <>;
chomp($num);
while ($num >= 0)
{
$count++;
$sum += $num;
print "Enter another number: ";
$num = <>;
chomp($num);
}
print "$count numbers were entered\n";
if ($count > 0)
{
print "The average is ",$sum/$count,"\n";
}
exit(0);

Q6. Does Perl have objects? If yes, then does it force you to use objects? If no, then why?

Ans: Yes, Perl has objects and it doesn’t force you to use objects. Many object oriented modules can be used without understanding objects. But if the program is too large then it is efficient for the programmer to make it object oriented.

Q7. Can we load binary extension dynamically?

Ans: Yes, we can load binary extension dynamically but your system supports that. If it doesn’t support, then you can statically compile the extension.

Q8. Write a program to concatenate the $firststring and $secondstring and result of these strings should be separated by a single space.

Ans:

Syntax:
$result = $firststring . ” “.$secondstring;
Program:
#!/usr/bin/perl
$firststring = "abcd";
$secondstring = "efgh";
$combine = "$firststring $secondstring";
print "$Combine\n";
Output:
abcd efgh

Q9. How do I replace every TAB character in a file with a comma?

Ans: perl -pi.bak -e 's/\t/,/g' myfile.txt

Q10. In Perl, there are some arguments that are used frequently. What are that arguments and what do they mean?

Ans:

-w (argument shows warning)
-d (use for debug)
-c (which compile only not run)
-e (which executes)
We can also use combination of these like:
-wd

Q11. How many types of primary data structures in Perl and what do they mean?

Ans: The scalar: It can hold one specific piece of information at a time (string, integer, or reference). It starts with dollar $ sign followed by the Perl identifier and Perl identifier can contain alphanumeric and underscores. It is not allowed to start with a digit. Arrays are simply a list of scalar variables.

Arrays: Arrays begin with @ sign. Example of array:
my @arrayvar = (“string a”, “string b “string c”);

Associative arrays: It also frequently called hashes, are the third major data type in Perl after scalars and arrays. Hashes are named as such because they work very similarly to a common data structure that programmers use in other languages–hash tables. However, hashes in Perl are actually a direct language supported data type.

Q12. Which functions in Perl allows you to include a module file or a module and what is the difference between them?
“use”

Ans:

  1. The method is used only for the modules (only to include .pm type file)
  2. The included objects are verified at the time of compilation.
  3. We don’t need to specify the file extension.
  4. loads the module at compile time.

“require”

  1. The method is used for both libraries and modules.
  2. The included objects are verified at the run time.
  3. We need to specify the file Extension.
  4. Loads at run-time.

suppose we have a module file as “Module.pm”
use Module;
or
require “Module.pm”;
(will do the same)

Q13. How can you define “my” variables scope in Perl and how it is different from “local” variable scope?

Ans:

$test = 2.3456;
{
my $test = 3;
print "In block, $test = $test ";
print "In block, $:: test = $:: test ";
}
print "Outside the block, $test = $test ";
print "Outside the block, $:: test = $::test ";
Output:
In block, $test = 3
In block, $::test = 2.3456
Outside the block, $test = 2.3456
Outside the block, $::test = 2.3456
The scope of “my” variable visibility is in the block only but if we declare one variable local then we can access that from the outside of the block also. ‘my’ creates a new variable, ‘local’ temporarily amends the value of a variable.

Q14. Which guidelines by Perl modules must be followed?

Ans: Below are guidelines and are not mandatory
The name of the package should always begin with a capital letter.
The entire file name should have the extension “.pm”.
In case no object oriented technique is used the package should be derived from the Exporter class.
Also if no object oriented techniques are used the module should export its functions and variables to the main namespace using the @EXPORT and @EXPOR_OK arrays (the use directive is used to load the modules).

 

Q15. How the interpreter is used in Perl?

Ans: Every Perl program must be passed through the Perl interpreter in order to execute. The first line in many Perl programs is something like:
#!/usr/bin/perl
The interpreter compiles the program internally into a parse tree. Any words, spaces, or marks after a pound symbol will be ignored by the program interpreter. After converting into parse tree, interpreter executes it immediately. Perl is commonly known as an interpreted language, is not strictly true. Since the interpreter actually does convert the program into byte code before executing it, it is sometimes called an interpreter/compiler. Although the compiled form is not stored as a file.

Q16. “The methods defined in the parent class will always override the methods defined in the base class”. What does this statement means?

Ans: The above statement is a concept of Polymorphism in Perl. To clarify the statement, let’s take an example:
[perl]
package X;
sub foo
{
print("Inside X::foo\n");
}
package Z;
@ISA = (X);
sub foo
{
print("Inside Z::foo\n");

}

package main;

Z->foo();
[/perl]
This program displays:

Inside Z::foo
– In the above example, the foo() method defined in class Z class overrides the inheritance from class X. Polymorphism is mainly used to add or extend the functionality of an existing class without reprogramming the whole class.

Q17. For a situation in programming, how can you determine that Perl is a suitable?

Ans: If you need faster execution the Perl will provide you that requirement. There a lot of flexibility in programming if you want to develop a web based application. We do not need to buy the license for Perl because it is free. We can use CPAN (Comprehensive Perl Archive Network), which is one of the largest repositories of free code in the world.

Q18. Write syntax to add two arrays together in perl? 

Ans: @arrayvar = (@array1,@array2);
To accomplish the same, we can also use the push function.

Q19. How many types of operators are used in the Perl?

Ans:

Arithmetic operators
+, – ,*
Assignment operators:
+= , -+, *=
Increment/ decrement operators:
++, —
String concatenation:
‘.’ operator
comparison operators:
==, !=, >, < , >=
Logical operators:
&&, ||, !

Q20. If you want to empty an array then how would you do that?

Ans: We can empty an array by setting its length to any –ve number, generally -1 and by assigning null list
use strict;
use warnings;
my @checkarray;
if (@checkarray)
{
print "Array is not empty";
}
else
{
print "Array is empty";
}

Q21. Where the command line arguments are stored and if you want to read command-line arguments with Perl, how would you do that?

Ans: The command line arguments in Perl are stored in an array @ARGV.
$ARGV[0] (the first argument)
$ARGV[1] (the second argument) and so on.
$#ARGV is the subscript of the last element of the @ARGV array, so the number of arguments on the command line is $#ARGV + 1

Q22. Suppose an array contains @arraycontent=(‘ab’, ‘cd’, ‘ef’, ‘gh’). How to print all the contents of the given array?

Ans:

@arraycontent=(‘ab’, ‘cd’, ‘ef’, ‘gh’)

foreach (@arraycontent)
{
print "$_\n";
}

Q23. What is the use of -w, -t and strict in Perl?

Ans: When we use –w, it gives warnings about the possible interpretation errors in the script.
Strict tells Perl to force checks on the definition and usage of variables. This can be invoked using the use strict command. If there are any unsafe or ambiguous commands in the script, this pragma stops the execution of the script instead of just giving warnings.
When used –t, it switches on taint checking. It forces Perl to check the origin of variables where outside variables cannot be used in sub shell executions and system calls.

Q24. Write a program to download the contents from www.perlinterview.com/answers.php website in Perl.

Ans:

#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
my $siteurl = 'www.perlinterview.com/answers.php';
my $savefile = 'content.kml';
getstore($siteurl, $savefile);

Q25. Which has the highest precedence, List or Terms? Explain?

Ans: Terms have the highest precedence in Perl. Terms include variables, quotes, expressions in parenthesis etc. List operators have the same level of precedence as terms. Specifically, these operators have very strong left word precedence.

Topics:Perl Interview Questions and AnswersInformation Technologies (IT)

Comments

Subscribe

Top Courses in Python

Top Courses in Python

We help you to choose the right Python career Path at myTectra. Here are the top courses in Python one can select. Learn More →

aathirai cut mango pickle

More...