Unix Questions and Answers – Using test and [ ] to Evaluate Expressions – 1

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

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

Answer: d
Explanation: There are some comparison operators which are used by test. For example,

-eq    -    equal to
-ne    -    not equal to

advertisement
advertisement

3. We can use comparison operators without a ‘-‘.
a) True
b) False
View Answer

Answer: b
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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

4. ___ implies greater than and ____ implies less than.
a) gt, le
b) gt, lt
c) ge,le
d) ge,lt
View Answer

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

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

advertisement
test $x  -eq  $y
[ $x  -eq  $y ]

advertisement

6. It is essential to use whitespaces when we use [].
a) True
b) False
View Answer

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

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

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

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

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

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.