Unit 2 - Practice Quiz

CSE325 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 Which 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

2 Which 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

3 How 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

4 Which variable assignment syntax is valid in Bash?

A. VAR := value
B. VAR=value
C. $VAR=value
D. VAR = value

5 How do you access the value stored in a variable named count?

A. count
B. %count%
C. $count
D. &count

6 Which command allows a user-defined variable to be accessed by child processes or subshells?

A. global
B. define
C. export
D. import

7 What 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

8 Which 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)

9 When using the expr command for multiplication, how must the operator be written?

A. X
B. *
C. *
D. x

10 Which of the following commands calculates the sum of and using expr correctly?

A. expr (y)
B. expr y
C. expr y
D. expr y

11 Which utility is best suited for performing floating-point arithmetic in a shell script?

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

12 What 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

13 What is the result of the arithmetic expansion $(( 5 % 2 ))?

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

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

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

15 What 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

16 How do you redirect standard error (stderr) to standard output (stdout)?

A. 1>&2
B. &>
C. 2>&1
D. 2>1

17 Which operator is used to append standard output to an existing file?

A. 2>
B. >
C. >>
D. |

18 In the if statement syntax, which keyword ends the conditional block?

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

19 Which flag is used in a test condition [ ... ] to check if a file exists?

A. -d
B. -x
C. -e
D. -f

20 Which operator performs an integer comparison for "greater than" inside [ ... ]?

A. -ge
B. -gt
C. >
D. succ

21 Which of the following is the correct syntax for checking if string b?

A. [ b ]
B. Both B and C
C. [ b ]
D. [ b ]

22 In a case statement, what sequence marks the end of a pattern block?

A. end
B. break
C. ;;
D. ;

23 Which looping construct is specifically designed to iterate over a list of items?

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

24 A 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)

25 What 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.

26 How do you define an infinite loop in Bash?

A. loop forever
B. for (;;)
C. Both B and C
D. while true; do ... done

27 Which statement immediately terminates the current loop iteration and moves to the next one?

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

28 What 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]

29 How do you access the second element of an array named arr?

A. ${arr[2]}
B. ${arr[1]}
C. $arr(1)
D. $arr[2]

30 Which syntax returns all elements of an array?

A. Both A and B
B. ${arr[@]}
C. $arr
D. ${arr[*]}

31 How do you determine the length (number of elements) of an array named arr?

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

32 To 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

33 How do you define a function named myFunc?

A. function myFunc { ... }
B. Both A and B
C. def myFunc() { ... }
D. myFunc() { ... }

34 Inside a function, which variable holds the first argument passed to it?

A. $0
B. $arg1
C. $#
D. $1

35 How do you return a numeric status code from a function?

A. echo 5
B. result 5
C. return 5
D. exit 5

36 Which keyword is used to define variables that are limited to the scope of the function?

A. scope
B. private
C. var
D. local

37 What is the output of echo $(( 2 + 3 * 4 ))?

A. 20
B. 2 + 3 * 4
C. Error
D. 14

38 Which special variable holds the number of arguments passed to a script or function?

A. $@
B. $$
C. $*
D. $#

39 Which 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

40 Which command is used to read input from the user into a variable?

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

41 What 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

42 If 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

43 Which logical operator represents logical AND inside [ ... ]?

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

44 Which logical operator represents logical OR inside [[ ... ]]?

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

45 How 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

46 What 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

47 How 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 ]

48 Which file is automatically executed when a user logs into a Bash login shell?

A. .bashrc
B. .bash_profile
C. .inputrc
D. .bash_logout

49 What 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")

50 In a menu-driven program using select, which variable holds the user's numeric selection?

A. $SELECT
B. $CHOICE
C. $OPTION
D. $REPLY