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)
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
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) { }
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
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 { }
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
How do you check for equality in a conditional statement in Perl?
A) ==
B) =
C) !=
D) ===
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
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)
Which loop executes a block of code a specific number of times in Perl?
A) foreach
B) while
C) for
D) until
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) { }
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
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
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++) { }
How do you stop the execution of a loop prematurely in Perl?
A) break
B) exit
C) continue
D) last
Which of the following is NOT a valid loop in Perl?
A) for
B) foreach
C) until
D) loop
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
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
Which loop will execute as long as the condition is true in Perl?
A) while
B) for
C) until
D) foreach
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
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
Which keyword can be used to exit a loop immediately in Perl?
A) next
B) exit
C) last
D) continue
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)
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
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
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
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
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
Which of the following examples uses the “redo” keyword correctly?
A) redo
B) redo $label
C) redo loop
D) redo { }
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;
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
Qno
Answer
1
B) if (x > 10)
2
C) if (condition) { } elsif (condition) { }
3
A) If the condition is false
4
A) if (x > 10) { } else { }
5
A) The else block will execute
6
A) ==
7
A) Syntax error
8
A) if (x != 10) { }
9
C) for
10
C) foreach $item (@array) { }
11
B) Executes the loop as long as the condition is true
12
B) It runs as long as the condition is false
13
A) for (my $i = 0; $i < 10; $i++) { }
14
D) last
15
D) loop
16
B) Skips the loop body entirely
17
A) for loop is used for indexed loops, while foreach is for iterating through elements
18
A) while
19
A) 1 2 3 4 5
20
C) The loop will run indefinitely
21
C) last
22
A) When you want to loop while a condition is false
23
A) Skips the current iteration and continues with the next one
24
C) To exit the loop entirely
25
C) Repeats the current iteration
26
B) It skips the current iteration and moves to the next one