Unix Questions and Answers – Case and Expr Command

This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Case and Expr Command”.

1. ____ statement matches an expression for more than one alternative.
a) for
b) while
c) elif
d) case
View Answer

Answer: d
Explanation: The case statement is the second conditional offered by the shell. This statement matches an expression for more than one alternative. case statements are used in menu-driven programs. The syntax of the case statement is:
case expression in

   pattern1) commands1 ;;
   pattern2) commands2 ;;
   . . . . .
esac

advertisement
advertisement

2. Every pattern in case statement in terminated with a _____
a) ;
b) :
c) ;;
d) //
View Answer

Answer: c
Explanation: case statement matches an expression for more than one alternative. Every pattern in the case statement is terminated with a ;; .

case expression in
   pattern1) commands1 ;;
   pattern2) commands2 ;;
   . . . . .
esac

3. case statement should have a corresponding closing esac.
a) True
b) False
View Answer

Answer: a
Explanation: Alike every if statement is closed with a fi, case statement should also be closed with a corresponding esac. Without it, we’ll encounter an error.
Note: Join free Sanfoundry classes at Telegram or Youtube

4. The ___ option in case statement matches any option not matched by the previous options.
a) ^
b) $
c) *
d) //
View Answer

Answer: c
Explanation: The last option (*) or the default option is used for matching any option which is not matched by any of the previously specified options. For example,

advertisement
case “$choice” in
   1)  ls  -l ;;
   2)  ps -f ;;
   3) who
   *) echo “invalid option” 
esac

5. case can also be used for matching multiple patterns.
a) True
b) False
View Answer

Answer: a
Explanation: case statement can also specify the same action for more than one pattern. Suppose, a programmer wants to develop a logic for both Y or y (or N or n), then he/she can make use of the case statement in the following manner:

advertisement
echo “do you wish to continue? (y/n) : ”
read answer
case  “$answer” in
    y|Y) ;;            //NULL statement, no action to be performed 
    n|N) exit ;;

6. case can also use wildcards to pattern matching.
a) True
b) False
View Answer

Answer: a
Explanation: case statement has superb string matching feature that uses wildcards. It uses the filename matching meta-characters *, ? and the character class for matching strings.

7. Which command is used for computation and string handling?
a) expr
b) case
c) if
d) read
View Answer

Answer: a
Explanation: The bourne shell can check whether an integer is greater than another or not, but it doesn’t have any computing features at all. It has to rely totally on expr command for that purpose. This command performs two functions:
• Performs arithmetic operations on integers
• Manipulates strings

8. expr can perform ____ arithmetic operations.
a) 2
b) 4
c) 5
d) 3
View Answer

Answer: c
Explanation: expr command can perform four basic arithmetic operations as well as the modulus function. For example,

$ x=5  y=10
$ expr $x + $y
$ expr $x - $y
$ expr $x /* $y        // asterisk has to escaped
$ expr $y / $x
$ expr $y % $x

9. Which of the following is performed by expr string handling’s function?
a) determine the length of string
b) extract a substring
c) locate the position of a character in a string
d) determine the length of string, extract and locate the position of the string
View Answer

Answer: d
Explanation: Though expr’s string handling features are not that elegant, bourne shell users hardly have any choice. For evaluating strings expr uses two expressions separated by a colon.

10. The string to be worked upon is placed on the left of the colon when using expr string handling function.
a) True
b) False
View Answer

Answer: a
Explanation: For evaluating strings expr uses two expressions separated by a colon. The string to be worked upon is placed on the left of the colon while a regular expression is placed on its right.

11. Which symbol is used for finding the length of the string?
a) .
b) *
c) .*
d) .*.
View Answer

Answer: c
Explanation: The length of the string can be extracted using .* . This regular expression signifies to expr that it has to display the number of characters matching the pattern i.e. the length of the entire string. For example,

$ expr “sanfoundry” :  ‘.*’
10

12. Which of the following pattern is used for extracting a substring using expr?
a) /( )
b) \(. .\)
c) . .\)
d) \\(
View Answer

Answer: b
Explanation: expr can extract a substring enclosed by the escaped characters \ ( and \). For example, to extract ‘03’ from the string 2003 use the following command:

$ str= 2003
$ expr “$str” :  ‘. .\(. .\)’
03

13. For locating the first position of a character in a string we can use expr command.
a) True
b) False
View Answer

Answer: a
Explanation: expr command can also locate the first occurrence of character inside a string. For example, to locate the position of character d in the string ‘abdullah’, we have to count the number of characters which are not ‘d’ ([^d]*),

$ str=abdullah ; expr “$str”  :  ‘[^d]*d’
3

14. expr is a _____ command
a) internal
b) external
c) shell
d) derived
View Answer

Answer: b
Explanation: expr command is an external command.

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.