250+ TOP MCQs on Using test and [ ] to Evaluate Expressions

Unix Multiple Choice Questions on “Using test and [ ] to Evaluate Expressions”.

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

Answer: d
Clarification: 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

Answer: d
Clarification: 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

Answer: b
Clarification: 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

Answer: b
Clarification: 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.

  250+ TOP MCQs on System Calls Basics and Answers

5. Which of the following operator is used as a shorthand for test?
a) % %
b) [ ]
c) & &
d) ( )

Answer: b
Clarification: 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

Answer: a
Clarification: 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

Answer: a
Clarification: 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

Answer: b
Clarification: 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

Answer: a
Clarification: 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

  250+ TOP MCQs on Administrator Specific Commands & Privileges

Answer: d
Clarification: 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”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top