Unit 10: Shell programming - Practice Quiz
1 Which line is conventionally placed at the very top of a shell script to specify the interpreter?
#!/bin/bash
## bin/bash
!#/bin/bash
// bin/bash
2
Which command makes a shell script named run.sh executable?
chmod +x run.sh
make run.sh
run.sh +x
exec run.sh
3
In a shell while loop, the loop body executes as long as the condition is:
4
What is the correct keyword used to end a for loop in shell scripting?
end
stop
done
endfor
5
Which is a valid way to define a function named greet in bash?
func greet { echo hi }
def greet(): echo hi
function greet -> { echo hi }
greet() { echo hi; }
6
How do you access the first element of a bash array named arr?
${arr[0]}
$arr.first
${arr(1)}
${arr{0}}
7 Inside a shell script, which variable holds the first command-line argument?
$1
$0
$@
$#
8 Which special variable gives the total number of command-line arguments passed to a script?
$?
$!
$#
$*
9 Which command is used to read input from the user into a variable in a shell script?
get
read
scan
input
10
Which keyword marks the end of an if statement in shell scripting?
done
fi
endif
end
11
Which keyword introduces the alternative block in a shell if-else statement?
else
elif
otherwise
elseif
12
What best describes a nested if in shell scripting?
if statement placed inside another if block
case statement written as if
if with multiple elif branches
if statements joined by &&
13
In shell scripting, [ ] is equivalent to which command?
eval
test
expr
check
14
Which operator checks whether two integers are equal inside [ ]?
-equal
==
=:=
-eq
15
Which keyword ends a case statement in shell scripting?
fi
endcase
esac
done
16
Which operator is used for string equality comparison in [ ]?
-eq
.eq
=
==eq
17 Which logical operator represents AND in shell conditional expressions?
||
&
&&
!!
18 Which syntax correctly declares an array in bash with three values?
arr={one two three}
arr=(one two three)
array arr(one two three)
arr = [one, two, three]
19 How is a value typically returned as output from a shell function to be captured by the caller?
return value keyword
yield
echo and command substitution
output
20
Which keyword pair correctly begins and ends the body of a while loop in shell?
begin ... end
do ... done
then ... fi
do ... end
21
What will be the output of the following shell script?
bash
i=1
while [ $i -le 3 ]
do
echo -n "$i "
i=$((i+1))
done
1 2 3 4
0 1 2 3
1 2 3
1 2
22
Which for loop correctly iterates over the numbers 1 to 5 using C-style syntax in Bash?
for ((i=1; i<=5; i++)); do echo $i; done
for i in (1..5); do echo $i; done
for i=1 to 5; do echo $i; done
for (i=1; i<=5; i++) do echo $i; done
23
In the script below, what does $1 refer to inside the function?
bash
greet() {
echo "Hello $1"
}
greet World
24
Given arr=(apple banana cherry), what does ${#arr[@]} return?
0
cherry
apple
3
25
How do you access the second element of a Bash array named colors?
$colors[2]
${colors(1)}
${colors[2]}
${colors[1]}
26
What is the purpose of the line #!/bin/bash at the top of a shell script?
27
Which command makes a script file run.sh executable so it can be run with ./run.sh?
chmod +x run.sh
chown +x run.sh
exec run.sh
chmod +r run.sh
28
Which command reads a line of input from the user and stores it in a variable named name?
scan name
get name
input name
read name
29
If a script is run as ./script.sh a b c, what value does $# hold?
a
c
4
3
30
What does $@ represent in a shell script?
31
Which operator is used to test whether two integers are equal in a test/[ ] expression?
-equal
-eq
=
==
32
What is the result of the arithmetic expansion echo $((7 % 3))?
2
1
0
3
33
What will the following script print?
bash
x=10
if [ $x -gt 5 ]
then
echo "big"
fi
10
big
34
Consider the script below. What is printed when the user enters 4?
bash
read n
if [ $((n % 2)) -eq 0 ]
then
echo "even"
else
echo "odd"
fi
even
odd
4
35
In a nested if structure, when is the innermost block executed?
36
Which expression correctly checks whether the file data.txt exists and is a regular file?
[ -f data.txt ]
[ -e -f data.txt ]
[ -d data.txt ]
[ -x data.txt ]
37
Why is it important to put spaces inside the brackets, as in [ $a -lt $b ]?
[ is a command and requires its arguments to be separated by spaces
38
What will the following script output when fruit=banana?
bash
case $fruit in
apple) echo "A";;
banana) echo "B";;
*) echo "other";;
esac
B
A
banana
other
39
In a case statement, what role does the *) pattern serve?
40
How can a shell function return an exit status that the caller can check with $??
exit keyword which only stops the function
return statement with a numeric value between 0 and 255
$?
echo statement to print the value
41
In a Bash script, what is the difference between $@ and $* when both are used unquoted in a for loop iterating over arguments containing spaces?
$* preserves boundaries while unquoted $@ joins them
$@ and $* behave identically, undergoing word splitting on IFS
$@ preserves argument boundaries while unquoted $* joins them
42
What is the output of the following?
bash
x=""
if [ $x = "a" ]; then echo yes; else echo no; fi
yes
no
[: =: unary operator expected
43
Given arr=(10 20 30 40), what does ${arr[@]:1:2} evaluate to?
20 30
20 30 40
10 20
30 40
44
What is the result of echo $(( 5 & 3 | 2 )) in Bash arithmetic?
5
3
7
1
45
What does this C-style loop print?
bash
for ((i=0; i<3; i++)); do
echo -n "$i"
done
0 1 2
0123
123
012
46
In the following, what does the script print?
bash
f() { local x=5; g; }
g() { echo $x; }
x=99
f
99
x
5
47
Why might the following pipeline print 0 instead of the expected line count?
bash
count=0
cat file.txt | while read line; do
count=$((count+1))
done
echo $count
while runs in a subshell, so count changes don't persist to the parent
read resets count to 0 on each iteration
$((count+1)) is invalid syntax inside a loop
48
Given x="apple", which pattern in a case statement matches?
bash
case $x in
ae) echo one;;
[aA]) echo two;;
*) echo three;;
esac
three
one
one then two
two
49
What does the exit status of the if condition depend on here?
bash
if grep -q "foo" file.txt; then echo found; else echo missing; fi
echo found
file.txt exists, regardless of grep
grep, where 0 means match found
grep
50
What is printed?
bash
n=15
if (( n % 3 == 0 )); then
if (( n % 5 == 0 )); then echo FizzBuzz
else echo Fizz; fi
elif (( n % 5 == 0 )); then echo Buzz
fi
Fizz then Buzz
Fizz
Buzz
FizzBuzz
51
Why is the shebang #!/usr/bin/env bash often preferred over #!/bin/bash?
bash via PATH, improving portability across systems
52
Given declare -A m; m[x]=1; m[y]=2, what does echo ${#m[@]} output?
1
# cannot count associative arrays
0
2
53
What does [[ "file.txt" == *.txt ]] && echo match output, and why?
match, because [[ ]] performs pattern matching with unquoted *.txt
* must be escaped inside [[ ]]
match, but only because .txt is a literal regex
== requires exact string equality
54
After running ./script.sh a b c with shift 2 at the top, what does echo $1 $# print?
c 3
c 1
b 2
a 3
55
What is the effect of read -r -t 5 -p "Name: " name if the user types nothing for 6 seconds?
read returns non-zero after 5s, leaving name unset/empty
read waits indefinitely, ignoring -t
read sets name to the string timeout
read returns 0 with name set to a newline
56
A script runs correctly with bash script.sh but fails with ./script.sh reporting Permission denied. What is the most likely cause?
bash-only syntax
57
What does this script output?
bash
compute() { return 7; }
compute
echo $?
0
7
58
What does the following print?
bash
i=5
while (( i-- )); do
echo -n "$i "
done
5 4 3 2 1 0
4 3 2 1
4 3 2 1 0
5 4 3 2 1
59
Which comparison correctly tests whether the integer in $n is greater than 10 in a POSIX [ ] test?
[ "$n" -gt 10 ]
[ "$n" > 10 ]
[ "$n" gt 10 ]
[ "$n" >> 10 ]
60
What is the behavior of if [ -n "$var" ] && [ -f "$var" ]; then echo ok; fi?
ok only if $var is non-empty and names an existing regular file
ok if $var is non-empty or is a regular file
[ ] tests cannot be combined with &&
ok when $var is set to any value
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →