Unit 3 - Practice Quiz

CSC104 50 Questions
0 Correct 0 Wrong 50 Left
0/50

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

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

A. .py
B. .bat
C. .sh
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. $1
B. $0
C. $#
D. $arg1

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. 8
B. 5 + 3
C. 53
D. Error

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

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

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

A. exec script.sh
B. chmod +x script.sh
C. enable script.sh
D. chown +x 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}
B. length($text)
C. ${text[@]}
D. ${text.length}

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

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

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

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

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

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

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

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

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

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

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. match
B. switch
C. select
D. case

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

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

23 Which loop iterates over a list of items?

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

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

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

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

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

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

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

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

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

28 Which command terminates the entire loop immediately?

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

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

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

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

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

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

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

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

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

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

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

34 Which conditional operator checks if a specific directory exists?

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

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

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

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

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

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

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

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

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

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

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

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

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

41 What does the tilde ~ symbol represent in Bash?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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