PERL Fundamentals: Variables and Data Structures
Covers PERL basics including scalars, arrays, hashes, and their operations
Questions
Which of the following piece of code print the text covered by double quotes.
For Example: "Sun rises in the East"
- print Sun rises in the East
- print 'Sun rises in the East'
- print ' "Sun rises in the East" '
- print " 'Sun rises in the East ' "
- display "Sun rises in the East"
Match the following:
| A. Scalar | I. $ |
| B. Array | II. % |
| C. Associated Array | III. @ |
| IV. * |
- A - II, B - IV, C - I
- A - I, B - III, C - II
- A - II, B - IV, C - III
- A - I, B - II, C - III
- No prefix is required for the variable declaration in PERL.
Choose the wrong one from the following SCALAR variable declarations
- $First_Name = "Kumar"
- $Salary = 1234.56
- $My.Country =" INDIA"
- $Player = Sachin
- $Country_code = 91
Find the correct syntax from the following
- @ages = (35, 56, 29); @names = ("Pavan", "Chakri", "Kumar"); print "$ages[0] = ages[2]n"; It displays $ages[0] = 29
- @array = qw/This is also an array/; print @array; print $array[2]; It displays is
- @Months = qw/Jan Feb Mar Apr May Jun/ ; print "$Months[-6]n"; It gives compilation error
- @My_array = (ab .. ag)
print @My_array displays
ab ac ad ae af ag - None of the above
Which of the following syntax will return the size of the array @My_array
@My_array = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
- @My_array
- print @My_array
- print $My_array
- $size = @My_array; print $size;
- $size1 = $#My_array; print $size1;
Find the wrong array declaration from the following.
- @array1 = (5, 10, 15, 20, 25.45);
- @array1 = (5, 10, 15, 20, 25);
@array2 = (1, 2, 3, 4, @array1); - @My_array = 10
- @My_array = (Delhi, Calcutta, Mumbai, Bangalore, 5, 10, 15, 20)
- @array1 = 1;
@array2 = 10;
@My_array = (@array1 .. @array2);
@alpha = (a..z);
print "Before splicing - @alphan";
splice (@alpha, 5, 3, "af".."ah");
print "After splicing - @alphan";
This code produces the following result
Before splicing - a b c d e f g h i j k l m n o p q r s t u v w x y z
After splicing - a b c d e af ag ah i j k l m n o p q r s t u v w x y z
Which of the following statement is correct about the above piece of code. (Multiple answers mighe be correct)
- splice( ) function has the first argument as the array name.
- splice( ) function has the second argument as the OFFSET. Starting position in the array. Here the offset is 5
- splice() function has the third argument as the LENGTH. The function replaces the elements starting from the OFFSET and count the number of elements mentioned in the LENGTH parameter. Here 3 is the length. So the elements f g h are picked for replacement
- splice() function has the fourth argument as the LIST of elements to replace with. The function replaces the elements starting from the OFFSET and count the number of elements mentioned in the LENGTH parameter with the LIST.
- All the above are correct.
Find the wrong Array declaration
- @My_array = (2, 4, 6, 8, 10)
- @My_array = (1, 5, 8.9, 45.90)
- @My_array = (1 to 10)
- @My_array = (1 .. 10)
- @My_array = (100, 1 .. 10)
Consider @Countries = ("India", "Denmark", "Russia", "Germany", "Japan", "Ireland")
Which of the following statement is correct about the Array operations? (Multiple answers might be correct)
- shift(@Countries) will remove country "India" from the beginning of the array @Countries
Now @Countries looks like
@Countries = ("Denmark", "Russia", "Germany", "Japan", "Ireland") - shift(@Countries) will remove country "Ireland" from the end of the array @Countries
Now @Countries looks like
@Countries = ("India", "Denmark", "Russia", "Germany", "Japan") - unshift(@Countries, "Canada") will add country "Canada" at the end of the array @Countries
Now @Countries looks like
@Countries = ("India", "Denmark", "Russia", "Germany", "Japan", "Canada") - unshift(@Countries, "Canada") will add country "Canada" at the front of the array @Countries.
Now @Countries looks like
@Countries = ("Canada", "India", "Denmark", "Russia", "Germany", "Japan") - None of the above are correct
$TCS_Office = "TATA CONSULTANCY SERVICES LIMITED, MADHAPUR".
Which of the following piece of code will convert the above string into array of elements and assign to @TCS_Off_Addr
@TCS_Off_Addr = ("TATA", "CONSULTANCY", "SERVICES", "LIMITED,", "MADHAPUR");
- @TCS_Off_Addr = split(' ', $TCS_Office);
- @TCS_Off_Addr = split / /, $TCS_Office;
- @TCS_Off_Addr = split($TCS_Office, ' ');
- Both Option 1 and Option 2 are correct
- All options 1, 2 and 3 are correct.
Which of the following does not print the size of the array @My_array = (1..10);
- print scalar @My_array
- $my_arr_size = @My_array print $my_arr_size
- print $#My_array
- print 0+@My_array
- print $#My_array + 1
Pick the wrong piece of code regarding the transformation of text into array.
- $str = "PERL is one of the popular programing languages"; @words = split(' ', $str);
- $str = "PERL is one of the popular programing languages"; @words = split('', $str);
- $str = "PERL is one of the popular programing languages"; @words = split(' ', $str, 3);
- Option 1 and Option 3 are correct
- All 3 codes are correct and gives different results
Consider the Array @Countries = ("India", "Denmark", "Russia", "Germany", "Japan", "Ireland")
Which of the following statement is correct about the Array operations?
- push(@Countries, "Canada") will add new country "Canada" at the beginning of the array @Countries
Now @Country gives
@Countries = ("Canada", "India", "Denmark", "Russia", "Germany", "Japan", "Ireland") - push(@Countries, "Canada") will add new country "Canada" at the end of the array @Countries
Now @Countries gives
@Countries = ("India", "Denmark", "Russia", "Germany", "Japan", "Ireland", "Canada") - pop(@Countries, "India") will remove country "India" from the array @Countries
Now @Countries looks like
@Countries = ("Denmark", "Russia", "Germany", "Japan", "Ireland") - pop(@Countries, "Ireland") will remove the country "Ireland" from the array @Countries
Now @Countries looks like
@Countries = ("India", "Denmark", "Russia", "Germany", "Japan") - pop(@Countries) will remove the country "Ireland" last element of the array @Countries
Now @Countries looks like
@Countries = ("India", "Denmark", "Russia", "Germany", "Japan")
Which of the following is correct about Hashes in PERL ?
- Hash is a set of Key Value pair and denoted by a symbol %
- %Scores_Of = ('Sachin', 90, 'Dravid', 75, 'Sehwag', 51, 'Kumble', 8);
- %Scores_Of = ('Sachin' => 90, 'Dravid' => 75, 'Sehwag' => 51, 'Kumble' => 8);
- %Scores_Of = (-Sachin => 90, -Dravid => 75, -Sehwag => 51, -Kumble => 8);
- All the above are correct
Pick wrong piece of code for printing the Hashes in PERL
Consider the hash %My_Portfolio = ('DFHL', 100, 'ITC', 250, 'ASHOKLEY', 500, 'AXISBANK', 150);
- print "$_n" for keys %My_Portfolio;
- print scalar keys %My_Portfolio;
- print "%My_Portfolio";
- @Stocks = keys %My_Portfolio; print "$Stocks[1]n";
- @Quantity = values %My_Portfolio; print "$Quantity[2]n";