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.
forwhileuntilforeachfor loop in Shell scripting do?while loop structured in Shell scripting?while condition; do commands; donewhile commands; do condition; donewhile condition: commandswhile commands until conditionwhile and until loops in Shell scripting?while runs while the condition is true, until runs until the condition is truewhile runs only once, until runs indefinitelywhile uses a counter, until doesn’twhile is faster than untilforwhileuntilrepeatfor loop if the condition is met?stopbreakexitcontinuefor loop in Shell scripting?for i in 1 2 3 4; do echo $i; done1 2 3 4 on the same lineforwhileuntilrepeatcount=1; while [ $count -le 5 ]; do echo $count; count=$((count+1)); donecount=5break statement do in a loop?exitcontinueskipreturncontinue statement is used in a for loop?for i in 1 2 3 4 5; do if [ $i -eq 3 ]; then continue; fi; echo $i; done1 2 3 4 51 2 4 531 2break statement be used in a while loop?for loopsuntil loopscontinue must be used insteadexitstopbreakendbreak 2 in a nested loop?continue statement inside a for loop?continuereturnexitbreakcontinue statement to skip the rest of the loop and move to the next iteration?forwhileuntilreadinputinputdataaskread command in Shell scripting do?storereadinputinputdatawhile loop that checks for valid inputuntil loop with an input checkcontinue for invalid inputwhile [ "$input" != "quit" ]; do read input; donewhile [ "$input" == "quit" ]; do read input; donefor input in quit; do read input; doneread input until quitecho "Enter your name:"; read nameecho("Enter your name:"); input nameprompt "Enter your name:"ask "Enter your name:"while condition; do read input; donefor condition; do read input; doneuntil condition; do read input; donerepeat condition; do read input; donewhile true; do read -p "Enter a number: " num; if [ "$num" -gt 10 ]; then break; fi; donefor loopwhile loop with input validationuntil loop with exit conditionscontinue and break-p flag in the read command?| Qno | Answer |
|---|---|
| 1 | a) for |
| 2 | a) It repeats a command a specific number of times |
| 3 | a) while condition; do commands; done |
| 4 | a) while runs while the condition is true, until runs until the condition is true |
| 5 | c) until |
| 6 | b) break |
| 7 | c) It requires a fixed number of iterations |
| 8 | a) It will print the numbers 1, 2, 3, 4 |
| 9 | c) until |
| 10 | a) It prints the numbers from 1 to 5 |
| 11 | b) It exits the loop entirely |
| 12 | b) continue |
| 13 | a) It skips the current iteration and continues with the next iteration |
| 14 | b) 1 2 4 5 |
| 15 | b) Yes, to exit the loop |
| 16 | c) break |
| 17 | b) It breaks both loops |
| 18 | b) The loop skips the current iteration and continues with the next one |
| 19 | d) break |
| 20 | d) All of the above |
| 21 | a) read |
| 22 | b) It accepts user input from the command line |
| 23 | b) read |
| 24 | d) All of the above |
| 25 | a) while [ "$input" != "quit" ]; do read input; done |
| 26 | a) echo "Enter your name:"; read name |
| 27 | c) until condition; do read input; done |
| 28 | a) It will prompt the user until a number greater than 10 is entered |
| 29 | b) Using a while loop with input validation |
| 30 | b) It outputs a prompt before reading the input |