Linux Bash Shell Questions & Answers on Arithmetic Expressions for Freshers

https://www.computersprofessor.com/2018/07/linux-bash-shell-questions-answers-on.html
1. Which built-in command performs integer arithmetic in bash shell?
a) let
b) get
c) set
d) none of the mentioned
Answer: a
2. Which expression use the value of the enclosed arithmetic expression?
a) $(())
b) $()
c) ${}
d) $[].
Answer: a
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
a) b = a << b
b) a = a << b
c) b = b << a
d) a = a << b
Answer: b
4. Which one of the following is bitwise ‘exclusive or’ operator?
a) ^=
b) |=
c) !=
d) none of the mentioned
Answer: a
5. Which one of the following is not a valid operator in bash shell?
a) ||
b) ~
c) =<<
d) -=
Answer: c
6. What is the output of this program?
#!/bin/bash
a=2
b=4
let c=a**b
echo $c
exit 0
a) 8
b) 16
c) 32
d) none of the mentioned
b) 16
c) 32
d) none of the mentioned
Answer: b
Explanation:’**’ is the exponentation operator in bash shell.
Output:
root@ubuntu:/home/sanfoundry#./test.sh
16
root@ubuntu:/home/sanfoundry#
7. What is the output of this program?
#!/bin/bash
a=10; b=20
c=$((++a))
let a=c+a
echo $a
exit 0
a) 21
b) 22
c) program will generate an error message
d) none of the mentioned
b) 22
c) program will generate an error message
d) none of the mentioned
Answer: b
Output:
root@ubuntu:/home/sanfoundry#./test.sh
22
root@ubuntu:/home/sanfoundry#
Output:
root@ubuntu:/home/sanfoundry#./test.sh
22
root@ubuntu:/home/sanfoundry#
8. What is the output of this program?
#!/bin/bash
a=10
b=$(( $a<0?10:$a<100 ))
echo $b
exit 0
a) 10
b) 20
c) 1
d) 0
b) 20
c) 1
d) 0
Answer: c
Explanation: Firstly the ‘$a<0 1.="" and="" because="" been="" checked.="" checked="" colon="" condition="" div="" false="" hand="" has="" hence="" is="" it="" of="" output="" program="" right="" side="" so="" the="" this="" true="">
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
1
root@ubuntu:/home/sanfoundry#
9. What is the output of this program?
#!/bin/bash
a=10
b=$(( $a<0&&$a<100 ))
echo $b
exit 0
a) 10
b) 0
c) 1
d) none of the mentioned
b) 0
c) 1
d) none of the mentioned
Answer: b
Explanation: The condition ‘$a<0 0.="" and="" div="" false="" is="" logical="" operator="" output="" provides="" so="" the="">
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
0
root@ubuntu:/home/sanfoundry#
10. What is the output of this program?
#!/bin/bash
a=1; b=2; c=3
d=$(( ++a**b*c++ + a ))
echo $d
exit 0
a) 14
b) 12
c) program will generate an error message
d) none of the mentioned
b) 12
c) program will generate an error message
d) none of the mentioned
Answer: a
Explanation: The operators in decreasing order of precedence are ++, **, *, +.
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
14
root@ubuntu:/home/sanfoundry#