MCQs on Basic Syntax and Data Types | Perl

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

  1. In Perl, scalar variables are prefixed with which symbol?
    a) $
    b) @
    c) %
    d) #
  2. 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);
  3. A hash in Perl is represented using:
    a) $
    b) @
    c) %
    d) *
  4. Which function retrieves all keys from a hash?
    a) values()
    b) keys()
    c) hash_keys()
    d) get_keys()
  5. How do you access the first element of an array named @colors?
    a) @colors[1]
    b) $colors[1]
    c) $colors[0]
    d) @colors[0]
  6. What is the correct way to assign a scalar variable in Perl?
    a) $name == "John";
    b) $name = "John";
    c) $name <- "John";
    d) $name := "John";
  7. Which of these creates an empty hash?
    a) %hash = ();
    b) %hash = {};
    c) @hash = ();
    d) $hash = {};

Comments and Code Structure

  1. What character is used to write single-line comments in Perl?
    a) //
    b) #
    c) /* */
    d) --
  2. Which pragma enforces variable declaration in Perl?
    a) use warnings;
    b) use strict;
    c) use vars;
    d) use debug;
  3. How do you write a multi-line comment in Perl?
    a) /* comment */
    b) """ comment """
    c) # comment line 1\n# comment line 2
    d) // comment
  4. What is the default scope of a variable in Perl?
    a) Global
    b) Local
    c) Static
    d) Dynamic
  5. Indentation in Perl code is:
    a) Mandatory
    b) Optional but recommended
    c) Enforced for loops only
    d) Not allowed
  6. 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
  7. Which of the following makes code reusable in Perl?
    a) Loops
    b) Subroutines
    c) Inline comments
    d) Block scope
  8. 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

  1. 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
  2. Which quotes allow variable interpolation in Perl?
    a) Single quotes
    b) Double quotes
    c) Backticks
    d) Both b and c
  3. What will the following code print?
    $name = “John”; print “Hello, $name!”;
    a) Hello, John!
    b) Hello, $name!
    c) Hello, name!
    d) Error: Undefined variable
  4. How do you escape special characters in a string?
    a) Using \
    b) Using #
    c) Using $
    d) Using !
  5. 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
  6. Which operator is used for string concatenation in Perl?
    a) +
    b) .
    c) &
    d) ||
  7. 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
  8. To compare two strings in Perl, you use:
    a) ==
    b) =
    c) eq
    d) cmp
  9. 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
  10. How do you convert a string to uppercase in Perl?
    a) touppercase($string)
    b) uc($string)
    c) upper($string)
    d) to_upper($string)
  11. Which special variable is used for the last matched string?
    a) $1
    b) $_
    c) $&
    d) $*
  12. 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)
  13. What is the output of the following code?
    $string = “hello”; print uc($string);
    a) Hello
    b) HELLO
    c) hello
    d) hELLO
  14. 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
  15. 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

QnoAnswer
1a) $
2b) @array = (1, 2, 3);
3c) %
4b) keys()
5c) $colors[0]
6b) $name = "John";
7a) %hash = ();
8b) #
9b) use strict;
10c) # comment line 1\n# comment line 2
11a) Global
12b) Optional but recommended
13d) Ends the data input section
14b) Subroutines
15b) They separate program lines
16a) Replacing variables with their values in a string
17d) Both b and c
18a) Hello, John!
19a) Using \
20c) Returns the length of a string
21b) .
22c) Line 1
Line 2
23c) eq
24a) Removes the last character of a string
25b) uc($string)
26c) $&
27b) s/search/replace/
28b) HELLO
29a) Extract a part of a string
30b) Perl Programming


			

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top