Unix Questions and Answers – Logical Operators and Conditional Execution

This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Logical Operators and Conditional Execution”.

1. Which of the following operators are used for logical execution?
a) ||
b) &&
c) %%
d) && and ||
View Answer

Answer: d
Explanation: The operators && and || are used for logical execution i.e. they act as AND, OR condition respectively. The syntax for using these operators is:

command1 && command2
command1 || command2

advertisement
advertisement

2. When we use &&, the second command is executed only when first succeeds.
a) True
b) False
View Answer

Answer: a
Explanation: The shell provides two operators that allow conditional execution, one of which is &&. It delimits two commands; the second command is executed only when the first command succeeds.

3. When we use ||, both the commands are executed.
a) True
b) False
View Answer

Answer: b
Explanation: || is an operator used by the shell for conditional execution. It plays an inverse role in which the second command is executed only when the first fails.
Note: Join free Sanfoundry classes at Telegram or Youtube

4. The syntax for using && is ______________
a) cmd1 && cmd2
b) cmd1 cmd2 &&
c) cmd1 & cmd2&
d) cmd1
View Answer

Answer: a
Explanation: The shell provides two operators that allow conditional execution –the && and ||. The syntax for using these operators is:

cmd1 && cmd2
cmd1 || cmd2

5. To perform decision depending on the fulfillment of certain criteria, ____ is used.
a) if
b) else
c) for
d) if and else
View Answer

Answer: d
Explanation: The if and else statement makes two-way decisions depending on the fulfillment of certain criteria. These statements are used in UNIX as they are used in other programming languages. For example,

advertisement
If command is successful
Then
    Execute command
Else
    Execute command
Fi

6. Every if is closed with a corresponding ____
a) else
b) fi
c) if
d) else if
View Answer

Answer: b
Explanation: if and else statements are used for making two-way decisions. Every if is closed with the corresponding fi. If we don’t do this we’ll encounter an error.
advertisement

7. To check more than two conditions, ___ is used with if-else statements.
a) while
b) for
c) elif
d) for
View Answer

Answer: c
Explanation: if-else statements makes two-way decisions depending on the fulfillment of certain conditions. But if we want to check more than two conditions we can use elif statement. For example,

If command is successful
Then
    Execute command
Elif  command is successful
    Then
    Execute command
Else. . .    
Fi

8. The name of the script is stored in which special parameter?
a) $1
b) $0
c) $#
d) $*
View Answer

Answer: b
Explanation: There are some special parameters used by the shell. One of which is $0 which stores the name of the program.

9. Which of the following is not a special parameter used by the shell?
a) $$
b) $*
c) $?
d) $-
View Answer

Answer: d
Explanation:

        $$ - displays PID of the current shell
        $* - displays the complete set of positional parameters as a single string
        $?  - displays exit status of the last command

10. To know the exit status of a command, we can use ____
a) $$
b) $*
c) $?
d) $-
View Answer

Answer: c
Explanation: The parameter $? stores the exit status of the last command. It displays the value if the command succeeds and a non zero value if it fails.

$ grep director.lst  emp.lst  >/dev/null ; echo $?
1                        // success

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.