# PERL quiz questions

• 1.

What will be the value of \$count after executing the following code?

``````my %hash = (a => 1, b => 2);

my \$count = 0;

while (my (\$key, \$value) = each(%hash)) {
\$count++ if \$value == keys(%hash)
}``````
1. 0

2. 1

3. 2

4. 3

5. the code if ill-formed

• 2.

What will be the value of \$keys?

``````my %hash;

\$hash{undef} = undef;
\$hash{''} = '';

my \$keys = keys(%hash);``````
1. undef

2. 0

3. 1

4. 2

5. the code is ill-formed

• 3.

What will the @b array consist of?

``````my @a = (10, 5, 1);

my @b = sort @a;``````
1. (1, 5, 10)

2. (10, 5, 1)

3. (1, 10, 5)

4. (5, 10, 1)

5. ()

• 4.

What will be the value of \$val?

``````my \$str = 'aa bb cccc';

my \$val = () = \$str =~ /\w+/g;``````
1. undef

2. aa

3. cccc

4. 0

5. 3

• 5.

What will be printed?

``````use strict;
my @a = (1, 2, 3);

my %b = (2 => undef);

for my \$val (@a) {
last if \$b{\$val};
}

print \$val;``````
1. 1

2. 2

3. 3

4. undef

5. the code is ill-formed

• 6.

What will be the value of \$size after executing the following code?

``````my @a = (0, 1, 2);

\$#a = 0;

my \$size = @a;``````
1. undef

2. 0

3. 1

4. 2

5. 3

• 7.

What gets printed?

``````my \$a = 123456;
my \$b = 123_456;

if (\$a == \$b) {
print "same";
} else {
print "different";
}``````
1. same

2. different

3. the code is ill-formed

• 8.

What gets printed?

``````my \$a = '123';
my \$b = '0123';

if (\$a == \$b) {
print "same";
} else {
print "different";
}``````
1. same

2. different

3. the code is ill-formed

• 9.

What gets printed?

``````my \$a = 123;
my \$b = 0123;

if (\$a == \$b) {
print "same";
} else {
print "different";
}``````
1. same

2. different

3. the code is ill-formed

• 10.

What gets printed?

``````package A;

sub new {
my \$class = shift;
my \$self = {};
\$self->init();
return bless \$self, \$class;
};

sub init {
my \$self = shift;
\$self->{key} = 'value';
}

sub get {
my (\$self, \$key) = @_;
return \$self->{\$key};
}

package main;

my \$obj = A->new();
print \$obj->get('key'), "\n";``````
1. empty string

2. value

3. the code will fail

• 11.

What will be printed by the following code?

``````package A;

sub new { return bless {}, shift };
sub foo { return 'A' }

package B1;
use base 'A';

sub foo { return 'B1' }

package B2;
use base 'A';

sub foo { return 'B2' }

package C;
use base 'B1', 'B2';

package main;

my \$obj = C->new();

print \$obj->foo(), "\n";``````
1. A

2. B1

3. B2

4. the code is ill-formed

• 12.

What will be printed by the following code?

``````package A;

sub new { return bless {}; }
sub foo { return 'A'; }

package B;
use base 'A';

sub foo { return 'B'; }

package main;

my \$obj = B->new();

print \$obj->foo(), "\n";``````
1. A

2. B

3. the code will fail

• 13.

What will be the value of \$str after execution of the following code?

``````my \$str = '112133';

\$str =~ s/(.)\1/\$1/g;``````
1. 112133

2. 1233

3. 1213

4. 12133

• 14.

What gets printed?

``````my \$str = 'a\\b\n';

print \$str;``````
1. ab(newline)

2. a\b(newline)

3. a\b\n

4. a\\b(newline)

5. a\\b\n

• 15.

What will be printed?

``````my \$val = '0';

if (\$val) {
print "true\n";
} else {
print "false\n";
}``````
1. true

2. false

• 16.

What gets printed?

``````my \$a = (0, 1, 2);

print "\$a\n";``````
1. 0

2. 1

3. 2

4. 3

5. 0 1 2

• 17.

What gets printed?

``````my \$a = 3;

print foo(), "\n";

sub foo {
my \$a = \$a;
}``````
1. undef

2. 0

3. 3

• 18.

What will be the value of \$keys after execution of the following code?

``````my \$var;

if (exists \$var->{key1}->{key2}) {
\$var->{key1}->{key2} = 1;
}

my \$keys = keys(%{\$var});``````
1. undef

2. 0

3. 1

4. 2

5. code will fail

• 19.

All three ways of calling new() are equivalent in the code below

``````package A;

sub new {
my (\$class, \$param) = @_;
return bless {param => \$param}, \$class;
}

package main;

my \$a1 = A->new('foo');
my \$a2 = new A('foo');
my \$a3 = A::new('A', 'foo');``````
1. true

2. false

• 20.

What gets printed by the following program?

``````my (\$x) = foo();
print "\$x\n";

sub foo {
return wantarray ? 'list context' : 'scalar context';
}``````
1. list context

2. scalar context

3. nothing