MCQs on Decision Making in Scripts | Shell Scripting

Mastering decision-making in scripts is essential for building dynamic and responsive applications. This chapter covers conditional statements, logical operators, and the test command to help manage flow control effectively in scripting languages.


1. Conditional Statements: if, else, elif

  1. What is the correct syntax for an if statement in most scripting languages?
    a) if condition { }
    b) if (condition)
    c) if condition:
    d) if {condition}
  2. How does an else if condition differ from an if statement?
    a) It is used to check multiple conditions
    b) It runs only if the previous if condition fails
    c) It has a different syntax than if
    d) There is no difference
  3. Which of the following is the correct syntax for an elif statement?
    a) elif condition {}
    b) elif (condition)
    c) elif condition:
    d) else if condition:
  4. What happens if no if condition is true and there is no else block?
    a) The script terminates
    b) The script skips the block entirely
    c) The script throws an error
    d) The else block will be executed automatically
  5. In which of the following cases is elif used?
    a) When only one condition needs to be checked
    b) When multiple conditions need to be checked in a sequence
    c) When the if condition fails
    d) When the else block is not sufficient
  6. What is the output when no condition is met and there is an else statement?
    a) No output
    b) The output of the else block is executed
    c) The script fails
    d) The if block is executed instead
  7. How many elif statements can be used in a script?
    a) None
    b) Only one
    c) As many as needed
    d) A maximum of two
  8. Which of the following options will always run regardless of conditions?
    a) else
    b) if
    c) elif
    d) None of the above
  9. What is the purpose of an else statement in a script?
    a) To handle any condition not covered by if
    b) To handle a specific condition
    c) To repeat the if condition
    d) To terminate the script
  10. Which of the following is NOT a valid way to end a conditional block in most scripting languages?
    a) endif
    b) Closing curly brace }
    c) fi
    d) End of the block without additional syntax

2. Logical Operators (&&, ||, !)

  1. What does the logical operator && represent?
    a) OR
    b) AND
    c) NOT
    d) XOR
  2. Which logical operator is used to negate a condition?
    a) !
    b) &&
    c) ||
    d) not
  3. When does the logical operator || return true?
    a) When both conditions are false
    b) When both conditions are true
    c) When at least one condition is true
    d) When at least one condition is false
  4. Which of the following is the correct use of the && operator?
    a) if (a && b)
    b) if a && b
    c) if (a || b)
    d) if (a && b == true)
  5. What is the result of false && true in a conditional expression?
    a) true
    b) false
    c) undefined
    d) null
  6. Which of the following would be the correct way to check if a variable is not equal to 0 using logical operators?
    a) if (x != 0)
    b) if (x ! 0)
    c) if (!(x == 0))
    d) if (!x == 0)
  7. What does the condition !(a && b) evaluate to?
    a) If either a or b is false, the result is false
    b) It negates the outcome of the AND operation
    c) It always returns true
    d) It checks if both a and b are true
  8. When can the || operator be used in conditional statements?
    a) When checking if both conditions are true
    b) When at least one of the conditions needs to be true
    c) When checking if neither condition is true
    d) None of the above
  9. Which of the following conditions is equivalent to !(x == 1)?
    a) x != 1
    b) x == 1
    c) x < 1
    d) x > 1
  10. What happens if you combine && and || in a single conditional expression?
    a) They work independently
    b) The && operation takes precedence
    c) The || operation takes precedence
    d) Both operators are evaluated in the same order

3. Test Command [ ] and [[ ]]

  1. What is the primary purpose of the test command [ ] in scripting languages?
    a) To perform calculations
    b) To check conditions in scripts
    c) To handle errors
    d) To execute commands
  2. Which of the following is the correct syntax for using the test command to check if a file exists?
    a) [ -f file.txt ]
    b) [ file.txt ]
    c) test -e file.txt
    d) test [ file.txt ]
  3. What is the difference between [ and [[ in scripting?
    a) [ is used for logical operations, and [[ is used for numeric operations
    b) [ is a command, and [[ is an internal keyword for advanced tests
    c) [[ is only used for string comparison
    d) There is no difference
  4. Which of the following tests can be performed with the test command [ ]?
    a) String comparisons
    b) File tests
    c) Numeric comparisons
    d) All of the above
  5. How do you check if a variable is not empty using [ ] in a script?
    a) [ -z $var ]
    b) [ ! -z $var ]
    c) [ -n $var ]
    d) [ $var ]
  6. What is the result of the test command [ -f myfile.txt ]?
    a) Returns true if the file exists
    b) Returns true if the file is a directory
    c) Always returns true
    d) Always returns false
  7. Which of the following is not a valid operator inside [[ ]] for string comparison?
    a) ==
    b) !=
    c) =~
    d) =>
  8. What does [[ -d directory ]] test for?
    a) Whether a directory exists
    b) Whether a file exists
    c) Whether the directory is empty
    d) Whether a file is a directory
  9. How do you test if a variable is equal to a specific string using [[ ]]?
    a) [[ $var == "text" ]]
    b) [[ $var = "text" ]]
    c) [ $var == "text" ]
    d) [ $var = "text" ]
  10. What is the output when [[ 5 -gt 3 ]] is used in a script?
    a) True
    b) False
    c) Error
    d) Nothing

Answers Table

QNoAnswer (Option with Text)
1b) if (condition)
2b) It runs only if the previous if condition fails
3c) elif condition:
4b) The script skips the block entirely
5b) When multiple conditions need to be checked in a sequence
6b) The output of the else block is executed
7c) As many as needed
8a) else
9a) To handle any condition not covered by if
10c) fi
11b) AND
12a) !
13c) When at least one condition is true
14a) if (a && b)
15b) false
16a) if (x != 0)
17b) It negates the outcome of the AND operation
18b) When at least one of the conditions needs to be true
19a) x != 1
20b) The && operation takes precedence
21b) To check conditions in scripts
22a) [ -f file.txt ]
23b) [ is a command, and [[ is an internal keyword for advanced tests
24d) All of the above
25b) [ ! -z $var ]
26a) Returns true if the file exists
27d) =>
28a) Whether a directory exists
29a) [[ $var == "text" ]]
30a) True

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