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.
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
2. Every pattern in case statement in terminated with a _____
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.
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.
4. The ___ option in case statement matches any option not matched by the previous options.
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,
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.
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:
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.
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?
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.
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
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.
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?
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) . .\)
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.
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
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.