MCQs on Loops in Shell Scripting | Shell Scripting

Learn the fundamentals of loops in Shell scripting, including the for, while, and until loops, break and continue statements, and loop control with input with these 30 MCQs.


for, while, and until Loops

  1. Which loop is used for iterating over a list of items in Shell scripting?
    a) for
    b) while
    c) until
    d) foreach
  2. What does the for loop in Shell scripting do?
    a) It repeats a command a specific number of times
    b) It runs a command indefinitely
    c) It runs commands until a condition is met
    d) It checks if a condition is true before running
  3. How is the while loop structured in Shell scripting?
    a) while condition; do commands; done
    b) while commands; do condition; done
    c) while condition: commands
    d) while commands until condition
  4. What is the main difference between while and until loops in Shell scripting?
    a) while runs while the condition is true, until runs until the condition is true
    b) while runs only once, until runs indefinitely
    c) while uses a counter, until doesn’t
    d) while is faster than until
  5. Which loop is more suitable when you need to repeat something while a condition is false?
    a) for
    b) while
    c) until
    d) repeat
  6. How would you terminate a for loop if the condition is met?
    a) stop
    b) break
    c) exit
    d) continue
  7. Which of the following is true about the for loop in Shell scripting?
    a) It can only iterate over numbers
    b) It repeats until a condition is false
    c) It requires a fixed number of iterations
    d) It runs commands until a condition is true
  8. What will the following command do?
    for i in 1 2 3 4; do echo $i; done
    a) It will print the numbers 1, 2, 3, 4
    b) It will print 1 2 3 4 on the same line
    c) It will repeat forever
    d) It will print the command syntax error
  9. Which loop should be used for executing commands as long as a condition remains false?
    a) for
    b) while
    c) until
    d) repeat
  10. What is the output of the following script?
    count=1; while [ $count -le 5 ]; do echo $count; count=$((count+1)); done
    a) It prints the numbers from 1 to 5
    b) It prints count=5
    c) It prints nothing
    d) It prints an error message

Break and Continue Statements

  1. What does the break statement do in a loop?
    a) It stops the current iteration and moves to the next one
    b) It exits the loop entirely
    c) It breaks the loop into multiple smaller loops
    d) It breaks the loop and restarts
  2. Which statement would you use to skip the current iteration of a loop in Shell scripting?
    a) exit
    b) continue
    c) skip
    d) return
  3. What happens if a continue statement is used in a for loop?
    a) It skips the current iteration and continues with the next iteration
    b) It ends the loop
    c) It restarts the loop from the first iteration
    d) It pauses the loop
  4. What will the following script print?
    for i in 1 2 3 4 5; do if [ $i -eq 3 ]; then continue; fi; echo $i; done
    a) 1 2 3 4 5
    b) 1 2 4 5
    c) 3
    d) 1 2
  5. Can the break statement be used in a while loop?
    a) No, it can only be used in for loops
    b) Yes, to exit the loop
    c) Yes, but only in until loops
    d) No, continue must be used instead
  6. How do you exit a loop prematurely in Shell scripting?
    a) exit
    b) stop
    c) break
    d) end
  7. What is the effect of break 2 in a nested loop?
    a) It breaks the outer loop
    b) It breaks both loops
    c) It breaks the inner loop only
    d) It exits the entire script
  8. What happens if you place a continue statement inside a for loop?
    a) The loop will stop
    b) The loop skips the current iteration and continues with the next one
    c) The loop restarts
    d) The script will end
  9. Which statement is used to stop the execution of a loop in Shell scripting?
    a) continue
    b) return
    c) exit
    d) break
  10. In which type of loop can you use the continue statement to skip the rest of the loop and move to the next iteration?
    a) for
    b) while
    c) until
    d) All of the above

Loop Control with Input

  1. How do you accept user input in Shell scripting?
    a) read
    b) input
    c) inputdata
    d) ask
  2. What does the read command in Shell scripting do?
    a) It displays a message to the user
    b) It accepts user input from the command line
    c) It pauses the script execution
    d) It reads the content of a file
  3. Which command is used to store the user input in a variable?
    a) store
    b) read
    c) input
    d) inputdata
  4. How would you loop until the user provides a valid input?
    a) Using a while loop that checks for valid input
    b) Using an until loop with an input check
    c) Using continue for invalid input
    d) All of the above
  5. Which of the following will stop the loop if the user enters ‘quit’?
    a) while [ "$input" != "quit" ]; do read input; done
    b) while [ "$input" == "quit" ]; do read input; done
    c) for input in quit; do read input; done
    d) read input until quit
  6. How would you prompt the user for input with a message in Shell scripting?
    a) echo "Enter your name:"; read name
    b) echo("Enter your name:"); input name
    c) prompt "Enter your name:"
    d) ask "Enter your name:"
  7. Which of the following is the correct structure to loop until a specific condition is met from user input?
    a) while condition; do read input; done
    b) for condition; do read input; done
    c) until condition; do read input; done
    d) repeat condition; do read input; done
  8. What will the following code do?
    while true; do read -p "Enter a number: " num; if [ "$num" -gt 10 ]; then break; fi; done
    a) It will prompt the user until a number greater than 10 is entered
    b) It will print numbers greater than 10
    c) It will always break after one prompt
    d) It will never break
  9. How can you loop over user input multiple times without manually restarting the script?
    a) Using a for loop
    b) Using a while loop with input validation
    c) Using an until loop with exit conditions
    d) Using continue and break
  10. What is the purpose of the -p flag in the read command?
    a) It pauses the script
    b) It outputs a prompt before reading the input
    c) It prints the input after reading
    d) It stores the input in a file

Answer Key

QnoAnswer
1a) for
2a) It repeats a command a specific number of times
3a) while condition; do commands; done
4a) while runs while the condition is true, until runs until the condition is true
5c) until
6b) break
7c) It requires a fixed number of iterations
8a) It will print the numbers 1, 2, 3, 4
9c) until
10a) It prints the numbers from 1 to 5
11b) It exits the loop entirely
12b) continue
13a) It skips the current iteration and continues with the next iteration
14b) 1 2 4 5
15b) Yes, to exit the loop
16c) break
17b) It breaks both loops
18b) The loop skips the current iteration and continues with the next one
19d) break
20d) All of the above
21a) read
22b) It accepts user input from the command line
23b) read
24d) All of the above
25a) while [ "$input" != "quit" ]; do read input; done
26a) echo "Enter your name:"; read name
27c) until condition; do read input; done
28a) It will prompt the user until a number greater than 10 is entered
29b) Using a while loop with input validation
30b) It outputs a prompt before reading the input

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