Unit 10: Shell programming - Practice Quiz

CSE105 — Creative Engineering Workshop 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which line is conventionally placed at the very top of a shell script to specify the interpreter?

Structure of Shell Scripts Easy
A. #!/bin/bash
B. ## bin/bash
C. !#/bin/bash
D. // bin/bash

2 Which command makes a shell script named run.sh executable?

Creating and Executing Scripts Easy
A. chmod +x run.sh
B. make run.sh
C. run.sh +x
D. exec run.sh

3 In a shell while loop, the loop body executes as long as the condition is:

while loop Easy
A. False
B. True
C. Empty
D. Zero characters

4 What is the correct keyword used to end a for loop in shell scripting?

for loop Easy
A. end
B. stop
C. done
D. endfor

5 Which is a valid way to define a function named greet in bash?

functions Easy
A. func greet { echo hi }
B. def greet(): echo hi
C. function greet -> { echo hi }
D. greet() { echo hi; }

6 How do you access the first element of a bash array named arr?

arrays Easy
A. ${arr[0]}
B. $arr.first
C. ${arr(1)}
D. ${arr{0}}

7 Inside a shell script, which variable holds the first command-line argument?

Command-Line Arguments Easy
A. $1
B. $0
C. $@
D. $#

8 Which special variable gives the total number of command-line arguments passed to a script?

Command-Line Arguments Easy
A. $?
B. $!
C. $#
D. $*

9 Which command is used to read input from the user into a variable in a shell script?

Interactive Shell Scripts Easy
A. get
B. read
C. scan
D. input

10 Which keyword marks the end of an if statement in shell scripting?

if Easy
A. done
B. fi
C. endif
D. end

11 Which keyword introduces the alternative block in a shell if-else statement?

if-else Easy
A. else
B. elif
C. otherwise
D. elseif

12 What best describes a nested if in shell scripting?

nested if Easy
A. An if statement placed inside another if block
B. A case statement written as if
C. An if with multiple elif branches
D. Two if statements joined by &&

13 In shell scripting, [ ] is equivalent to which command?

test command and [] Easy
A. eval
B. test
C. expr
D. check

14 Which operator checks whether two integers are equal inside [ ]?

test command and [] Easy
A. -equal
B. ==
C. =:=
D. -eq

15 Which keyword ends a case statement in shell scripting?

case Statement Easy
A. fi
B. endcase
C. esac
D. done

16 Which operator is used for string equality comparison in [ ]?

Operators in Shell Scripting Easy
A. -eq
B. .eq
C. =
D. ==eq

17 Which logical operator represents AND in shell conditional expressions?

Operators in Shell Scripting Easy
A. ||
B. &
C. &&
D. !!

18 Which syntax correctly declares an array in bash with three values?

arrays Easy
A. arr={one two three}
B. arr=(one two three)
C. array arr(one two three)
D. arr = [one, two, three]

19 How is a value typically returned as output from a shell function to be captured by the caller?

functions Easy
A. Using the return value keyword
B. Using yield
C. Using echo and command substitution
D. Using output

20 Which keyword pair correctly begins and ends the body of a while loop in shell?

while loop Easy
A. begin ... end
B. do ... done
C. then ... fi
D. 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

while loop Medium
A. 1 2 3 4
B. 0 1 2 3
C. 1 2 3
D. 1 2

22 Which for loop correctly iterates over the numbers 1 to 5 using C-style syntax in Bash?

for loop Medium
A. for ((i=1; i<=5; i++)); do echo $i; done
B. for i in (1..5); do echo $i; done
C. for i=1 to 5; do echo $i; done
D. 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

functions Medium
A. The return value of the function
B. The first argument passed to the function
C. The name of the script
D. The first argument passed to the script

