1Which 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.##
Correct Answer: #!
Explanation:The shebang (#!), followed by the path to the interpreter (e.g., #!/bin/bash), tells the system which shell to use to execute the script.
Incorrect! Try again.
2Which 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
Correct Answer: chmod +x myscript.sh
Explanation:The chmod command modifies file permissions. The +x flag adds the execute permission.
Incorrect! Try again.
3What 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
Correct Answer: count=10
Explanation:In bash assignment, there must be no spaces around the = sign. The $ symbol is only used when accessing the variable, not when assigning to it.
Incorrect! Try again.
4Which 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
Correct Answer: echo $PATH
Explanation:echo prints text or variables to the standard output. To access the value of a variable, the $ prefix is required.
Incorrect! Try again.
5When 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
Correct Answer: Because * acts as a wildcard for file matching in the shell
Explanation:The shell interprets the asterisk * as a wildcard that matches filenames. To pass it as a literal multiplication operator to expr, it must be escaped using a backslash \*.
Incorrect! Try again.
6Which utility is preferred for performing floating-point arithmetic in shell scripts?
A.expr
B.let
C.bc
D.float
Correct Answer: bc
Explanation:expr and let only support integer arithmetic. bc (Basic Calculator) is an arbitrary-precision calculator language that supports floating-point numbers.
Incorrect! Try again.
7How do you define an arithmetic operation involving variables and using double parentheses?
A.$(( a + b ))
B.(( b ))
C.[[ a + b ]]
D.{ a + b }
Correct Answer: $(( a + b ))
Explanation:The syntax is optional inside for variable names, the outer $ is required to substitute the result.
Incorrect! Try again.
8What is the output of the following command? echo "scale=2; 10/3" | bc
A.3
B.3.33
C.3.333
D.3.00
Correct Answer: 3.33
Explanation:When piping to bc, scale=2 sets the number of decimal places to 2. Therefore, results in $3.33$.
Incorrect! Try again.
9Which operator is used to redirect standard output to a file, overwriting the file if it exists?
A.>>
B.<
C.>
D.|
Correct Answer: >
Explanation:The > operator redirects standard output (stdout) to a file. It creates the file if it doesn't exist or truncates (overwrites) it if it does.
Incorrect! Try again.
10What 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
Correct Answer: Redirects output and appends to the file
Explanation:The >> operator redirects standard output to a file but appends the data to the end of the file rather than overwriting existing content.
Incorrect! Try again.
11Which operator creates a pipe, sending the standard output of the first command as standard input to the second command?
A.&
B.|
C.>
D.;
Correct Answer: |
Explanation:The pipe character | connects the stdout of the left command to the stdin of the right command (e.g., ls | grep txt).
Incorrect! Try again.
12Which 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
Correct Answer: command 2> error.log
Explanation:File descriptor 1 is stdout and 2 is stderr. 2> specifically redirects the error stream.
Incorrect! Try again.
13Which command allows the user to input data into a variable during script execution?
A.input
B.get
C.scan
D.read
Correct Answer: read
Explanation:The read command reads a line from standard input (or a file) and assigns it to a variable.
Incorrect! Try again.
14In a conditional statement, which flag checks if a file exists and is a regular file?
A.-d
B.-r
C.-f
D.-s
Correct Answer: -f
Explanation:Inside test brackets [ ]: -f checks for a regular file, -d checks for a directory, -r checks for read permissions, and -s checks if size is greater than zero.
Incorrect! Try again.
15Which operator is used for numeric equality comparison in a [ ] test construct?
A.=
B.==
C.-eq
D.-equal
Correct Answer: -eq
Explanation:In standard bash [ ] tests, -eq is used for integers. = and == are used for string comparisons.
Incorrect! Try again.
16What 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
Correct Answer: if [ condition ]; then ... else ... fi
Explanation:The bash syntax requires if, followed by the test, then then, the commands, else (optional), and ends with fi.
Incorrect! Try again.
17Which special variable holds the exit status of the last executed command?
A.$$
B.$#
C.$?
D.$!
Correct Answer: $?
Explanation:$? contains the exit status (return code) of the most recently executed foreground pipeline. 0 usually indicates success.
Incorrect! Try again.
18Which 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
Correct Answer: case
Explanation:The case statement allows handling multiple patterns/options cleanly, making it ideal for menu selection logic compared to multiple nested if-else statements.
Incorrect! Try again.
19How is a case statement block terminated in bash?
A.endcase
B.esac
C.done
D.fi
Correct Answer: esac
Explanation:Just as if ends with fi, the case statement ends with esac (case spelled backward).
Incorrect! Try again.
20Inside a case statement, what signifies the end of a specific pattern block?
A.;
B.;;
C.break
D.stop
Correct Answer: ;;
Explanation:Double semicolons ;; are used to terminate a specific pattern clause in a case statement, preventing fall-through.
Incorrect! Try again.
21Which loop construct iterates as long as the condition is true?
A.until
B.for
C.while
D.case
Correct Answer: while
Explanation:A while loop executes the block of code as long as the specified condition evaluates to true (exit status 0).
Incorrect! Try again.
22Which 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
Correct Answer: until
Explanation:The until loop executes commands as long as the test command returns a non-zero exit status (false). It stops when the condition becomes true.
Incorrect! Try again.
23What 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}
Correct Answer: for (( i=0; i<5; i++ ))
Explanation:Bash supports C-style for-loops using double parentheses (( ... )).
Incorrect! Try again.
24How do you access all arguments passed to a script simultaneously?
A.$1
B.$#
C.$@
D.$?
Correct Answer: $@
Explanation:# is the count of arguments.
Incorrect! Try again.
25Which command is used to terminate a loop prematurely?
A.exit
B.stop
C.continue
D.break
Correct Answer: break
Explanation:break exits the current loop structure immediately. exit terminates the entire script.
Incorrect! Try again.
26Which command skips the remainder of the current loop iteration and moves to the next one?
A.pass
B.next
C.continue
D.skip
Correct Answer: continue
Explanation:continue skips the remaining commands in the current loop cycle and jumps to the next iteration.
Incorrect! Try again.
27How 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"
Correct Answer: fruits=("Apple" "Banana")
Explanation:Bash arrays are defined using parentheses () with elements separated by spaces.
Incorrect! Try again.
28How do you access the first element of an indexed array named arr?
A.$arr[0]
B.${arr[0]}
C.$arr{0}
D.arr[0]
Correct Answer: ${arr[0]}
Explanation:Curly braces {} are required when accessing array indices in bash to distinguish the index from surrounding text.
Incorrect! Try again.
29Which syntax returns the total number of elements (length) in an array named arr?
A.${#arr}
B.${#arr[@]}
C.${arr[*]}
D.length(arr)
Correct Answer: ${#arr[@]}
Explanation:The # prefix combined with [@] or [*] gives the count of elements in the array.
Incorrect! Try again.
30What 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={}
Correct Answer: declare -A myMap
Explanation:The declare -A command is explicitly required to create an associative array in bash. declare -a (lowercase) creates an indexed array.
Incorrect! Try again.
31How do you define a function named myFunc in bash?
A.function myFunc() { ... }
B.def myFunc: ...
C.void myFunc() { ... }
D.func myFunc { ... }
Correct Answer: function myFunc() { ... }
Explanation:The syntax is either function name { ... } or name() { ... }. The option function myFunc() { ... } is a valid combination.
Incorrect! Try again.
32Inside 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]}
Correct Answer: $1
Explanation:Inside a function, 2, etc., refer to the arguments passed to the function, not the script's command-line arguments (unless the function was called with them).
Incorrect! Try again.
33How 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
Correct Answer: local x=1
Explanation:The local keyword restricts the scope of the variable to the function in which it is declared.
Incorrect! Try again.
34What is the output of echo $(( 5 % 2 ))?
A.2.5
B.2
C.1
D.5
Correct Answer: 1
Explanation:The % operator performs the modulo operation (remainder). $5$ divided by $2$ is $2$ with a remainder of $1$.
Incorrect! Try again.
35Which special variable represents the Process ID (PID) of the current script?
A.$PID
B.$!
C.$$
D.$?
Correct Answer: $$
Explanation:$$ expands to the process ID of the current shell execution.
Incorrect! Try again.
36What command is used to read input from a menu selection loop nicely formatted with numbers?
A.case
B.select
C.choose
D.option
Correct Answer: select
Explanation:The select construct generates a numbered menu from a list of items and prompts the user for input, automatically handling the loop logic.
Incorrect! Try again.
37What 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
Correct Answer: Enables debugging mode (prints commands as they execute)
Explanation:set -x enables a mode of the shell where all commands are printed to standard error before execution, which is useful for debugging.
Incorrect! Try again.
38Which logical operator represents a logical AND inside [ ] brackets?
A.&&
B.-a
C.-and
D.&
Correct Answer: -a
Explanation:Inside the legacy single brackets [ ], -a is the AND operator. (Note: Inside [[ ]] or outside brackets, && is used).
Incorrect! Try again.
39Which logical operator represents a logical OR inside [ ] brackets?
A.||
B.-o
C.-or
D.|
Correct Answer: -o
Explanation:Inside the legacy single brackets [ ], -o is the OR operator.
Incorrect! Try again.
40What 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
Correct Answer: It executes in the current shell context (sourcing)
Explanation:Using . (or source) executes the script commands in the current shell environment, meaning variable changes persist after the script ends.
Incorrect! Try again.
41Which command substitution syntax is modern and preferred over backticks `...`?
A.${ command }
B.$( command )
C.(( command ))
D.[ command ]
Correct Answer: $( command )
Explanation:$(...) is the preferred syntax for command substitution because it is easier to read and allows nesting.
Incorrect! Try again.
42What will echo "Value: $HOME" print?
A.Value: $HOME
B.Value: /home/username
C.Value:
D.Error
Correct Answer: Value: /home/username
Explanation:Double quotes " " allow variable expansion. $HOME will be replaced by the path to the current user's home directory.
Incorrect! Try again.
43What will echo 'Value: $HOME' print?
A.Value: $HOME
B.Value: /home/username
C.Value:
D.Error
Correct Answer: Value: $HOME
Explanation:Single quotes ' ' treat all characters literally. No variable expansion occurs.
Incorrect! Try again.
44Which test operator checks if a string is empty (zero length)?
A.-z
B.-n
C.-e
D.-s
Correct Answer: -z
Explanation:The -z operator returns true if the length of the string is zero.
Incorrect! Try again.
45Which command is used to shift positional parameters (e.g., make 1)?
A.move
B.shift
C.next
D.rotate
Correct Answer: shift
Explanation:The shift command moves positional parameters to the left. 1, 2, and the original $1 is lost.
Incorrect! Try again.
46How 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 ]
Correct Answer: [ $num -ne 10 ]
Explanation:For integer comparison, -ne stands for Not Equal.
Incorrect! Try again.
47To 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
Correct Answer: for file in *.txt
Explanation:The shell expands *.txt to the list of matching filenames. The loop iterates over this list.
Incorrect! Try again.
48What is the result of echo $(( 2**3 )) in bash?
A.6
B.8
C.5
D.Error
Correct Answer: 8
Explanation:The ** operator in bash arithmetic expansion performs exponentiation ().
Incorrect! Try again.
49Which output redirection allows you to redirect output to null (discard it completely)?
A.> /dev/null
B.> /dev/void
C.> /dev/empty
D.> /dev/zero
Correct Answer: > /dev/null
Explanation:/dev/null is a special file that discards all data written to it. It is often used to suppress output.
Incorrect! Try again.
50What 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
Correct Answer: "*" treats them as a single string
Explanation:"*" concatenates all arguments into one single string separated by the first character of IFS.
Incorrect! Try again.
Give Feedback
Help us improve by sharing your thoughts or reporting issues.