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
Which of the following will be matched by this code?
my $pattern = '.*';
$str =~ /(\Q$pattern\E)/;
Answer
Which of the following erases all entries in the %h hash?
AnswerWhich of the following is the correct way of sorting an array of integers in ascending order?
AnswerWhat gets printed?
my $var = 1;
$main::var = 2;
if ($var == $main::var) {
print 'true';
} else {
print 'false';
}
Answer
How many key-value pairs will the hash contain?
my %hash = (
[1, 2] => 1,
[1, 2] => 2
);
Answer
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"
}
Answer
What gets printed?
package A;
sub NEW { bless {}, shift }
sub AUTOLOAD { print ref(shift) }
package main;
my $obj = NEW A;
$obj->foo();
Answer
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';
}
Answer
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();
Answer
What will be printed?
my $var = sub { print 'Hello!' };
print ref($var);
Answer
What gets printed?
my @a = ([1, 2, 3, 4], [5, 6, 7, 8]);
print join(' ', @{$a[1]}[1..3]);
Answer
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";
}
Answer
What will be printed?
BEGIN {
print 'a';
}
END {
print 'b';
}
BEGIN {
print 'c';
}
END {
print 'd';
}
Answer
What gets printed?
my $a = 1;
{
$a = 2;
}
END {
$a = 3;
}
print $a;
Answer
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();
Answer
What will be printed?
print 'a';
BEGIN {
print 'b';
}
sub foo {
print 'c';
}
Answer
What will be printed?
my @a = (1, undef, 2);
my $sum = 0;
foreach my $val (@a) {
eval {
$sum += foo($val);
};
if ([email protected]) {
$sum += 100;
}
}
print "$sum\n";
sub foo {
my $val = shift;
die "I don't like undefs" unless defined $val;
return $val + $val;
}
Answer
What will be printed?
BEGIN {
my $id = 0;
sub nextval {
$id++;
return $id;
}
}
my $id = 2;
$id = nextval();
print $id;
Answer
What will be printed?
my @a = (0, 1,);
print scalar(@a), "\n";
Answer
© 2017 QuizBucket.org