Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

re: perl questions, confused 1

Status
Not open for further replies.

oysters2000

Programmer
Feb 14, 2005
5
GB
please would someone please explain the following with the answer they get as i have different answers from two different books. They seem to contridict themselves. I will be writing my exam tomorrow and need to clear these up.
much appreciated.



What output is generated by the following code segmenet?
$i = 0;
while ($i++ <= 5)
{
print $i;
}
a. 2345
b. 12345
c. 012345
d. 123456

What output is generated by the following code segment?
"rubberband" =~ m/(b{1,3})/;
print $1;
a. b
b. bb
c. bbb
d. bberb

What output is generated by the following code segment?
@list = ("Alpha", "beta", "0123", "123");
@list = sort(@list);
print $list[1];
a. Alpha
b. beta
c. 0123
d. 123

What output is generated by the following code segment?
%hash = (1 => "Air",
2 => "Land",
3 => "Sea",
4 => "Space");
%hash = reverse(%hash);
foreach $key (keys(%hash))
{
if ($key =~ m/[^Space]{3,}/)
{
print $hash{$key};
}

Consider the following Perl code:
%myHash = (Alpha => "A",
Beta => "B",
Gamma => "G",
G => "Delta");
print($myHash{"Alpha"});
print(" ");
print($myHash{"Beta"});
print(" ");
reverse(%myHash);
print($myHash{"G"});
What is the output of this code?
a. Alpha Beta Gamma
b. Alpha Beta G
c. A B G
d. A B Delta

Consider the following Perl code:
%myHash = (Alpha => "A",
Beta => "B",
Gamma => "G",
G => "Delta");
print($myHash{"Alpha"});
print(" ");
print($myHash{"Beta"});
print(" ");
delete($myHash{"Alpha"});
delete($myHash{"Beta"});
delete($myHash{"Gamma"});
foreach $key (keys(%myHash))
{
print("$key $myHash{$key}");
}
What is the output of this code?
a. Alpha Beta A B G Delta
b. A B G Delta
c. A B A B G Delta
d. A B

Consider the following Perl code:
@myArray = ("Beta", "Gamma", "Alpha");
print("$myArray[1] ");
sort(@myArray);
print($myArray[1]);
What is the output of this code?
a. Gamma Gamma
b. Beta Gamma
c. Gamma Beta
d. Beta Alpha

Consider the following Perl code:
@myArray = ("A", "B", "C");
unshift(@myArray, pop(@myArray));
unshift(@myArray, pop(@myArray));
unshift(@myArray, pop(@myArray));
print("@myArray");
What is the output of this code?
a. No output is generated
b. A B C
c. C B A
d. C
}
a. 1
b. 2
c. 4
d. Space

What output is generated by the following code segment?
print (add(5, 7));
sub add
{
foreach $val (@_)
{
$total += $val;
print $val;
}
}
a. 5
b. 7
c. 12
d. 57

What output is generated by the following code segment?
print (add(3, 4));
sub add
{
for ($i = 0; $i < 2; $i++)
{
$total += $_[$i];
}
}
a. 2
b. 3
c. 4
d. 7

What output is generated by the following code segmenet?
$i = 0;
while ($i++ < 5)
{
print $i;
}
a. 2345
b. 12345
c. 012345
d. 123456

What output is generated by the following code segment?
$var = "Alpha";
package Beta;
$var = "Beta";
{
package Gamma;
$var = "Gamma";
package main;
}
print $var;
a. Alpha
b. Beta
c. Gamma
d. No output is generated

Consider the following module definition:
package Employee;
1;
sub new
{
my $object = {};
$object->{"name"} = $_[0];
$object->{"salary"} = $_[1];
$object->{"start_date"} = $_[2];
return bless $object;
}

What output is generated by the following code segment using the module defined above?
require Employee;
$jose = new Employee("Jose Martinez", 45500, "11/5/2001");
print $jose->{"name"};
a. name
b. jose
c. Jose Martinez
d. Employee
 
oysters,

run them and see, and stop posting the same question over and over.

If you come across a specific problem, post again, but not another exam sheet

1.d.

cigless ...
 
PaulTEG said:
run them and see, and stop posting the same question over and over.
Yes, I've already asked you not to double-post.
 
well, it wasn't going through, that is why i dbl posted it.Then i forgot to explain why i needed these quetions answered as you guys might think that it is homewrok that i have not done. Here are my answers as i can get my perl to run to check the answers, i wouldn't be emailing you if i could run the scripts and have my answers straight away!

here are my answers
this not home work as will be writing an exam and the answers i get are wrong. They dont make sense as these are not difficult questions.
here are my answers
c,b,b,c,b,c,c,c,d,b,a,c
the book says they are wrong?
 
What output is generated by the following code segmenet?
$i = 0;
while ($i++ <= 5)
{
print $i;
}
a. 2345
b. 12345
c. 012345
d. 123456
[tt]while ($i++ <= 5)[/tt]
It's [tt]$i++[/tt] not [tt]++$i[/tt].
This means that the value of $i is fetched before
$i is incremented. But $i will be incremented before
the print statement. So the equivalent is

$i = 0;
while ($i <= 5)
{ $i++;
print $i;
}

and the output will be
123456

"rubberband" =~ m/(b{1,3})/;
print $1;
[tt]b{1,3}[/tt] will match "b" or "bb" or "bbb".
So the output will be
bb

You really don't have a Perl interpreter, do you?
 
As far as I can see there are three possibilities.[ol][li]perl isn't available for your operating system. Seems unlikely.[/li][li]You have it, but it doesn't work. Uninstall, download a new one, reinstall. Job done.[/li][li]You are accessing this Internet site from a WAP phone. Doesn't it take a long type to key in the postings?[/li][/ol]
 
Perl and/or Python soon to be available on mobile phones, can't find the link now, but it was due end Q3/04

--Paul

cigless ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top