Monday, December 30, 2013

Capgemini - Perl Interview questions with additional answers


  1. Brief about data structures in perl.
    Scalars, Arrays and Hashes

  2. What are the differences between arrays and lists?

    • Lists are constant data while arrays are variables.

    • Arrays have a name to identify them, while lists don't.

    • Elements of an array can be modified and stored in the same array variable but the same is not possible with lists.

    • Stack (push,pop) and queue (unshift,shift) operations are possible only on arrays and not possible on lists.
      What are the differences between arrays and hashes?

    • Arrays have numerals as indices while hashes have keys (strings) as indices.

    • Arrays are often ordered list while hashes are unordered list.

  3. Have you used modules in perl? Can you brief about a module and its functions or sub routines?

    1. File::Find, 2. File::Copy, 3. XML::Simple, 4. DBI, 5. WWW::Machanize, 6. CAM::PDF, 7. CGI, 8. DBIx::Class, 9. Net::FTP

  4. Difference between use and require.
    View StackOverflow Answer

  5. How to create objects in OO perl?

  6. Write a perl script to validate an IP address.
    A partly correct way is given here.

  7. How to sort a numeric array?
    View StackOverflow Answer

  8. What are the differences between grep and map functions?
    View StackOverflow Answer

  9. How to sort a hash with key,values pairs as name_of_person,age based on their age?
    %hash = #hash map of format name => age ("ganesh" => "26", "kumaresh" => "27", "kannan" => "30", ); 
    foreach $value (sort {$a <=> $b} values(%hash)) {print "$value\n" }
    
  10. How to remove duplicate entries in an array?
    my @arr = (1,2,2,3,4,4,5,5,5,6,6,1,1,1); #array having duplicates 
    my %hash; 
    foreach (@arr) {$hash{$_} = undef; #creating a hash key for each array element. this line helps in removing duplicates since hash keys cannot be duplicated and new duplicate key overwrites the old key. } 
    my @res = keys %hash; 
    print "@res"; #prints array without puplicates
    
  11. What is the use of @ISA array?
    @ISA is stand for ‘is a’, that means the array contains parents' package

  12. Why do we need references?
    If we need to pass multiple arrays/hashes values into subroutine. In that case we used references.

No comments: