Intermediate Perl test (Intermediate)
30 questions · 35 minutes · Free
Regular expressions, references, subroutines, modules and CPAN.
Take the Intermediate Perl test
What it covers
- Perl regular expressions
- Perl references
- Perl subroutines
- Perl modules and CPAN
- Perl one-liners
Interview format
- Questions: 30 questions — 18 multiple choice, 9 multi-answer, 3 predict-the-output
- Time: 35 minutes, timed from the first question
- Difficulty: Intermediate
- Skill area: Backend
- Price: Free
Earn a certificate
The Intermediate Perl test is one of the 8 tests of the VLSI Design & Verification learning path. Pass it and the other 7 tests at 70% or more and readyforhires issues you a verifiable certificate in your name, with its own code and verify link to share or add to LinkedIn.
Sample questions
5 sample questions from the Intermediate Perl test, with the answers. Samples are taken out of the live test, so you will not meet these when you sit it.
Which of the following statements is correct regarding modifying arguments in Perl subroutines?
- Modifying $_[0] directly inside the subroutine does not change the original variable passed by the caller, whereas assigning to $x inside the subroutine does affect the original variable.
- Modifying $_[0] directly inside the subroutine will change the original variable passed by the caller, whereas assigning to $x inside the subroutine does not affect the original variable.
- Modifying $_[0] directly inside the subroutine and assigning to $x inside the subroutine both change the original variable passed by the caller.
- Modifying $_[0] directly inside the subroutine and assigning to $x inside the subroutine both do not change the original variable passed by the caller.
Answer: B. Modifying $_[0] directly inside the subroutine will change the original variable passed by the caller, whereas assigning to $x inside the subroutine does not affect the original variable.
In Perl, the @_ array contains aliases to the caller's variables. Modifying $_[0] directly inside the subroutine changes the original variable passed by the caller. However, if you assign $_[0] to a local variable like $x inside the subroutine and then modify $x, it does not affect the original variable because $x is a copy of $_[0]. This behavior highlights the difference between directly modifying the alias and creating a new variable.
How can default arguments be implemented in Perl subroutines?
- Perl uses the 'arguments' keyword to define default values for subroutine parameters.
- Perl supports default arguments in subroutines using the 'default' keyword.
- Perl does not support default arguments in subroutines natively, but they can be simulated using the '||' operator or the 'defined' function within the subroutine.
- Perl automatically assigns default values to subroutine parameters if they are not provided.
Answer: C. Perl does not support default arguments in subroutines natively, but they can be simulated using the '||' operator or the 'defined' function within the subroutine.
Perl does not have built-in support for default arguments in subroutines. However, developers can simulate default arguments by checking the values of the parameters within the subroutine using logical operators like '||' or the 'defined' function. This allows the subroutine to use a default value if the parameter is not provided or is undefined.
Consider the following code: my $text = 'The year is 2023'; if ($text =~ /(?<year>\d{4})/) { print $+{year}; } What will be the output?
- The value of $+{year} will be '2023'
- The value of $+{year} will be '2023' if the regex is modified with the 'g' modifier
- The value of $+{year} will be '2023' only if the regex is used in a list context
- The value of $+{year} will be '2023' if the regex is enclosed in a qr// operator
Answer: A. The value of $+{year} will be '2023'
Named capture groups in Perl regular expressions allow you to access captured values using the $+{name} syntax. In the given example, the regex pattern '(?<year>\
A Perl programmer is trying to match a non-digit character that is immediately preceded by a digit using a regular expression. However, the current pattern `/\D(?=\d)/` is not working correctly. Which of the following modifications to the regex pattern would fix the issue and correctly use a lookbehind assertion?
- The regex pattern should be modified to use `(?<=\d)\D` to match a non-digit character that is immediately preceded by a digit.
- The regex pattern should be modified to use `(?<=\D)\d` to match a digit character that is immediately preceded by a non-digit character.
- The regex pattern should be modified to use `(?<=\d)\d` to match a digit character that is immediately preceded by another digit.
- The regex pattern should be modified to use `(?<=\D)\D` to match a non-digit character that is immediately preceded by another non-digit character.
Answer: A. The regex pattern should be modified to use `(?<=\d)\D` to match a non-digit character that is immediately preceded by a digit.
The original regex pattern `/\D(?=\d)/` uses a lookahead assertion to match a non-digit character that is immediately followed by a digit. However, the question focuses on lookbehind assertions, which are used to match a pattern that occurs before the current position. To fix this, the pattern should use a lookbehind assertion, such as `(?<=\d)\D`, to match a non-digit character that is immediately preceded by a digit. This ensures that the lookbehind assertion is correctly applied.
What will happen if an array is passed to a subroutine as a reference, and the subroutine modifies the array?
- The subroutine will receive a copy of the array, and modifying the array inside the subroutine will not affect the original array.
- The subroutine will receive a reference to the array, and modifying the array inside the subroutine will affect the original array.
- The subroutine will receive the array as a list, and modifying the array inside the subroutine will affect the original array.
- The subroutine will receive a reference to the array, but modifying the array inside the subroutine will not affect the original array.
Answer: B. The subroutine will receive a reference to the array, and modifying the array inside the subroutine will affect the original array.
When an array reference is passed to a subroutine, the subroutine receives a scalar containing the reference to the array. Any modifications made to the array inside the subroutine will affect the original array because the reference points to the same memory location. This is in contrast to passing the array directly, which would create a copy.
Frequently asked questions
- How many questions are in the Intermediate Perl test?
- 30 questions (18 multiple choice, 9 multi-answer, 3 predict-the-output), with 35 minutes to answer them.
- What does the Intermediate Perl test cover?
- Perl regular expressions, Perl references, Perl subroutines, Perl modules and CPAN, Perl one-liners.
- Is the Intermediate Perl test free?
- Yes. It is free to take with a readyforhires account.
- What level is the Intermediate Perl test?
- Intermediate, in Backend.