PERL Fundamentals: Variables and Data Structures

Covers PERL basics including scalars, arrays, hashes, and their operations

15 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

Which of the following piece of code print the text covered by double quotes.

For Example: "Sun rises in the East"

  1. print Sun rises in the East
  2. print 'Sun rises in the East'
  3. print ' "Sun rises in the East" '
  4. print " 'Sun rises in the East ' "
  5. display "Sun rises in the East"
Question 2 Multiple Choice (Single Answer)

Match the following:

 
A. Scalar I. $
B. Array II. %
C. Associated Array III. @
IV. *
  1. A - II, B - IV, C - I
  2. A - I, B - III, C - II
  3. A - II, B - IV, C - III
  4. A - I, B - II, C - III
  5. No prefix is required for the variable declaration in PERL.
Question 3 Multiple Choice (Single Answer)

Choose the wrong one from the following SCALAR variable declarations

  1. $First_Name = "Kumar"
  2. $Salary = 1234.56
  3. $My.Country =" INDIA"
  4. $Player = Sachin
  5. $Country_code = 91
Question 4 Multiple Choice (Single Answer)

Find the correct syntax from the following

  1. @ages = (35, 56, 29); @names = ("Pavan", "Chakri", "Kumar"); print "$ages[0] = ages[2]n"; It displays $ages[0] = 29
  2. @array = qw/This is also an array/; print @array; print $array[2]; It displays is
  3. @Months = qw/Jan Feb Mar Apr May Jun/ ; print "$Months[-6]n"; It gives compilation error
  4. @My_array = (ab .. ag)

    print @My_array displays

    ab ac ad ae af ag
  5. None of the above
Question 5 Multiple Choice (Single Answer)

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

  1. @My_array
  2. print @My_array
  3. print $My_array
  4. $size = @My_array; print $size;
  5. $size1 = $#My_array; print $size1;
Question 6 Multiple Choice (Single Answer)

Find the wrong array declaration from the following.

  1. @array1 = (5, 10, 15, 20, 25.45);
  2. @array1 = (5, 10, 15, 20, 25);
    @array2 = (1, 2, 3, 4, @array1);
  3. @My_array = 10
  4. @My_array = (Delhi, Calcutta, Mumbai, Bangalore, 5, 10, 15, 20)
  5. @array1 = 1;
    @array2 = 10;
    @My_array = (@array1 .. @array2);
Question 7 Multiple Choice (Single Answer)

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

  1. splice( ) function has the first argument as the array name.
  2. splice( ) function has the second argument as the OFFSET. Starting position in the array. Here the offset is 5
  3. 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
  4. 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.
  5. All the above are correct.
Question 8 Multiple Choice (Single Answer)

Find the wrong Array declaration

  1. @My_array = (2, 4, 6, 8, 10)
  2. @My_array = (1, 5, 8.9, 45.90)
  3. @My_array = (1 to 10)
  4. @My_array = (1 .. 10)
  5. @My_array = (100, 1 .. 10)
Question 9 Multiple Choice (Multiple Answers)

Consider @Countries = ("India", "Denmark", "Russia", "Germany", "Japan", "Ireland")

Which of the following statement is correct about the Array operations? (Multiple answers might be correct)

  1. shift(@Countries) will remove country "India" from the beginning of the array @Countries

    Now @Countries looks like
    @Countries = ("Denmark", "Russia", "Germany", "Japan", "Ireland")
  2. shift(@Countries) will remove country "Ireland" from the end of the array @Countries

    Now @Countries looks like
    @Countries = ("India", "Denmark", "Russia", "Germany", "Japan")
  3. 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")
  4. 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")
  5. None of the above are correct
Question 10 Multiple Choice (Single Answer)

$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");

  1. @TCS_Off_Addr = split(' ', $TCS_Office);
  2. @TCS_Off_Addr = split / /, $TCS_Office;
  3. @TCS_Off_Addr = split($TCS_Office, ' ');
  4. Both Option 1 and Option 2 are correct
  5. All options 1, 2 and 3 are correct.
Question 11 Multiple Choice (Single Answer)

Which of the following does not print the size of the array @My_array = (1..10);

  1. print scalar @My_array
  2. $my_arr_size = @My_array print $my_arr_size
  3. print $#My_array
  4. print 0+@My_array
  5. print $#My_array + 1
Question 12 Multiple Choice (Single Answer)

Pick the wrong piece of code regarding the transformation of text into array.

  1. $str = "PERL is one of the popular programing languages"; @words = split(' ', $str);
  2. $str = "PERL is one of the popular programing languages"; @words = split('', $str);
  3. $str = "PERL is one of the popular programing languages"; @words = split(' ', $str, 3);
  4. Option 1 and Option 3 are correct
  5. All 3 codes are correct and gives different results
Question 13 Multiple Choice (Single Answer)

Consider the Array @Countries = ("India", "Denmark", "Russia", "Germany", "Japan", "Ireland")

Which of the following statement is correct about the Array operations?

  1. 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")
  2. 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")
  3. pop(@Countries, "India") will remove country "India" from the array @Countries

    Now @Countries looks like
    @Countries = ("Denmark", "Russia", "Germany", "Japan", "Ireland")
  4. pop(@Countries, "Ireland") will remove the country "Ireland" from the array @Countries

    Now @Countries looks like
    @Countries = ("India", "Denmark", "Russia", "Germany", "Japan")
  5. pop(@Countries) will remove the country "Ireland" last element of the array @Countries

    Now @Countries looks like
    @Countries = ("India", "Denmark", "Russia", "Germany", "Japan")
Question 14 Multiple Choice (Single Answer)

Which of the following is correct about Hashes in PERL ?

  1. Hash is a set of Key Value pair and denoted by a symbol %
  2. %Scores_Of = ('Sachin', 90, 'Dravid', 75, 'Sehwag', 51, 'Kumble', 8);
  3. %Scores_Of = ('Sachin' => 90, 'Dravid' => 75, 'Sehwag' => 51, 'Kumble' => 8);
  4. %Scores_Of = (-Sachin => 90, -Dravid => 75, -Sehwag => 51, -Kumble => 8);
  5. All the above are correct
Question 15 Multiple Choice (Single Answer)

Pick wrong piece of code for printing the Hashes in PERL
Consider the hash %My_Portfolio = ('DFHL', 100, 'ITC', 250, 'ASHOKLEY', 500, 'AXISBANK', 150);

  1. print "$_n" for keys %My_Portfolio;
  2. print scalar keys %My_Portfolio;
  3. print "%My_Portfolio";
  4. @Stocks = keys %My_Portfolio; print "$Stocks[1]n";
  5. @Quantity = values %My_Portfolio; print "$Quantity[2]n";