MCQs on Control Structures | Perl

Here are 30 multiple-choice questions (MCQs) based on Chapter 3: Control Structures, covering Conditional Statements, Looping Constructs, and Using Labels and Loop Control. These MCQs are designed to test your understanding of key concepts like if-else statements, looping constructs (for, foreach, while, until), and control mechanisms (next, last, redo).


Conditional Statements (if, unless, else, elsif)

  1. Which of the following is a correct way to use an if statement in Perl?
    • A) if x > 10
    • B) if (x > 10)
    • C) if x > 10 then
    • D) if (x > 10) then
  2. What is the correct syntax for an else-if statement in Perl?
    • A) if (condition) { } else if (condition) { }
    • B) if (condition) { } elif (condition) { }
    • C) if (condition) { } elsif (condition) { }
    • D) if (condition) { } elseif (condition) { }
  3. In which of the following situations will an “unless” statement return true?
    • A) If the condition is false
    • B) If the condition is true
    • C) If the condition is nil
    • D) None of the above
  4. Which of these is the correct syntax for a basic if-else construct in Perl?
    • A) if (x > 10) { } else { }
    • B) if x > 10 { } else { }
    • C) if x > 10 then { } else { }
    • D) if (x > 10) then { } else { }
  5. What will happen if the condition in an if statement evaluates to false?
    • A) The else block will execute
    • B) The if block will execute
    • C) The code will throw an error
    • D) None of the above
  6. How do you check for equality in a conditional statement in Perl?
    • A) ==
    • B) =
    • C) !=
    • D) ===
  7. What happens if you use an “elsif” without an “if” statement in Perl?
    • A) Syntax error
    • B) It will work as expected
    • C) It will be interpreted as “else”
    • D) None of the above
  8. How would you modify an if statement to check if a variable x is not equal to 10?
    • A) if (x != 10) { }
    • B) if (x < 10) { }
    • C) if (x == 10) { }
    • D) if (x =! 10) { }

Looping Constructs (for, foreach, while, until)

  1. Which loop executes a block of code a specific number of times in Perl?
    • A) foreach
    • B) while
    • C) for
    • D) until
  2. What is the correct syntax for a foreach loop in Perl?
  • A) foreach ($item in @array) { }
  • B) foreach @array as $item { }
  • C) foreach $item (@array) { }
  • D) foreach ($array as $item) { }
  1. How does a while loop in Perl execute?
  • A) Executes the loop until a condition is true
  • B) Executes the loop as long as the condition is true
  • C) Executes the loop if the condition is false
  • D) It runs indefinitely unless manually stopped
  1. Which of the following describes the behavior of the “until” loop?
  • A) It runs as long as the condition is true
  • B) It runs as long as the condition is false
  • C) It runs when the loop is stopped
  • D) It runs only once
  1. Which of the following is a correct way to write a “for” loop in Perl?
  • A) for (my $i = 0; $i < 10; $i++) { }
  • B) for ($i = 0; $i < 10) { }
  • C) for (my $i = 0; $i > 10; $i–) { }
  • D) for (my $i = 10; $i < 0; $i++) { }
  1. How do you stop the execution of a loop prematurely in Perl?
  • A) break
  • B) exit
  • C) continue
  • D) last
  1. Which of the following is NOT a valid loop in Perl?
  • A) for
  • B) foreach
  • C) until
  • D) loop
  1. What does a while loop do if its condition is false when first evaluated?
  • A) Executes the loop body once
  • B) Skips the loop body entirely
  • C) Goes into an infinite loop
  • D) None of the above
  1. How does a for loop in Perl differ from a foreach loop?
  • A) for loop is used for indexed loops, while foreach is for iterating through elements
  • B) They are the same
  • C) foreach is used for numerical ranges
  • D) for loop is for condition checks only
  1. Which loop will execute as long as the condition is true in Perl?
  • A) while
  • B) for
  • C) until
  • D) foreach
  1. What is the output of the following loop?
    for (my $i = 1;
    $i <= 5; $i++) { print “$i\n”;
    }
    A) 1 2 3 4 5
    B) 1 2 3 4
    C) 5 4 3 2 1
    D) 1 1 1 1 1
  1. What will happen if a “while” loop condition always evaluates to true?
  • A) The loop will execute once
  • B) The loop will never execute
  • C) The loop will run indefinitely
  • D) The loop will skip execution
  1. Which keyword can be used to exit a loop immediately in Perl?
  • A) next
  • B) exit
  • C) last
  • D) continue
  1. In which scenario would you use an “until” loop instead of a “while” loop?
  • A) When you want to loop while a condition is false
  • B) When you want to loop while a condition is true
  • C) When you don’t know the condition
  • D) When you want to iterate over an array

Using Labels and Loop Control (next, last, redo)

  1. What does the “next” keyword do in a loop?
  • A) Skips the current iteration and continues with the next one
  • B) Ends the loop immediately
  • C) Restarts the loop from the beginning
  • D) None of the above
  1. What is the purpose of the “last” keyword in Perl?
  • A) To skip the current iteration
  • B) To restart the loop
  • C) To exit the loop entirely
  • D) To prevent further iterations
  1. What does the “redo” keyword do in a loop?
  • A) Restarts the loop from the beginning
  • B) Skips to the next iteration
  • C) Repeats the current iteration
  • D) Exits the loop
  1. Which of the following is true about the “next” keyword in Perl?
  • A) It stops the loop entirely
  • B) It skips the current iteration and moves to the next one
  • C) It restarts the loop from the beginning
  • D) It pauses the loop
  1. When would you use the “last” keyword in a loop?
  • A) When you want to terminate the loop early
  • B) When you want to skip an iteration
  • C) When you want to continue iterating
  • D) When you want to restart the loop
  1. Which of the following examples uses the “redo” keyword correctly?
  • A) redo
  • B) redo $label
  • C) redo loop
  • D) redo { }
  1. Which of the following is a correct use of “next” in Perl?
  • A) next if $x > 5;
  • B) next $x;
  • C) next if $x == 5;
  • D) next loop;
  1. What happens when “last” is used within a nested loop?
  • A) It exits both loops
  • B) It exits the outer loop
  • C) It exits the inner loop
  • D) It exits the entire program

Answer Key

QnoAnswer
1B) if (x > 10)
2C) if (condition) { } elsif (condition) { }
3A) If the condition is false
4A) if (x > 10) { } else { }
5A) The else block will execute
6A) ==
7A) Syntax error
8A) if (x != 10) { }
9C) for
10C) foreach $item (@array) { }
11B) Executes the loop as long as the condition is true
12B) It runs as long as the condition is false
13A) for (my $i = 0; $i < 10; $i++) { }
14D) last
15D) loop
16B) Skips the loop body entirely
17A) for loop is used for indexed loops, while foreach is for iterating through elements
18A) while
19A) 1 2 3 4 5
20C) The loop will run indefinitely
21C) last
22A) When you want to loop while a condition is false
23A) Skips the current iteration and continues with the next one
24C) To exit the loop entirely
25C) Repeats the current iteration
26B) It skips the current iteration and moves to the next one
27A) When you want to terminate the loop early
28B) redo $label
29A) next if $x > 5;
30C) It exits the inner loop

			

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