1Which line must begin a shell script to indicate that it should be interpreted by the Bash shell?
A.#!/bin/bash
B.//bin/bash
C.#include <bash>
D.#!/bin/sh
Correct Answer: #!/bin/bash
Explanation:
The shebang line #!/bin/bash tells the operating system to use the Bash interpreter to execute the script.
Incorrect! Try again.
2Which command is used to grant execute permission to a shell script file named script.sh?
A.exec script.sh
B.chmod +x script.sh
C.chown +x script.sh
D.chmod -r script.sh
Correct Answer: chmod +x script.sh
Explanation:
The chmod command modifies file permissions. The +x flag adds execute permission to the user, group, and others (depending on umask), allowing the script to be run.
Incorrect! Try again.
3How do you execute a script named myscript.sh in the current directory if the directory is not in your $PATH?
A.call myscript.sh
B.myscript.sh
C../myscript.sh
D.run myscript.sh
Correct Answer: ./myscript.sh
Explanation:
When a script is not in the system $PATH, you must specify the relative path ./ (current directory) or the absolute path to execute it.
Incorrect! Try again.
4Which variable assignment syntax is valid in Bash?
A.VAR := value
B.VAR=value
C.$VAR=value
D.VAR = value
Correct Answer: VAR=value
Explanation:
In Bash, there must be no spaces around the assignment operator = when assigning a value to a variable.
Incorrect! Try again.
5How do you access the value stored in a variable named count?
A.count
B.%count%
C.$count
D.&count
Correct Answer: $count
Explanation:
The $ symbol is used to reference the value stored inside a variable in shell scripting.
Incorrect! Try again.
6Which command allows a user-defined variable to be accessed by child processes or subshells?
A.global
B.define
C.export
D.import
Correct Answer: export
Explanation:
The export command makes a variable an environment variable, allowing it to be inherited by child processes.
Incorrect! Try again.
7What is the output of the following command sequence?\na=10\necho "The value is $a"
A.Error
B.The value is a
C.The value is 10
D.The value is $a
Correct Answer: The value is 10
Explanation:
Double quotes " " allow for variable expansion, so $a is replaced by 10. Single quotes ' ' would preserve the literal string.
Incorrect! Try again.
8Which symbol is used to perform command substitution (using the output of a command as a variable value)?
A.@(command)
B.${command}
C.#{command}
D.$(command)
Correct Answer: $(command)
Explanation:
$(command) (or backticks `command`) executes the command and replaces the expression with the command's standard output.
Incorrect! Try again.
9When using the expr command for multiplication, how must the operator be written?
A.X
B.*
C.*
D.x
Correct Answer: *
Explanation:
The * character is a wildcard in shell. When used with expr, it must be escaped using a backslash \* to be treated as a multiplication operator.
Incorrect! Try again.
10Which of the following commands calculates the sum of and using expr correctly?
A.expr (y)
B.expr y
C.expr y
D.expr y
Correct Answer: expr y
Explanation:
The expr command requires spaces between the operands and the operator.
Incorrect! Try again.
11Which utility is best suited for performing floating-point arithmetic in a shell script?
A.eval
B.bc
C.let
D.expr
Correct Answer: bc
Explanation:
bc (Basic Calculator) is an arbitrary-precision calculator language that supports floating-point math, whereas expr and let only support integers.
Incorrect! Try again.
12What is the correct syntax to calculate with 2 decimal places using bc?
A.echo "scale=2; 10/3" | bc
B.expr 10 / 3 | bc
C.bc -f "10/3"
D.echo "10/3" | bc
Correct Answer: echo "scale=2; 10/3" | bc
Explanation:
The scale variable in bc determines the number of decimal digits after the decimal point.
Incorrect! Try again.
13What is the result of the arithmetic expansion $(( 5 % 2 ))?
A.2
B.1
C.5
D.2.5
Correct Answer: 1
Explanation:
The % operator calculates the modulus (remainder). $5$ divided by $2$ is $2$ with a remainder of $1$.
Incorrect! Try again.
14Which 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 output to a file and overwrites existing content. >> appends to the file.
Incorrect! Try again.
15What does the command ls | grep "txt" do?
A.Feeds the standard output of ls as standard input to grep
B.Executes ls and grep simultaneously without data transfer
C.Searches for "txt" in the ls executable file
D.Writes the output of ls to a file named grep
Correct Answer: Feeds the standard output of ls as standard input to grep
Explanation:
The pipe | operator passes the stdout of the command on the left (ls) to the stdin of the command on the right (grep).
Incorrect! Try again.
16How do you redirect standard error (stderr) to standard output (stdout)?
A.1>&2
B.&>
C.2>&1
D.2>1
Correct Answer: 2>&1
Explanation:
2 represents stderr and 1 represents stdout. 2>&1 redirects file descriptor 2 to the same location as file descriptor 1.
Incorrect! Try again.
17Which operator is used to append standard output to an existing file?
A.2>
B.>
C.>>
D.|
Correct Answer: >>
Explanation:
The >> operator appends data to the end of a file without overwriting existing content.
Incorrect! Try again.
18In the if statement syntax, which keyword ends the conditional block?
A.end
B.endif
C.done
D.fi
Correct Answer: fi
Explanation:
In shell scripting, an if block is closed with fi (if spelled backwards).
Incorrect! Try again.
19Which flag is used in a test condition [ ... ] to check if a file exists?
A.-d
B.-x
C.-e
D.-f
Correct Answer: -e
Explanation:
-e checks if a file (or directory) exists. Note: -f specifically checks if it exists AND is a regular file (not a directory).
Incorrect! Try again.
20Which operator performs an integer comparison for "greater than" inside [ ... ]?
A.-ge
B.-gt
C.>
D.succ
Correct Answer: -gt
Explanation:
Inside single brackets [ ], -gt is used for integer comparison. The > symbol is interpreted as output redirection unless escaped or used inside double parentheses.
Incorrect! Try again.
21Which of the following is the correct syntax for checking if string b?
A.[ b ]
B.Both B and C
C.[ b ]
D.[ b ]
Correct Answer: Both B and C
Explanation:
In Bash [ ... ], both = and == can be used for string comparison. -eq is strictly for integers.
Incorrect! Try again.
22In a case statement, what sequence marks the end of a pattern block?
A.end
B.break
C.;;
D.;
Correct Answer: ;;
Explanation:
The double semicolon ;; is used to terminate a specific case option block.
Incorrect! Try again.
23Which looping construct is specifically designed to iterate over a list of items?
A.select
B.while
C.for
D.until
Correct Answer: for
Explanation:
The for loop iterates over a list of words or elements (e.g., for i in 1 2 3).
Incorrect! Try again.
24A while loop continues to execute as long as the condition is:
A.Undefined
B.Null
C.True (zero exit status)
D.False (non-zero exit status)
Correct Answer: True (zero exit status)
Explanation:
The while loop runs as long as the command/condition evaluates to true (exit status 0).
Incorrect! Try again.
25What is the difference between while and until loops?
A.while loops run while true; until loops run while false.
B.while loops execute at least once; until loops may not execute.
C.until loops run while true; while loops run while false.
D.There is no difference.
Correct Answer: while loops run while true; until loops run while false.
Explanation:
while continues as long as the condition is true. until continues as long as the condition is false (it stops when the condition becomes true).
Incorrect! Try again.
26How do you define an infinite loop in Bash?
A.loop forever
B.for (;;)
C.Both B and C
D.while true; do ... done
Correct Answer: Both B and C
Explanation:
while true and the C-style for ((;;)) (often written without parens in other contexts, but valid in bash C-style syntax) create infinite loops.
Incorrect! Try again.
27Which statement immediately terminates the current loop iteration and moves to the next one?
A.exit
B.continue
C.return
D.break
Correct Answer: continue
Explanation:
continue skips the remainder of the current loop iteration and starts the next iteration. break exits the loop entirely.
Incorrect! Try again.
28What is the syntax to declare an indexed array named fruits containing "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 space-separated values: arr=(val1 val2).
Incorrect! Try again.
29How do you access the second element of an array named arr?
A.${arr[2]}
B.${arr[1]}
C.$arr(1)
D.$arr[2]
Correct Answer: ${arr[1]}
Explanation:
Bash arrays are 0-indexed. The second element is at index 1. Braces {} are required for array access.
Incorrect! Try again.
30Which syntax returns all elements of an array?
A.Both A and B
B.${arr[@]}
C.$arr
D.${arr[*]}
Correct Answer: Both A and B
Explanation:
Both {arr[@]} expand to all members of the array, though they handle quoting differently within double quotes.
Incorrect! Try again.
31How do you determine the length (number of elements) of an array named arr?
A.${#arr[@]}
B.length(arr)
C.$#arr
D.${arr.size}
Correct Answer: ${#arr[@]}
Explanation:
The # prefix before the array name (combined with [@]) returns the count of elements in the array.
Incorrect! Try again.
32To use an associative array (key-value pairs) in Bash, how must you declare it?
A.array -assoc my_array
B.declare -a my_array
C.typeset my_array
D.declare -A my_array
Correct Answer: declare -A my_array
Explanation:
Associative arrays must be explicitly declared using declare -A. Lowercase -a is for indexed arrays.
Incorrect! Try again.
33How do you define a function named myFunc?
A.function myFunc { ... }
B.Both A and B
C.def myFunc() { ... }
D.myFunc() { ... }
Correct Answer: Both A and B
Explanation:
Bash supports both the function name { ... } syntax and the standard name() { ... } syntax.
Incorrect! Try again.
34Inside a function, which variable holds the first argument passed to it?
A.$0
B.$arg1
C.$#
D.$1
Correct Answer: $1
Explanation:
Positional parameters 2, etc., hold the arguments passed to the function. $0 is the script name.
Incorrect! Try again.
35How do you return a numeric status code from a function?
A.echo 5
B.result 5
C.return 5
D.exit 5
Correct Answer: return 5
Explanation:
return sets the exit status of the function (0-255). exit would terminate the entire script. echo sends data to stdout.
Incorrect! Try again.
36Which keyword is used to define variables that are limited to the scope of the function?
A.scope
B.private
C.var
D.local
Correct Answer: local
Explanation:
The local keyword ensures the variable is only visible within the function block, preventing side effects on global variables.
Incorrect! Try again.
37What is the output of echo $(( 2 + 3 * 4 ))?
A.20
B.2 + 3 * 4
C.Error
D.14
Correct Answer: 14
Explanation:
Bash arithmetic follows standard order of operations (precedence). Multiplication () happens before addition ().
Incorrect! Try again.
38Which special variable holds the number of arguments passed to a script or function?
A.$@
B.$$
C.$*
D.$#
Correct Answer: $#
Explanation:
$# stores the count of positional parameters (arguments) passed.
Incorrect! Try again.
39Which I/O redirection syntax allows a command to read input from the current script until a delimiter is reached (Here Document)?
A.|EOF
B.>EOF
C.<<EOF
D.<EOF
Correct Answer: <<EOF
Explanation:
<< followed by a delimiter (commonly EOF) starts a Here Document, feeding lines of text to the command until the delimiter appears again.
Incorrect! Try again.
40Which command is used to read input from the user into a variable?
A.read
B.get
C.scan
D.input
Correct Answer: read
Explanation:
The read command accepts input from stdin (keyboard) and assigns it to a variable.
Incorrect! Try again.
41What does $? represent in shell scripting?
A.The process ID of the last background command
B.The exit status of the last executed command
C.The process ID of the current shell
D.The current user's ID
Correct Answer: The exit status of the last executed command
Explanation:
$? returns the exit code of the most recently executed command (0 for success, non-zero for failure).
Incorrect! Try again.
42If you want to debug a script myscript.sh by seeing every command as it executes, how do you run it?
A.bash -d myscript.sh
B.bash -v myscript.sh
C.bash -x myscript.sh
D.bash -e myscript.sh
Correct Answer: bash -x myscript.sh
Explanation:
The -x flag enables xtrace mode, which prints each command to stderr before executing it, useful for debugging.
Incorrect! Try again.
43Which logical operator represents logical AND inside [ ... ]?
A.-a
B.AND
C.&&
D.&
Correct Answer: -a
Explanation:
Inside single test brackets [ ... ], -a is used for logical AND. Inside double brackets [[ ... ]] or command chaining, && is used.
Incorrect! Try again.
44Which logical operator represents logical OR inside [[ ... ]]?
A.OR
B.|
C.||
D.-o
Correct Answer: ||
Explanation:
Inside the enhanced test construct [[ ... ]], || is used for logical OR. -o is used in standard [ ... ].
Incorrect! Try again.
45How do you perform a bitwise left shift on a variable by 2 bits?
A.shift x 2
B.$(( x << 2 ))
C.expr x LSHIFT 2
D.bc x << 2
Correct Answer: $(( x << 2 ))
Explanation:
Arithmetic expansion $(( ... )) supports bitwise operators like << (left shift) and >> (right shift).
Incorrect! Try again.
46What does the shift command do in a shell script?
A.Performs a bitwise shift on a number
B.Moves positional parameters to the left (1)
C.Changes the shell prompt
D.Shifts the screen output
Correct Answer: Moves positional parameters to the left (1)
Explanation:
shift is used to iterate through arguments. It discards 2 to 3 to $2, etc.
Incorrect! Try again.
47How do you check if a directory named data exists in a conditional statement?
A.[ -d data ]
B.[ -r data ]
C.[ -f data ]
D.[ -x data ]
Correct Answer: [ -d data ]
Explanation:
The -d flag specifically checks if the path exists and is a directory.
Incorrect! Try again.
48Which file is automatically executed when a user logs into a Bash login shell?
A..bashrc
B..bash_profile
C..inputrc
D..bash_logout
Correct Answer: .bash_profile
Explanation:
For login shells, Bash looks for .bash_profile, .bash_login, or .profile. .bashrc is typically for non-login interactive shells.
Incorrect! Try again.
49What is the correct way to add an element "Grape" to an existing indexed array fruits?
A.append fruits "Grape"
B.fruits.add("Grape")
C.fruits = fruits + "Grape"
D.fruits+=("Grape")
Correct Answer: fruits+=("Grape")
Explanation:
The += operator with parentheses (value) appends an element to a Bash array.
Incorrect! Try again.
50In a menu-driven program using select, which variable holds the user's numeric selection?
A.$SELECT
B.$CHOICE
C.$OPTION
D.$REPLY
Correct Answer: $REPLY
Explanation:
The select construct presents a menu. The input string matches the variable name provided, but the index/number selected is stored in the built-in variable $REPLY.