Any2Quiz
Computer Science / Languages / Shell

Conditionals and Loops Quiz - Languages Shell

Free practice quiz: Conditionals and Loops (Languages Shell). 48 multiple-choice questions with answers and explanations. Play online, no sign-up needed.

Play all 48 questions free

Sample questions

  1. Consider the following `bash` script: `if [ $(echo $(( 5 % 2 )) ) -eq 0 ]; then echo 'Even'; else echo 'Odd'; fi`. What will be the output?

    1. Even
    2. Odd
    3. Error
    4. Nothing
    Show answer

    Answer: B. Odd

    The expression `$(( 5 % 2 ))` calculates the remainder of 5 divided by 2, which is 1. The `if` condition checks if this remainder is equal to 0. Since 1 is not equal to 0, the `else` block is executed, printing 'Odd'.

  2. What does the `select` command in `bash` provide?

    1. A way to indefinitely loop without a condition.
    2. A mechanism to create interactive menus.
    3. A method to process command-line arguments.
    4. A way to redirect output to a file.
    Show answer

    Answer: B. A mechanism to create interactive menus.

    The `select` command is used to present a numbered list of options to the user, allowing them to make a selection interactively. It's essentially a loop that presents a menu.

  3. Consider this `bash` code: `for num in 10 20 30; do echo $num; if [ $num -eq 20 ]; then echo "Found 20"; fi; done`. What is the output?

    1. 10 20 Found 20 30
    2. 10 20 30 Found 20
    3. 10 20 30
    4. 10 Found 20 20 30
    Show answer

    Answer: A. 10 20 Found 20 30

    The loop iterates through 10, 20, and 30. First, it prints 10. Then, it checks if 10 is equal to 20, which is false. Next, it prints 20. It checks if 20 is equal to 20, which is true, so it prints 'Found 20'. Finally, it prints 30. The check for 30 being equal to 20 is false.

  4. What is the purpose of `timeout <seconds> <command>` in `bash`?

    1. To execute a command only if it finishes within the specified seconds.
    2. To run a command and terminate it if it exceeds the specified seconds.
    3. To set a delay before executing a command.
    4. To limit the CPU usage of a command.
    Show answer

    Answer: B. To run a command and terminate it if it exceeds the specified seconds.

    The `timeout` command is used to run another command with a time limit. If the command doesn't finish within the specified duration, `timeout` sends a termination signal to it.

  5. Consider the `bash` script: `for i in {1..5}; do if (( i % 3 == 0 )); then echo "Divisible by 3"; continue; fi; echo $i; done`. What is the output?

    1. 1 2 Divisible by 3 4 5
    2. 1 2 Divisible by 3 4 5 Divisible by 3
    3. 1 2 3 4 5
    4. Divisible by 3 Divisible by 3
    Show answer

    Answer: A. 1 2 Divisible by 3 4 5

    The loop iterates from 1 to 5. For `i=1`, it prints '1'. For `i=2`, it prints '2'. For `i=3`, the condition `(( i % 3 == 0 ))` is true, so it prints 'Divisible by 3' and `continue` skips to the next iteration. For `i=4`, it prints '4'. For `i=5`, it prints '5'. The final `i=6` would make the loop condition false. The output will be 1 2 Divisible by 3 4 5.

Take the full quiz

More quizzes

Browse all Shell quizzes