InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
Tell me some facts about Perl memory management. |
|
Answer» As Perl programmers declare variables in their program, they TAKE some memory. It is up to the programmer to make sure that the program is utilizing memory in the best possible way. Programmers take care of memory leakage for EFFICIENT programming. Perl has an excellent garbage collector but with an exception. If a programmer uses global variables at all times, RATHER than local variables (where the garbage collector takes care of these variables as they go out of scope), they will end up using lots of EXTRA memory space. It is because the garbage collector will not remove the unused storage and hence the memory locations will remain OCCUPIED unless the computer restarts. |
|
| 2. |
What is the use of the next statement in Perl? Show with an example. |
|
Answer» The next STATEMENT is the same as that of the continue statement of C. It helps to SKIP the elements and move on to the next element of a SEQUENCE. @array = (a..z); print("@array \N"); for ($i = 0; $i < @array; $i++) { if ($i == 0 || $i == 4 || $i == 8 || $i == 14 || $i == 20) { next; } $array[$i] = "^"; } print("@array\n"); |
|
| 3. |
What are the base class and derived class in Perl? |
|
Answer» Programmers use both base class and derived class CONCEPTS while implementing inheritance. The base class is the parent class or super class in which various implementations reside. It helps in the creation of other CLASSES called the derived class. |
|
| 4. |
What are objects in Perl? |
|
Answer» A Perl object is SIMPLY a reference of the data TYPE that performs various OPERATIONS on behalf of that class. Perl stores the class object as a reference in a scalar variable. As we know, scalars can hold a reference to the object; PROGRAMMERS can use the same scalar to have different class OBJECTS. |
|
| 5. |
Write a Perl program to search your name from a string through regular expression. |
|
Answer» Here’s the program to search my NAME from a string through REGULAR expression. $strr = "My name is GAURAV and I'm here for a Perl interview"; if ($strr =~ /Gaurav/) { print " Your name is there in the string \n"; } else { print " Your name is not there in the string \n"; } |
|
| 6. |
What are the different regular expression operations available in Perl? |
|
Answer» Perl supports three different regular expression OPERATORS. These are:
|
|
| 7. |
What are regular expressions (RE)? |
|
Answer» Regular expressions are a specially encoded SEQUENCE of characters that helps PROGRAMMERS in defining a search pattern within the strings. They began to appear in the mid-1940s for describing regular languages. But gradually by the 1970s, they began to pop up in the programming world for searching and pattern-matching. There are numerous applications of regular expressions. Some practical day-to-day aspects are in data science, data extraction, data pre-processing, pattern matching, NATURAL language processing (NLP), web scraping, etc. e.g.: $s = "Gaurav Roy"; if ($s =~ m[Gaurav]) { PRINT "Match Found\n"; } else { print "Match Not Found\n"; } |
|
| 8. |
How will you create an infinite loop using Perl's for loop? Also, tell me how to terminate such loops? |
|
Answer» The syntax of the for loop in Perl is for(initialization; CONDITIONAL expression; increment or DECREMENT) { // code body that needs to be executed repetedly }But imagine a SITUATION where you have put double semicolon (;;) within the for loop. It means there is no beginning value or TERMINATING condition and hence it will execute for infinite times. To stop executing such infinitely running LOOPS, you can programmers can use the Ctrl + C shortcut. |
|
| 9. |
What are special variables? |
|
Answer» There are specific types of variables whose MEANINGS are predefined and have SPECIAL significance. They perform a specific function as and when required by the programmer. Most of these PERL special variables come with a long English-like naming convention. For example, Operating System Error variable $! which can be written as: $OS_ERROR. All the special variables available within Perl leverage punctuation CHARACTERS FOLLOWING the usual variable indicator such as at (@), dollar ($), or percent (%) sign, like $_. For example: foreach ('Karlos', 'Sue', 'Dee') { print($_); print(" \n "); } |
|
| 10. |
What is the role of warn function in the error handling of Perl? |
|
Answer» The warn function enables the PROGRAMMER to RAISE a WARNING MESSAGE that gets printed to STDERR but does not take further action. It is very USEFUL when Perl programmers want to print a warning message for the user plus proceed with the remaining operations − chdir('/home') or warn "Unable to change to this directory "; |
|
| 11. |
How will you differentiate between die and exit in Perl? |
|
Answer» In PERL, the die will PRINT a message to the ‘stderr’ before terminating the program. The exit will only terminate the program without showing anything. The 'die' in Perl is catchable using eval, whereas the 'exit' SIMPLY quits from the process. print "ENTER your rate:"; $rate = <STDIN>; if ($rate < 2000) { exit $rate; } else { print "\n Thanks for the investment! "; } use strict; use warnings; open(my $FH, '>', 'sssit/gauravkarlos/perlcode.txt') or die; print $fh "An example of error handling using die \n"; close $fh; |
|
| 12. |
What is Error Handling? What are the different types of errors in Perl? |
|
Answer» ERROR handling is the approach of taking appropriate actions against the program when it pops up with an error or is experiencing difficulty in executing the code. There are two types of errors in Perl:
Perl PROVIDES two built-in functions for generating fatal exceptions & warnings. These are: die() warn() |
|
| 13. |
How do you create a new directory in Perl? Mention the syntax of the function. |
|
Answer» For CREATING a new directory in PERL, we can use the function 'MKDIR'. PROGRAMMERS need to take the required permission for creating a directory. The syntax for the function is: mkdir(directoryName) or DIE; |
|
| 14. |
What are directories in Perl and how to access it using Perl? |
|
Answer» We all use different operating systems for our day-to-day tasks. Each of these operating systems COME with their own set of COMMANDS to look at the FILES list within a directory. For example, all Linux users use the 'li' command, whereas all Windows users use the 'DIR' command to explore the list of directories and sub-directories in their respective OS. Perl PROVIDES a universal way of accessing directories in Perl using Perl directory functions. Directory handling in Perl is similar to file handling. Here is a code snippet showing how to access a directory: my $directory1 = '/users/karlosray'; opendir (DIR, $directory1) or die "Unable to open the directory, $!"; while ($file = readdir DIR) { print "$file \n"; } closedir DIR;In this case the file does not exist and hence showed this output. |
|
| 15. |
Explain in simple words the context of a subroutine. |
|
Answer» We can DEFINE the context of a subroutine as the type of return value expected by the subroutine. Programmers can use one SINGLE function which can return different VALUES. |
|
| 16. |
Write a Perl program to open a file. |
|
Answer» Here’s a code snippet to open a FILE using Perl program: open(DATA, "<Filename.txt") or DIE "Unable to open the file Filename.txt, $! "; while(<DATA>) { print "$_"; } < or r: For read only Access > or w: For creating, Writing, and Truncating files >> or a: For writing, APPENDING, and creating files +< or r+: For reading and writing files +> or w+: For reading, writing, creating, and truncating files +>> or a+: For reading, writing, appending, and creating files |
|
| 17. |
What are the three basic file handles in Perl? |
|
Answer» The three basic file handles are in PERL are:
|
|
| 18. |
Why is Perl alias considered to be faster as compared to references? |
|
Answer» PROGRAMMERS have to dereference a REFERENCE variable USING the $, @, or % as the PREFIX of the reference variable. Aliases in Perl are faster COMPARED to references because they do not require any dereferencing. |
|
| 19. |
Show a simple Perl program to perform reference to functions. |
|
Answer» Perl is a wonderful language to WRITE simple programs in for various functions. LET’s LOOK at some examples of it: sub DisplayDetails { my (%details) = @_; foreach $i (%details) { print "Item : $i\n"; } } %details = ('Name' => 'Gaurav', 'age' => 28); # Creating a reference to the above function. $c_ref = \&DisplayDetails; # This is a function call that uses the reference. &$c_ref(%details); |
|
| 20. |
What are references in Perl? |
|
Answer» A reference in Perl is a scalar data type, which can hold the location of another value that might be ARRAYS, scalar, or Perl hashes. Since references are scalar in nature, programmers can use them ANYWHERE in replacement of a scalar. For creating a reference of any variable, subroutine, or value, we can PREFIX it with a BACKSLASH. $hashref = \%ENV; $arrayref = \@ARGV; $scalarref = \$FOO; $coderef = \&handler; |
|
| 21. |
Explain in simple words the use of a redo statement. |
|
Answer» The redo statement in Perl restarts the EXISTING loop without evaluating the control statement. Also, further statements within the BLOCK won't get executed. Program: $numb = 10; while($numb < 150){ if( $numb == 50 ){ $numb = $a + 10; redo; } print "Number = $numb \N"; } CONTINUE { $numb = $numb * 2; } |
|
| 22. |
What are escape sequence in Perl? |
|
Answer» ESCAPE sequences are special CHARACTERS that do not represent themselves when used as a part of STRING or character LITERAL. All these characters are preceded by a backslash. For example: ‘\n’, ‘\t’, ‘\\’, etc. Program: $result = "This is Mr. \" Karlos \""; print "$result \t"; print "\$result \n"; |
|
| 23. |
Define the goto statement in Perl? |
|
Answer» Perl's GOTO statement is a jumping statement, that MAKES a jump from one PART of the program to the other when the program encounters the goto label. It is sometimes referred to as the unconditional jump statement because the jumping from one section of the program to the other takes place without any condition. Programmers can use the goto statement anywhere WITHIN a function. The syntax is: LABEL_NAME: Statement 1; Statement 2; . . . . . Statement n; goto LABEL_NAME; Program: sub dispNumb() { my $numb = 1; lab: print "$numb "; $numb++; if ($numb <= 20) { goto lab; } } # Driver CODE dispNumb(); |
|
| 24. |
How do Perl programmers handle date and time operations within a Perl program? |
|
Answer» Perl is a feature-rich programming language that can easily handle dates and times within the program. Perl provides a PREDEFINED module called the DateTime module. This DateTime is a class that helps in REPRESENTING various combinations of dates and times. The DateTime module in Perl renders the New Style of the calendar by EXTENDING backward in time before its creation (in 1582). This is usually called the “proleptic Gregorian calendar”. In such a calendar, the first day of the calendar (which is also called the EPOCH) is the first day of year 1 and is believed to be the birth of Jesus Christ. The syntax is: use DateTime; Program: $datetime = localtime(); print "Local Time of the SYSTEM : $datetime\n"; |
|
| 25. |
Among the Terms and Lists (operators), which one has the highest precedence in Perl? |
||||||||||||||||||||||||||||||||||||||||||
|
Answer» In Perl, both Terms and lists have the highest precedence. Terms INCLUDE expressions in parenthesis, variables, etc. List OPERATORS ALSO render the same level of precedence as that of terms. Both these operators have strong left word precedence. The ENTIRE list of operator precedence in Perl looks like:
|
|||||||||||||||||||||||||||||||||||||||||||
| 26. |
How will you distinguish between my and local? |
|
Answer» VARIABLES DECLARED USING the 'my' keyword reside only within that block scope in which they get declared. Such variables do not get inherited, and the functions do not get VISIBILITY when called in that block. Again, when the variables defined as 'local' REMAIN visible in that block. They have visibility in functions when called from that particular block. |
|
| 27. |
Mention some situations where Perl-based applications are better than other programming languages. |
|
Answer» There are various benefits that Perl CATERS to its developers and stays ahead of other programming languages. The situations are:
APART from all these situations, Perl is free, plus it has a repository of pre-written codes with community support called CPAN (Comprehensive Perl Archive Network). The Comprehensive Perl Archive Network (CPAN) is a repository of over 250,000 software modules. Some of these are:
|
|
| 28. |
What are the two distinct functions that allow Perl developers to include a module file or a Perl module, and how do they differ? |
|
Answer» There are two different functions Perl DEVELOPERS can USE to include a Perl module.
The syntax is: use Module_name;In case you have a module file as “math.pm”, the code will be: use math;
In case you have a module file as “math.pm”, the code will be: require math.pm; |
|
| 29. |
What is the use of iterative statements in Perl? |
|
Answer» A loop statement allows a programmer to perform a repetitive task or EXECUTE a set of statements multiple times in iterations. It is essential when programmers need to execute a block of CODE several times. Iterative statements get EXECUTED sequentially. There are SPECIFIC ways to change the value so that at every iteration, it can show different values. There are five different TYPES of iterative statements in Perl. These are:
|
|
| 30. |
What is the syntax of if else statement in Perl? |
|
Answer» The SYNTAX of if else STATEMENT in Perl is: if(boolean_expression) { # statement(s) will execute if the given condition is TRUE } else { # statement(s) will execute if the given condition is false } |
|
| 31. |
What are conditional statements in Perl? What are its types? |
|
Answer» The conditional statement in Perl helps in making decisions. Based on that decision, a valid Perl statement or set of instructions gets executed. Through conditional STATEMENTS, a programmer checks a condition or set of conditions. Perl caters to 7 different TYPES of conditional statements. These are:
|
|
| 32. |
How will you define lexical variables? |
|
Answer» When a programmer creates a VARIABLE with the help of the 'my' operator and it is PRIVATE in nature, they are called the lexical variables. It stays alive from the spot it got declared till the end, where the block (pair of curly BRACES) FINISHES. Example: my $g; my @k; my %R; |
|
| 33. |
What are subroutines in Perl? |
|
Answer» Subroutines in PERL are named blocks containing a set of instructions meant for a specific operation. They accept ARGUMENTS and can return values operated within themselves. Perl programmers can divide their code into meaningful sub-units. The syntax is: sub subroutine_name { body of the subroutine } Program: # Function DEFINITION sub Hey { print "Hey, Karlos ! \n"; } # Function CALL Hey(); |
|
| 34. |
What is the use of the delete function in Perl? Mention its syntax. |
|
Answer» PROGRAMMERS use the delete function to REMOVE a hash element from the hash group. It eliminates both KEYS and their respective value elements from the hash. The SYNTAX of the delete function is: delete($hash_name{key_name}); |
|
| 35. |
How to merge two different arrays in Perl? |
|
Answer» The MERGED array function in Perl helps in merging two ARRAYS into a single array. It eliminates all the commas residing in between them. @arrayOne = ("GAURAV", "Karlos", "Ray", "is", "here"); @arrayTwo = ("He", "wrote", "this"); # merge operation performed on both the above given arrays @merged = (@arrayOne, @arrayTwo); PRINT "@merged \N"; |
|
| 36. |
Define the void context of Perl in a single line. |
|
Answer» The void context is a type of context that does not care about the return VALUE is. Also, it does not DEMAND a return value when APPLIED WITHIN the PROGRAM. |
|
| 37. |
What are the various Perl array functions? |
|
Answer» Perl array CONTAINS a set of specific functions associated with the Perl array. These functions help in adding or removing elements to or from an array. There are four different types of Perl array functions. These are:
|
|
| 38. |
Is it possible to load binary extension dynamically? |
|
Answer» YES, PERL programmers can load BINARY extensions dynamically. But, for that, your SYSTEM should support more functionality than what is currently AVAILABLE, so it can load it up and keeps running. But then, if the binary extension does not recommend it, programmers have to calculate & compile the extension manually. |
|
| 39. |
How will you define arrays in Perl? |
|
Answer» Arrays in Perl contain an ordered list of scalar VALUES. It has a PRECEDING at (@) sign. For accessing a single element from within the Perl array, developers use the dollar ($) sign. The SYNTAX for DECLARING arrays in Perl is: @array_Name = (1stElement, 2ndElement, 3rdElement.... nthElement); |
|
| 40. |
What are scalars in Perl? |
|
Answer» Scalars are variables that can contain only a single unit of data at a time. That data is said to REMAIN stored in scalar variables. Perl supports different types of scalar values. These are CHARACTER, string, NUMBERS, floating-point values, references, etc. The $ (dollar) sign is USED to CREATE scalars. |
|
| 41. |
What are Hashes in Perl? |
|
Answer» In Perl, hashes are a collection of KEY-value pairs having unordered forms. In this collection, the keys are unique STRINGS, and their corresponding VALUES are SCALAR elements. Hashes are preceded with a PERCENT (%) sign. They get indexed and accessed through their key values. |
|
| 42. |
What is the use of say() function in Perl? |
|
Answer» The say() function in Perl is LIKE the print() function which displays any string or variable passed within it as arguments. The only difference between say() and print() is that it automatically APPENDS a NEW line at the end of EVERY output string. |
|
| 43. |
What is CPAN in Perl? |
|
Answer» The acronym CPAN stands for Comprehensive Perl Archive Network. It is a Perl REPOSITORY that contains 250,000 Perl software DEVELOPMENT modules. Also, all of these come with accompanying documentation for 39,000 distributions, with around 12,000 contributors. Most of the CPAN modules are free and open source. It acts as an archive network or the Perl program or as a Package MANAGER for the automated software INSTALLER. |
|
| 44. |
Is Perl a case-sensitive programming language? |
|
Answer» YES. Like other programming languages like C, C++, Java, C#, Python, ETC., Perl is a case-sensitive programming LANGUAGE. |
|
| 45. |
Does Perl allow developers to use objects? Are they mandatory in a Perl program? |
|
Answer» Yes, PERL supports the concept of OBJECTS. But it is not compulsory to use objects. Perl allows using VARIOUS object-oriented CONCEPTS without using or understanding the objects. But in case the developer is creating a heavy application and the program is too large, then it is suitable for the PROGRAMMER to make it object-oriented. |
|
| 46. |
Which feature of Perl supports the reusability of codes? |
|
Answer» As we all know, Perl is an object-oriented programming language, so it supports the concept of inheritance. Inheritance is the concept that helps Perl programmers leverage code reusability. Inheritance SIMPLY means that methods, properties, and data members of a base/parent CLASS will REMAIN available and accessible to the derived/child classes. So, programmers do not have to WRITE the same code LOGIC repeatedly and simply inherit the properties and methods of the parent class. |
|
| 47. |
Mention some disadvantages of Perl. |
|
Answer» Every good thing COMES with some drawbacks. Some disadvantages of Perl PROGRAMMING are:
|
|
| 48. |
List some advantages of Perl. |
|
Answer» PERL is a general-purpose, high-level programming language. The TERM general-purpose MEANS developers can use this language in web DEVELOPMENT, text processing, desktop application development, network programming, etc. Apart from all these, there are several other advantages of Perl programming. These are:
|
|
| 49. |
What is Perl Programming? |
|
Answer» Perl is a high-level, interpreted, general-purpose programming language created by Larry Wall. Initially, the language was called "Practical Extraction and Reporting Language", and this was later MADE into the acronym – Perl. This language borrows its FEATURES from other LANGUAGES, including C, Shell Script (sh), and AWK. It was initially developed as general-purpose programming lannguage for text manipulation. Nowadays, developers use it for web APPLICATION development, network programming, system administration tool development, GUI app development, annd more. |
|