Boost your Perl knowledge with these 30 MCQs covering scalars, arrays, hashes, comments, code structure, and string manipulation. Perfect for learners aiming to master basic syntax and data types.
MCQs: Chapter 2 – Basic Syntax and Data Types
Scalars, Arrays, and Hashes
In Perl, scalar variables are prefixed with which symbol? a) $ b) @ c) % d) #
What is the correct syntax for declaring an array in Perl? a) $array = (1, 2, 3); b) @array = (1, 2, 3); c) %array = (1, 2, 3); d) array[ ] = (1, 2, 3);
A hash in Perl is represented using: a) $ b) @ c) % d) *
Which function retrieves all keys from a hash? a) values() b) keys() c) hash_keys() d) get_keys()
How do you access the first element of an array named @colors? a) @colors[1] b) $colors[1] c) $colors[0] d) @colors[0]
What is the correct way to assign a scalar variable in Perl? a) $name == "John"; b) $name = "John"; c) $name <- "John"; d) $name := "John";
Which of these creates an empty hash? a) %hash = (); b) %hash = {}; c) @hash = (); d) $hash = {};
Comments and Code Structure
What character is used to write single-line comments in Perl? a) // b) # c) /* */ d) --
Which pragma enforces variable declaration in Perl? a) use warnings; b) use strict; c) use vars; d) use debug;
How do you write a multi-line comment in Perl? a) /* comment */ b) """ comment """ c) # comment line 1\n# comment line 2 d) // comment
What is the default scope of a variable in Perl? a) Global b) Local c) Static d) Dynamic
Indentation in Perl code is: a) Mandatory b) Optional but recommended c) Enforced for loops only d) Not allowed
What does the __END__ token do in Perl scripts? a) Ends the script execution immediately b) Terminates the file input reading c) Marks the end of the program d) Ends the data input section
Which of the following makes code reusable in Perl? a) Loops b) Subroutines c) Inline comments d) Block scope
What is the significance of semicolons in Perl? a) They are optional at the end of statements b) They separate program lines c) They denote a block d) They mark comments
Interpolation and String Manipulation
What is interpolation in Perl? a) Replacing variables with their values in a string b) Merging two strings c) Searching for a pattern in a string d) Encoding a string
Which quotes allow variable interpolation in Perl? a) Single quotes b) Double quotes c) Backticks d) Both b and c
What will the following code print? $name = “John”; print “Hello, $name!”; a) Hello, John! b) Hello, $name! c) Hello, name! d) Error: Undefined variable
How do you escape special characters in a string? a) Using \ b) Using # c) Using $ d) Using !
What does the length() function do? a) Calculates the size of an array b) Counts the number of elements in a hash c) Returns the length of a string d) Appends a string
Which operator is used for string concatenation in Perl? a) + b) . c) & d) ||
What will the following code output? print “Line 1\nLine 2”; a) Line 1 Line 2 b) Line 1\nLine 2 c) Line 1 Line 2 d) Line 1\nLine 2
To compare two strings in Perl, you use: a) == b) = c) eq d) cmp
What does the chop() function do? a) Removes the last character of a string b) Removes the first character of a string c) Removes whitespace from a string d) Reverses a string
How do you convert a string to uppercase in Perl? a) touppercase($string) b) uc($string) c) upper($string) d) to_upper($string)
Which special variable is used for the last matched string? a) $1 b) $_ c) $& d) $*
How do you replace a substring in a string? a) str_replace b) s/search/replace/ c) replace(search, replace) d) update_str(search, replace)
What is the output of the following code? $string = “hello”; print uc($string); a) Hello b) HELLO c) hello d) hELLO
The substr() function in Perl is used to: a) Extract a part of a string b) Convert a string to uppercase c) Reverse a string d) Find the length of a string
What is the output of this code? $str = “Perl”; $str .= ” Programming”; print $str; a) PerlProgramming b) Perl Programming c) Perl d) Perl-Programming
Answer Key
Qno
Answer
1
a) $
2
b) @array = (1, 2, 3);
3
c) %
4
b) keys()
5
c) $colors[0]
6
b) $name = "John";
7
a) %hash = ();
8
b) #
9
b) use strict;
10
c) # comment line 1\n# comment line 2
11
a) Global
12
b) Optional but recommended
13
d) Ends the data input section
14
b) Subroutines
15
b) They separate program lines
16
a) Replacing variables with their values in a string