Unit 3 - Practice Quiz

CSC104

1 Which character sequence, placed at the very first line of a script, indicates the interpreter to be used for execution?

A. //bin/bash
B. #!/bin/bash
C. / /bin/bash /
D. $$/bin/bash

2 What is the correct syntax to assign the integer value $10$ to a variable named count in Bash?

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

3 How do you access the value stored in a variable named username?

A. username
B. %username%
C. $username
D. @username

4 Which command is used to accept user input and store it in a variable?

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

5 What is the standard file extension for Bash scripts, although not strictly required by Linux?

A. .bat
B. .sh
C. .py
D. .exe

6 Which character is used to start a single-line comment in a Bash script?

A. //
B. #
C. /*
D. --

7 Which variable represents the first argument passed to a Bash script?

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

8 Which variable contains the total number of arguments passed to the script?

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

9 What is the output of the expression: echo $(( 5 + 3 ))?

A. 5 + 3
B. 8
C. 53
D. Error

10 Which operator is used to calculate the remainder (modulo) in Bash arithmetic?

A. //
B. mod
C. %
D. \

11 To make a script named script.sh executable, which command should be run?

A. chmod +x script.sh
B. chown +x script.sh
C. exec script.sh
D. enable script.sh

12 What defines an array named fruits containing "apple", "banana", and "cherry"?

A. fruits = ["apple", "banana", "cherry"]
B. fruits=("apple" "banana" "cherry")
C. fruits="apple, banana, cherry"
D. array fruits = {apple, banana, cherry}

13 Given arr=(A B C), how do you access the second element ("B")?

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

14 Which syntax correctly returns the length of the string stored in variable text?

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

15 Given str="Hello World", what does ${str:0:5} output?

A. World
B. Hello
C. Hello
D. Worl

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

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

17 Which exit code conventionally indicates that a command executed successfully?

A. 1
B. -1
C.
D. 100

18 Which conditional operator checks if a file exists and is a regular file?

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

19 Which operator is used to compare if two integers are equal in a [ ] test condition?

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

20 What is the correct way to close an if statement?

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

21 Which keyword matches the start of a case statement to handle multiple patterns?

A. switch
B. case
C. select
D. match

22 In a case statement, what sequence acts as a break to terminate a pattern block?

A. ;
B. break
C. ;;
D. done

23 Which loop iterates over a list of items?

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

24 Which loop continues to execute as long as the condition is true?

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

25 Which loop executes as long as the condition is false (stops when it becomes true)?

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

26 Which keywords frame the body of a for or while loop?

A. start ... end
B. do ... done
C. bin ... bash
D. { ... }

27 Which command immediately terminates the current loop iteration and jumps to the next one?

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

28 Which command terminates the entire loop immediately?

A. stop
B. halt
C. break
D. return

29 What is the correct syntax to define a function named myFunc?

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

30 How do you call a function named greet with an argument "John"?

A. call greet("John")
B. greet("John")
C. greet "John"
D. execute greet "John"

31 Which keyword is used to define variables that are valid only within a function?

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

32 Which option can be added to the read command to display a prompt message to the user?

A. -m
B. -p
C. -t
D. -s

33 What is the purpose of command substitution $(command)?

A. To run a command in the background
B. To replace a command with a comment
C. To use the output of a command as a variable or argument
D. To debug a command

34 Which conditional operator checks if a specific directory exists?

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

35 Which Bash mode/flag prints each command before executing it, useful for debugging?

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

36 What does the logic operator && do between two commands (e.g., cmd1 && cmd2)?

A. Runs cmd2 only if cmd1 succeeds
B. Runs cmd2 only if cmd1 fails
C. Runs both commands simultaneously
D. Pipes output of cmd1 to cmd2

37 What does the logic operator || do (e.g., cmd1 || cmd2)?

A. Runs cmd2 only if cmd1 succeeds
B. Runs cmd2 only if cmd1 fails
C. Runs both commands in parallel
D. Redirects input

38 Which command is used to create a shortcut for a long command (e.g., creating ll for ls -al)?

A. shortcut
B. link
C. alias
D. export

39 Which file in the user's home directory is commonly used to store persistent aliases and environment variables?

A. .bash_history
B. .bashrc
C. .config
D. .env

40 Which command is used to reload the .bashrc file without logging out and back in?

A. reload .bashrc
B. refresh
C. source .bashrc
D. run .bashrc

41 What does the tilde ~ symbol represent in Bash?

A. The root directory
B. The current working directory
C. The user's home directory
D. The last executed command

42 Which shortcut repeats the very last command executed in the terminal?

A. @@
B. !!
C. r
D. again

43 If you want to perform floating-point arithmetic (decimals), which external utility is commonly piped into from Bash?

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

44 How do you check if a string variable $STR is empty in a conditional test?

A. [ -z "$STR" ]
B. [ -n "$STR" ]
C. [ "$STR" == 0 ]
D. [ -e "$STR" ]

45 What does the -ne operator stand for in an integer comparison?

A. Not Empty
B. Near Equal
C. Not Equal
D. Negative

46 Which command allows you to view the list of previously executed commands?

A. past
B. log
C. history
D. mem

47 What happens if you run a script that does not have the executable permission bit set using ./script.sh?

A. It runs in debug mode
B. Permission denied error
C. It asks for the root password
D. It runs slowly

48 How can you specify a range of numbers from 1 to 5 in a for loop?

A. {1..5}
B. [1-5]
C. (1 to 5)
D. range(1,5)

49 When defining a custom command (script) to be accessible system-wide, where is it commonly placed?

A. /tmp
B. /var/log
C. /usr/local/bin
D. /etc

50 Which mathematical assignment operator increments a variable by a value (e.g., )?

A. x++5
B. x += 5
C. x =+ 5
D. x :+ 5