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. int count = 10
D. $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. read
D. scan

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

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

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

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. 53
C. 8
D. Error

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

A. mod
B. %
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. array fruits = {apple, banana, cherry}
B. fruits=("apple" "banana" "cherry")
C. fruits = ["apple", "banana", "cherry"]
D. fruits="apple, banana, cherry"

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

23 Which loop iterates over a list of items?

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

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

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

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

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

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

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

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

A. exit
B. pass
C. break
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. myFunc() { commands; }
B. function myFunc { }
C. def myFunc():
D. void myFunc() { }

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

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

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

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

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

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

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

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

34 Which conditional operator checks if a specific directory exists?

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

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

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

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 both commands in parallel
C. Redirects input
D. Runs cmd2 only if cmd1 fails

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

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

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

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

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 current working directory
B. The last executed command
C. The root directory
D. The user's home directory

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

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

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

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

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

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

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

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

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. Permission denied error
B. It runs slowly
C. It asks for the root password
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 to 5)
B. [1-5]
C. range(1,5)
D. {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. /tmp
D. /var/log

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