Linux Bash Shell Questions & Answers – Arithmetic Expressions

This set of Linux / Unix questions and answers focuses on Arithmetic Expression in Linux Bash Shell Programming.

1. Which built-in command performs integer arithmetic in bash shell?
a) let
b) get
c) set
d) none of the mentioned
View Answer

Answer: a
Explanation: None.

2. Which expression use the value of the enclosed arithmetic expression?
a) $(())
b) $()
c) ${}
d) $[].
View Answer

Answer: a
Explanation: None.

3. If a and b are 2 variables then the meaning of a<<=b is
a) b = a << b
b) a = a << b
c) b = b << a
d) a = a << b
View Answer

Answer: b
Explanation: None.

4. Which one of the following is bitwise ‘exclusive or’ operator?
a) ^=
b) |=
c) !=
d) none of the mentioned
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

5. Which one of the following is not a valid operator in bash shell?
a) ||
b) ~
c) =<<
d) -=
View Answer

Answer: c
Explanation: None.

6. What is the output of this program?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1.    #!/bin/bash
  2.    a=2
  3.    b=4
  4.    let c=a**b
  5.    echo $c
  6.    exit 0

a) 8
b) 16
c) 32
d) none of the mentioned
View Answer

Answer: b
Explanation:’**’ is the exponentation operator in bash shell.
Output:
root@ubuntu:/home/sanfoundry#./test.sh
16
root@ubuntu:/home/sanfoundry#
advertisement

7. What is the output of this program?

  1.    #!/bin/bash
  2.    a=10; b=20
  3.    c=$((++a))
  4.    let a=c+a
  5.    echo $a
  6.    exit 0

a) 21
b) 22
c) program will generate an error message
d) none of the mentioned
View Answer

Answer: b
Explanation: None.
Output:
root@ubuntu:/home/sanfoundry#./test.sh
22
root@ubuntu:/home/sanfoundry#
advertisement

8. What is the output of this program?

  1.    #!/bin/bash
  2.    a=10
  3.    b=$(( $a<0?10:$a<100 ))
  4.    echo $b
  5.    exit 0

a) 10
b) 20
c) 1
d) 0
View Answer

Answer: c
Explanation: Firstly the ‘$a<0’ condition has been checked. Because it is false hence the right hand side condition of the colon (:) has been checked and this is true so program output is 1.
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
1
root@ubuntu:/home/sanfoundry#

9. What is the output of this program?

  1.    #!/bin/bash
  2.    a=10
  3.    b=$(( $a<0&&$a<100 ))
  4.    echo $b
  5.    exit 0

a) 10
b) 0
c) 1
d) none of the mentioned
View Answer

Answer: b
Explanation: The condition ‘$a<0’ is false so logical and operator provides the output 0.
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
0
root@ubuntu:/home/sanfoundry#

10. What is the output of this program?

  1.     #!/bin/bash
  2.     a=1; b=2; c=3
  3.     d=$(( ++a**b*c++ + a ))
  4.     echo $d
  5.     exit 0

a) 14
b) 12
c) program will generate an error message
d) none of the mentioned
View Answer

Answer: a
Explanation: The operators in decreasing order of precedence are ++, **, *, +.
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
14
root@ubuntu:/home/sanfoundry#

Sanfoundry Global Education & Learning Series – Linux Administration & Programming.

Here’s the list of Best Books in Linux Commands & Shell Programming.
Here’s the list of Best Books in Linux Kernel, Device-Drivers & System Programming.

To practice all questions on Linux Administration & Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on Linux.

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.