24 Given arr=(apple banana cherry), what does ${#arr[@]} return?

arrays Medium
A. 0
B. cherry
C. apple
D. 3

25 How do you access the second element of a Bash array named colors?

arrays Medium
A. $colors[2]
B. ${colors(1)}
C. ${colors[2]}
D. ${colors[1]}

26 What is the purpose of the line #!/bin/bash at the top of a shell script?

Structure of Shell Scripts Medium
A. It specifies the interpreter used to execute the script
B. It marks the beginning of the main function
C. It is a comment that is ignored by the shell
D. It imports the bash library into the script

27 Which command makes a script file run.sh executable so it can be run with ./run.sh?

Creating and Executing Scripts Medium
A. chmod +x run.sh
B. chown +x run.sh
C. exec run.sh
D. chmod +r run.sh

28 Which command reads a line of input from the user and stores it in a variable named name?

Interactive Shell Scripts Medium
A. scan name
B. get name
C. input name
D. read name

29 If a script is run as ./script.sh a b c, what value does $# hold?

Command-Line Arguments Medium
A. a
B. c
C. 4
D. 3

30 What does $@ represent in a shell script?

Command-Line Arguments Medium
A. The total count of arguments
B. All command-line arguments as separate words
C. The process ID of the shell
D. The exit status of the last command

31 Which operator is used to test whether two integers are equal in a test/[ ] expression?

Operators in Shell Scripting Medium
A. -equal
B. -eq
C. =
D. ==

32 What is the result of the arithmetic expansion echo $((7 % 3))?

Operators in Shell Scripting Medium
A. 2
B. 1
C. 0
D. 3

33 What will the following script print?
bash
x=10
if [ $x -gt 5 ]
then
echo "big"
fi

if Medium
A. Nothing
B. 10
C. big
D. An error message

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

if-else Medium
A. even
B. Nothing is printed
C. odd
D. 4

35 In a nested if structure, when is the innermost block executed?

nested if Medium
A. Always, regardless of outer conditions
B. Only when the outer condition is false
C. Whenever the innermost condition alone is true
D. Only when all enclosing outer conditions are also true

36 Which expression correctly checks whether the file data.txt exists and is a regular file?

test command and [ ] Medium
A. [ -f data.txt ]
B. [ -e -f data.txt ]
C. [ -d data.txt ]
D. [ -x data.txt ]

37 Why is it important to put spaces inside the brackets, as in [ $a -lt $b ]?

test command and [ ] Medium
A. Because spaces convert the numbers into strings
B. Because without spaces the loop runs infinitely
C. Because Bash automatically removes brackets without spaces
D. Because [ 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

case Statement Medium
A. B
B. A
C. banana
D. other

39 In a case statement, what role does the *) pattern serve?

case Statement Medium
A. It matches numeric values only
B. It terminates the case statement immediately
C. It matches only empty strings
D. It acts as a default case that matches any value not matched earlier

40 How can a shell function return an exit status that the caller can check with $??

functions Medium
A. Using the exit keyword which only stops the function
B. Using the return statement with a numeric value between 0 and 255
C. Assigning the value to a variable named $?
D. Using the 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?

Command-Line Arguments Hard
A. Unquoted $* preserves boundaries while unquoted $@ joins them
B. Both are invalid unless quoted, causing a syntax error
C. When unquoted, both $@ and $* behave identically, undergoing word splitting on IFS
D. Unquoted $@ 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

test command and [] Hard
A. Prints yes
B. Prints no
C. A syntax error: [: =: unary operator expected
D. Prints an empty line

43 Given arr=(10 20 30 40), what does ${arr[@]:1:2} evaluate to?

arrays Hard
A. 20 30
B. 20 30 40
C. 10 20
D. 30 40

44 What is the result of echo $(( 5 & 3 | 2 )) in Bash arithmetic?

Operators in Shell Scripting Hard
A. 5
B. 3
C. 7
D. 1

45 What does this C-style loop print?
bash
for ((i=0; i<3; i++)); do
echo -n "$i"
done

for loop Hard
A. 0 1 2
B. 0123
C. 123
D. 012

46 In the following, what does the script print?
bash
f() { local x=5; g; }
g() { echo $x; }
x=99
f

functions Hard
A. 99
B. Empty line
C. A syntax error about undefined x
D. 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 loop Hard
A. The pipe buffers all input, skipping the loop body
B. The while runs in a subshell, so count changes don't persist to the parent
C. read resets count to 0 on each iteration
D. $((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

case Statement Hard
A. Prints three
B. Prints one
C. Prints one then two
D. Prints 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

if-else Hard
A. The exit status of echo found
B. Whether file.txt exists, regardless of grep
C. The exit status of grep, where 0 means match found
D. The number of lines matched by 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

nested if Hard
A. Fizz then Buzz
B. Fizz
C. Buzz
D. FizzBuzz

51 Why is the shebang #!/usr/bin/env bash often preferred over #!/bin/bash?

Structure of Shell Scripts Hard
A. It locates bash via PATH, improving portability across systems
B. It prevents the script from inheriting environment variables
C. It forces the script to run in POSIX-compliant mode
D. It runs the script faster by skipping environment setup

52 Given declare -A m; m[x]=1; m[y]=2, what does echo ${#m[@]} output?

arrays Hard
A. 1
B. An error, since # cannot count associative arrays
C. 0
D. 2

53 What does [[ "file.txt" == *.txt ]] && echo match output, and why?

Operators in Shell Scripting Hard
A. match, because [[ ]] performs pattern matching with unquoted *.txt
B. An error, because * must be escaped inside [[ ]]
C. match, but only because .txt is a literal regex
D. Nothing, because == requires exact string equality

54 After running ./script.sh a b c with shift 2 at the top, what does echo $1 $# print?

Command-Line Arguments Hard
A. c 3
B. c 1
C. b 2
D. a 3

55 What is the effect of read -r -t 5 -p "Name: " name if the user types nothing for 6 seconds?

Interactive Shell Scripts Hard
A. read returns non-zero after 5s, leaving name unset/empty
B. read waits indefinitely, ignoring -t
C. read sets name to the string timeout
D. 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?

Creating and Executing Scripts Hard
A. The shebang line is missing a newline
B. The execute permission bit is not set on the file
C. The file is owned by root
D. The script uses bash-only syntax

57 What does this script output?
bash
compute() { return 7; }
compute
echo $?

functions Hard
A. Empty line
B. An error, since functions cannot return integers
C. 0
D. 7

58 What does the following print?
bash
i=5
while (( i-- )); do
echo -n "$i "
done

while loop Hard
A. 5 4 3 2 1 0
B. 4 3 2 1
C. 4 3 2 1 0
D. 5 4 3 2 1

59 Which comparison correctly tests whether the integer in $n is greater than 10 in a POSIX [ ] test?

test command and [] Hard
A. [ "$n" -gt 10 ]
B. [ "$n" > 10 ]
C. [ "$n" gt 10 ]
D. [ "$n" >> 10 ]

60 What is the behavior of if [ -n "$var" ] && [ -f "$var" ]; then echo ok; fi?

if Hard
A. Prints ok only if $var is non-empty and names an existing regular file
B. Prints ok if $var is non-empty or is a regular file
C. Errors because two [ ] tests cannot be combined with &&
D. Always prints ok when $var is set to any value