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. #include <bash>
C. #!/bin/sh
D. #!/bin/bash

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

A. chown +x script.sh
B. exec script.sh
C. chmod +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. ./myscript.sh
B. call myscript.sh
C. run myscript.sh
D. 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. import
C. define
D. export

7 What is the output of the following command sequence?\na=10\necho "The value is $a"

A. The value is $a
B. The value is 10
C. The value is a
D. Error

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. *
B. X
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. expr
C. bc
D. let

12 What is the correct syntax to calculate with 2 decimal places using bc?

A. expr 10 / 3 | bc
B. bc -f "10/3"
C. echo "10/3" | bc
D. echo "scale=2; 10/3" | bc

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

A. 2
B. 1
C. 2.5
D. 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. Executes ls and grep simultaneously without data transfer
B. Searches for "txt" in the ls executable file
C. Writes the output of ls to a file named grep
D. Feeds the standard output of ls as standard input to grep

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

24 A while loop continues to execute as long as the condition is:

A. True (zero exit status)
B. False (non-zero exit status)
C. Null
D. Undefined

25 What is the difference between while and until loops?

A. while loops execute at least once; until loops may not execute.
B. until loops run while true; while loops run while false.
C. while loops run while true; until loops run while false.
D. There is no difference.

26 How do you define an infinite loop in Bash?

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

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

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

28 What is the syntax to declare an indexed array named fruits containing "Apple" and "Banana"?

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

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

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

30 Which syntax returns all elements of an array?

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

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

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

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. declare -A my_array
D. typeset my_array

33 How do you define a function named myFunc?

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

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

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

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

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

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

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

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

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

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. scan
B. read
C. input
D. get

41 What does $? represent in shell scripting?

A. The process ID of the current shell
B. The exit status of the last executed command
C. The process ID of the last background command
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 -v myscript.sh
B. bash -e myscript.sh
C. bash -x myscript.sh
D. bash -d myscript.sh

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

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

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

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

45 How do you perform a bitwise left shift on a variable by 2 bits?

A. bc x << 2
B. $(( x << 2 ))
C. shift x 2
D. expr x LSHIFT 2

46 What does the shift command do in a shell script?

A. Shifts the screen output
B. Changes the shell prompt
C. Performs a bitwise shift on a number
D. Moves positional parameters to the left (1)

47 How do you check if a directory named data exists in a conditional statement?

A. [ -d data ]
B. [ -x data ]
C. [ -f data ]
D. [ -r data ]

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

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

49 What is the correct way to add an element "Grape" to an existing indexed array fruits?

A. append fruits "Grape"
B. fruits+=("Grape")
C. fruits.add("Grape")
D. fruits = fruits + "Grape"

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

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