PERL quiz questions

PERL interview questions

  • 1.

    How can information be put into hashes?

    Answer:

    When a hash value is referenced, it is not created. It is only created once a value is assigned to it. The contents of a hash have no literal representation. In case the hash is to be filled at once the unwinding of the hash must be done. The unwinding of hash means the key value pairs in hash can be created using a list, they can be converted from that as well. In this conversion process the even numbered items are placed on the right and are known as values. The items placed on the left are odd numbered and are stored as keys. The hash has no defined internal ordering and hence the user should not rely on any particular ordering.

    Example of creating hash:

    %birthdate = ( Ram => "01-01-1985",
     
     
    Vinod => "22-12-1983",
     
    Sahil => "13-03-1989",
     
    Sony => "11-09-1991");

     

    View
  • 2.

    Which of the following will be matched by this code?

    my $pattern = '.*';
    $str =~ /(\Q$pattern\E)/;
    1. any number of any characters

    2. any number of any characters surrounded by Q and E

    3. literally Q.*E

    4. literally .*

    5. literally \Q.*\E

    Answer
  • 3.

    Which of the following erases all entries in the %h hash?

    1. undef %h;

    2. delete %h;

    3. %h = {};

    4. %h = ();

    5. delete keys(%h);

    Answer
  • 4.

    Which of the following is the correct way of sorting an array of integers in ascending order?

    1. sort @a

    2. sort {$1 <=> $2} @a

    3. sort {$_[0] <=> $_[1]} @a

    4. sort {$a <=> $b} @a

    5. sort {$_[0] cmp $_[1]} @a

    Answer
  • 5.

    What gets printed?

    my $var = 1;
    $main::var = 2;
    
    if ($var == $main::var) {
      print 'true';
    } else {
      print 'false';
    }
    1. true

    2. false

    Answer
  • 6.

    How many key-value pairs will the hash contain?

    my %hash = (
      [1, 2] => 1,
      [1, 2] => 2
    );
    1. 1

    2. 2

    3. 3

    4. 4

    5. the code will fail

    Answer
  • 7.

    Which messages will be printed?

    package Animal;
    sub AUTOLOAD {}                                   
    sub new { bless {}, shift }
    
    package Dog;
    use base 'Animal';
    sub run {}
    
    package main;
    
    my $obj = Dog->new();
    
    if ($obj->can('run')) {
        print "can run\n"  
    }
     
    if ($obj->can('bark')) {
        print "can bark\n"  
    }
    1. both 'can run' and 'can bark'

    2. can run

    3. can bark

    4. none of the above

    5. the code will fail

    Answer
  • 8.

    What gets printed?

    package A;
    sub NEW { bless {}, shift }
    sub AUTOLOAD { print ref(shift) }
    
    package main;
    
    my $obj = NEW A;
    
    $obj->foo();
    1. A

    2. AA

    3. Can't locate object method "foo" via package "A"

    4. none of the above

    Answer
  • 9.

    What will be printed?

    package A;
    sub new { return bless {}, shift; }
    
    package B;
    use base 'A';
    
    package main;
    
    my $obj = B->new();
    
    if ($obj->isa('A')) {
      print 'true';
    } else {
      print 'false';         
    }
    1. true

    2. false

    Answer
  • 10.

    What gets printed after execution of the following program?

    package A;
    
    sub new { return bless {}, shift; }
    sub DESTROY { print ref(shift); }
    
    
    package B;
    use base 'A';
    
    sub DESTROY {
        my $self = shift;
        print ref($self);
        bless $self, 'A';
    }
    
    
    package main;
    
    my $obj = B->new();
    1. A

    2. B

    3. AB

    4. BA

    5. none of the above

    Answer
  • 11.

    What will be printed?

    my $var = sub { print 'Hello!' };
     
    print ref($var);
    1. 1

    2. true

    3. CODE

    4. SCALAR

    5. SUB

    Answer
  • 12.

    What gets printed?

    my @a = ([1, 2, 3, 4], [5, 6, 7, 8]);
    
    print join(' ', @{$a[1]}[1..3]);
    1. 1 2 3

    2. 2 3 4

    3. 5 6 7

    4. 6 7 8

    5. the code will fail

    Answer
  • 13.

    What will be printed (disregard newlines)?

    package A;
    
    sub new { return bless {} }
    sub DESTROY { print "a\n" }
    
    
    package main;
    
    my $a = A->new();
    
    die "b\n";
    
    END {
        print "c\n";
    }
    1. a b c

    2. b

    3. b c

    4. b a c

    5. b c a

    Answer
  • 14.

    What will be printed?

    BEGIN {
        print 'a';
    }
    
    END {
        print 'b';
    }
    
    BEGIN {
        print 'c';
    }
    
    END {
        print 'd';
    }
    1. abcd

    2. acbd

    3. cadb

    4. acdb

    5. the code will fail

    Answer
  • 15.

    What gets printed?

    my $a = 1;
    
    {
        $a = 2;  
    }
    
    END {
        $a = 3;
    }
    
    print $a;
    1. 1

    2. 2

    3. 3

    4. the code will fail

    Answer
  • 16.

    Which messages will be printed in the code below?

    package A;
    
    sub NEW {
        print "hello world!\n";
        return bless {};
    };
    
    sub DESTROY {
        print "goodbye world!\n";
    };
    
    
    package main;
    
    my $obj = A::NEW();
    1. both

    2. hello world!

    3. goodbye world!

    4. neither

    5. the code will fail

    Answer
  • 17.

    What will be printed?

    print 'a';
    
    BEGIN {
        print 'b';
    }
    
    sub foo {
        print 'c';
    }
    1. abc

    2. bac

    3. ab

    4. ba

    5. bca

    Answer
  • 18.

    What will be printed?

    my @a = (1, undef, 2);
    
    my $sum = 0;
    foreach my $val (@a) {
        eval {
            $sum += foo($val);
        };
        if ($@) {
            $sum += 100;
        }
    }
    
    print "$sum\n";
    
    sub foo {
        my $val = shift;
        die "I don't like undefs" unless defined $val;
        return $val + $val;
    }
    1. 2

    2. 6

    3. 106

    4. I don't like undefs

    Answer
  • 19.

    What will be printed?

    BEGIN {
        my $id = 0;
        sub nextval {
            $id++;
            return $id;
        }
    }
    
    my $id = 2;
    $id = nextval();
    print $id;
    1. 0

    2. 1

    3. 2

    4. 3

    5. the code will fail

    Answer
  • 20.

    What will be printed?

    my @a = (0, 1,);
    
    print scalar(@a), "\n";
    1. 0

    2. 1

    3. 2

    4. 3

    5. the code will fail

    Answer

© 2017 QuizBucket.org