Unit 2 - Practice Quiz

CSE325

1 Which special character combination, known as the shebang, is placed at the very first line of a shell script to indicate the interpreter?

A. //
B. #!
C. \
D. ##

2 Which command is used to grant execute permission to a shell script file named myscript.sh?

A. chmod +r myscript.sh
B. chmod +w myscript.sh
C. chmod +x myscript.sh
D. chown +x myscript.sh

3 What is the correct syntax to assign the value 10 to a variable named count in bash?

A. count = 10
B. count=10
C. $count=10
D. int count = 10

4 Which command is used to print the current value of the environment variable PATH?

A. print PATH
B. ls PATH
C. echo $PATH
D. cat $PATH

5 When using the expr command for multiplication, why must the asterisk be escaped (e.g., expr 5 \* 3)?

A. Because expr does not support multiplication
B. Because * acts as a wildcard for file matching in the shell
C. Because * is a reserved keyword for pointers
D. Because it is a division operator in bash

6 Which utility is preferred for performing floating-point arithmetic in shell scripts?

A. expr
B. let
C. bc
D. float

7 How do you define an arithmetic operation involving variables and using double parentheses?

A. $(( a + b ))
B. (( b ))
C. [[ a + b ]]
D. { a + b }

8 What is the output of the following command?
echo "scale=2; 10/3" | bc

A. 3
B. 3.33
C. 3.333
D. 3.00

9 Which operator is used to redirect standard output to a file, overwriting the file if it exists?

A. >>
B. <
C. >
D. |

10 What does the >> operator do?

A. Redirects input from a file
B. Redirects output and overwrites the file
C. Redirects output and appends to the file
D. Pipes output to another command

11 Which operator creates a pipe, sending the standard output of the first command as standard input to the second command?

A. &
B. |
C. >
D. ;

12 Which syntax correctly redirects standard error (stderr) to a file named error.log?

A. command > error.log
B. command 2> error.log
C. command &> error.log
D. command | error.log

13 Which command allows the user to input data into a variable during script execution?

A. input
B. get
C. scan
D. read

14 In a conditional statement, which flag checks if a file exists and is a regular file?

A. -d
B. -r
C. -f
D. -s

15 Which operator is used for numeric equality comparison in a [ ] test construct?

A. =
B. ==
C. -eq
D. -equal

16 What is the correct syntax for an if-else block in a shell script?

A. if [ condition ] { ... } else { ... }
B. if [ condition ]; then ... else ... fi
C. if (condition) ... else ... end
D. check [ condition ] ... otherwise ... done

17 Which special variable holds the exit status of the last executed command?

A. $$
B. $#
C. $?
D. $!

18 Which construct is best suited for creating a menu-driven program where a user selects a choice matching a pattern?

A. for
B. while
C. case
D. until

19 How is a case statement block terminated in bash?

A. endcase
B. esac
C. done
D. fi

20 Inside a case statement, what signifies the end of a specific pattern block?

A. ;
B. ;;
C. break
D. stop

21 Which loop construct iterates as long as the condition is true?

A. until
B. for
C. while
D. case

22 Which loop construct iterates as long as the condition is false (i.e., until it becomes true)?

A. while
B. do-while
C. until
D. for

23 What is the correct syntax for a C-style for loop in bash?

A. for (i=0; i<5; i++)
B. for (( i=0; i<5; i++ ))
C. loop [i=0, i<5, i++]
D. for i in {0..5}

24 How do you access all arguments passed to a script simultaneously?

A. $1
B. $#
C. $@
D. $?

25 Which command is used to terminate a loop prematurely?

A. exit
B. stop
C. continue
D. break

26 Which command skips the remainder of the current loop iteration and moves to the next one?

A. pass
B. next
C. continue
D. skip

27 How do you define an indexed array named fruits with elements "Apple" and "Banana"?

A. fruits = {"Apple", "Banana"}
B. fruits=("Apple" "Banana")
C. fruits["Apple", "Banana"]
D. array fruits = "Apple", "Banana"

28 How do you access the first element of an indexed array named arr?

A. $arr[0]
B. ${arr[0]}
C. $arr{0}
D. arr[0]

29 Which syntax returns the total number of elements (length) in an array named arr?

A. ${#arr}
B. ${#arr[@]}
C. ${arr[*]}
D. length(arr)

30 What is required to declare an associative array (key-value pair) in bash?

A. declare -a myMap
B. declare -A myMap
C. array -assoc myMap
D. myMap={}

31 How do you define a function named myFunc in bash?

A. function myFunc() { ... }
B. def myFunc: ...
C. void myFunc() { ... }
D. func myFunc { ... }

32 Inside a user-defined function, how do you access the first argument passed specifically to that function?

A. $0
B. $1
C. $arg1
D. ${args[0]}

33 How do you make a variable x accessible only within the function it is defined in?

A. private x=1
B. local x=1
C. var x=1
D. declare -p x=1

34 What is the output of echo $(( 5 % 2 ))?

A. 2.5
B. 2
C. 1
D. 5

35 Which special variable represents the Process ID (PID) of the current script?

A. $PID
B. $!
C. $$
D. $?

36 What command is used to read input from a menu selection loop nicely formatted with numbers?

A. case
B. select
C. choose
D. option

37 What does the command set -x do in a shell script?

A. Sets the execute permission
B. Enables debugging mode (prints commands as they execute)
C. Exits the script immediately on error
D. Exports all variables

38 Which logical operator represents a logical AND inside [ ] brackets?

A. &&
B. -a
C. -and
D. &

39 Which logical operator represents a logical OR inside [ ] brackets?

A. ||
B. -o
C. -or
D. |

40 What happens if you run a script as . script.sh (dot space script)?

A. It executes in a new subshell
B. It executes in the current shell context (sourcing)
C. It checks syntax without executing
D. It runs the script in the background

41 Which command substitution syntax is modern and preferred over backticks `...`?

A. ${ command }
B. $( command )
C. (( command ))
D. [ command ]

42 What will echo "Value: $HOME" print?

A. Value: $HOME
B. Value: /home/username
C. Value:
D. Error

43 What will echo 'Value: $HOME' print?

A. Value: $HOME
B. Value: /home/username
C. Value:
D. Error

44 Which test operator checks if a string is empty (zero length)?

A. -z
B. -n
C. -e
D. -s

45 Which command is used to shift positional parameters (e.g., make 1)?

A. move
B. shift
C. next
D. rotate

46 How do you check if a variable $num is NOT equal to 10 in a conditional [ ]?

A. [ $num != 10 ]
B. [ $num -ne 10 ]
C. [ $num <> 10 ]
D. [ $num ! 10 ]

47 To iterate through a list of files in the current directory ending in .txt, which for loop head is appropriate?

A. for file in *.txt
B. for file in ls *.txt
C. for file in "*.txt"
D. foreach file in *.txt

48 What is the result of echo $(( 2**3 )) in bash?

A. 6
B. 8
C. 5
D. Error

49 Which output redirection allows you to redirect output to null (discard it completely)?

A. > /dev/null
B. > /dev/void
C. > /dev/empty
D. > /dev/zero

50 What is the primary difference between * when enclosed in double quotes?

A. There is no difference
B. "*" treats them as a single string
C. "*" treats them as separate strings
D. "$@" expands to the count of arguments