This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Using test and [ ] to Evaluate Expressions – 1”.
1. test command can be used to check which of the following?
a) Compare two numbers
b) Compare two strings
c) Check attributes of a file
d) All of the mentioned
View Answer
Explanation: When we use if to evaluate expressions, we need the test statement because the true or false values returned by expressions can’t be directly handled by if. The test uses certain operators to evaluate the condition on its right and returns either true or false exit status. test works in three ways:
• compares two numbers
• compare two strings or a single one for a null value
• check a file’s attributes
2. Which of the following operators is used with test for comparison of numeric values?
a) -eq
b) -ne
c) -gg
d) –eq and -ne
View Answer
Explanation: There are some comparison operators which are used by test. For example,
-eq - equal to -ne - not equal to
3. We can use comparison operators without a ‘-‘.
a) True
b) False
View Answer
Explanation: Every numerical comparison operator used by the test begin a hyphen, followed by a two letter string and enclosed by a whitespace on either side.
4. ___ implies greater than and ____ implies less than.
a) gt, le
b) gt, lt
c) ge,le
d) ge,lt
View Answer
Explanation: There are some comparison operators which are used by test. For example,
gt implies greater than, lt implies less than, le implies less than or equal to, ge implies greater than or equal to.
5. Which of the following operator is used as a shorthand for test?
a) % %
b) [ ]
c) & &
d) ( )
View Answer
Explanation: UNIX provides a shorthand for test i.e. [ ]. We can use this pair of rectangular brackets enclosing the expression. Thus the following two are equal,
test $x -eq $y [ $x -eq $y ]
6. It is essential to use whitespaces when we use [].
a) True
b) False
View Answer
Explanation: We must provide whitespaces around the operators (like -eq), their operands (like $x) and inside the [ and ].
7. test and [ ] can be used for string comparison.
a) True
b) False
View Answer
Explanation: test can be used to compare strings with another set of operators (like = for equality of strings and != for inequality). For example, [ ! -z $string ] negates [ -z $string ].
8. Which one of the following option is used for AND operation in test command?
a) -o
b) -a
c) -e
d) -an
View Answer
Explanation: test also permits the checking of more than one condition in the same line using -a (AND) operator. For example,
If [ -n “$fname -a -n “$lname” ]; then . . .
9. Which one of the following option is used for OR operation in test command?
a) -o
b) -a
c) -e
d) -an
View Answer
Explanation: test also permits the checking of more than one condition in the same line using -o (OR) operator. For example,
If [ -n “$fname -o -n “$lname” ] ; then . . .
10. Which one of the following option is used for checking that the string is not null?
a) -a
b) -o
c) -z
d) -n
View Answer
Explanation: test can be used to compare strings with another set of operators. -n is used for checking if the string is not null. For example,
If [ -n “$fname ] ; then echo “$fname”
Sanfoundry Global Education & Learning Series – Unix.
To practice all areas of Unix, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Computer Science Books
- Practice Computer Science MCQs
- Check MCA Books
- Practice MCA MCQs
- Apply for Computer Science Internship