Perl MCQ Banner

Perl Multiple Choice Questions (MCQs) and Answers

Master Perl with Practice MCQs. Explore our curated collection of Multiple Choice Questions. Ideal for placement and interview preparation, our questions range from basic to advanced, ensuring comprehensive coverage of Perl. Begin your placement preparation journey now!

Q61

Q61 What will if ("hello" =~ /h.llo/) return?

A

True

B

False

C

Error

D

Undefined

Q62

Q62 What does $string =~ s/\d+/XXX/g; do?

A

Replaces all numbers with "XXX"

B

Removes all numbers

C

Finds numbers in a string

D

Counts the digits in a string

Q63

Q63 What will $string =~ tr/a-z/A-Z/; do in Perl?

A

Convert lowercase to uppercase

B

Remove all lowercase letters

C

Replace spaces with underscores

D

Find occurrences of vowels

Q64

Q64 What will be the output of $string = "abc123"; $string =~ /(\d+)/; print "$1";?

A

abc

B

123

C

Error

D

Empty string

Q65

Q65 Why does if ($text =~ /word/) sometimes fail unexpectedly?

A

Perl does not support regex

B

The variable is undefined

C

Regex match is case-sensitive by default

D

Regex must always use s///

Q66

Q66 Why does if ($str =~ /^test/) fail to match " mytest"?

A

The regex is incorrect

B

The caret ^ matches only the start of the string

C

Regex does not support spaces

D

The /i flag is missing

Q67

Q67 Why does if ($data =~ /\bword\b/) fail when searching for "word" inside "password"?

A

Perl does not support \b

B

\b requires spaces around words

C

\b matches word boundaries

D

"password" is an exception in Perl regex

Q68

Q68 Which operator is used to open a file in Perl?

A

<>

B

open

C

read

D

file

Q69

Q69 What does the die function do in Perl file handling?

A

Terminates execution with an error message

B

Continues execution after error

C

Closes the file

D

Deletes a file

Q70

Q70 What mode should be used to open a file for appending in Perl?

A

B

>file

C

>>file

D

<>>file

Q71

Q71 What does do in Perl?

A

Opens a file

B

Reads from a file

C

Deletes a file

D

Closes a file

Q72

Q72 What does binmode do when working with filehandles?

A

Opens a file in binary mode

B

Converts a file to text

C

Encodes file contents

D

Deletes a file

Q73

Q73 What will be the output of the following Perl code?
open(my $fh, "<", "file.txt") or die "Cannot open file";

A

Opens file.txt for reading

B

Opens file.txt for writing

C

Appends to file.txt

D

Throws an error

Q74

Q74 How do you correctly close a file in Perl?

A

end $file;

B

stop $file;

C

close $fh;

D

exit $file;

Q75

Q75 What will the following code do?
open(my $fh, ">", "output.txt"); print $fh "Hello, World!"; close $fh;

A

Creates output.txt and writes "Hello, World!"

B

Appends "Hello, World!" to output.txt

C

Throws an error

D

Reads from output.txt

Q76

Q76 What does this Perl statement do?
open(my $fh, "<", "file.txt") or warn "File not found";

A

Opens file.txt for reading

B

Opens file.txt for writing

C

Prints a warning if the file cannot be opened

D

Deletes file.txt

Q77

Q77 Why does the following Perl script fail?
open(my $fh, "<", "nonexistent.txt") or die "File error";

A

The file does not exist

B

Perl does not support file handling

C

The file is already open

D

Incorrect syntax

Q78

Q78 Why does a file handle sometimes not close properly in Perl?

A

The file is locked

B

The close function is not used

C

Perl does not allow closing files

D

The file has too much data

Q79

Q79 What is a common reason for a "Permission Denied" error in Perl file handling?

A

File is already open

B

Incorrect file path

C

Trying to write to a read-only file

D

File contains binary data

Q80

Q80 What symbol is used to define an array in Perl?

A

%

B

@

C

$

D

&

Q81

Q81 What is the correct way to access the first element of an array in Perl?

A

array[0]

B

@array[0]

C

$array[0]

D

array->0

Q82

Q82 How can you find the number of elements in an array?

A

count(@array)

B

scalar @array

C

size(@array)

D

length(@array)

Q83

Q83 How are hash keys and values stored in Perl?

A

As ordered lists

B

As key-value pairs

C

As arrays

D

As objects

Q84

Q84 What function is used to extract all keys from a hash?

A

keys()

B

values()

C

extract_keys()

D

get_keys()

Q85

Q85 What is the output of @arr = (1, 2, 3); print $arr[1];?

A

1

B

2

C

3

D

Error

Q86

Q86 What will the following Perl code do?
%hash = ("a" => 1, "b" => 2); print $hash{"b"};

A

Prints 1

B

Prints 2

C

Prints b

D

Throws an error

Q87

Q87 What will happen if you attempt to access a hash key that does not exist?

A

Returns undef

B

Throws an error

C

Returns 0

D

Returns an empty string

Q88

Q88 What does push(@array, 5); do?

A

Adds 5 to the end of @array

B

Removes 5 from @array

C

Replaces the first element with 5

D

Deletes @array

Q89

Q89 Why does print @array; sometimes produce unexpected output?

A

@array must be enclosed in double quotes

B

The array contains undefined values

C

Perl does not support printing arrays

D

Arrays must be printed with join()

Q90

Q90 What happens if you attempt to delete a hash key that does not exist?

A

Throws an error

B

Nothing happens

C

Deletes a random key

D

Resets the hash

ad verticalad vertical
ad