MCQs on Shell Arrays | Shell Scripting

Understanding arrays in shell scripting is crucial for managing collections of data efficiently. This chapter covers creating, iterating over, and working with associative arrays in Bash, specifically for version 4.x and beyond.


1. Creating and Using Arrays

  1. What is the correct syntax to declare an array in Bash?
    a) array=(1 2 3)
    b) array{1, 2, 3}
    c) array[1,2,3]
    d) array(1,2,3)
  2. How do you access the first element of an array in Bash?
    a) ${array[0]}
    b) array[1]
    c) ${array[1]}
    d) array(0)
  3. Which of the following will correctly create an array with the elements apple, banana, and cherry?
    a) array=("apple" "banana" "cherry")
    b) array=apple,banana,cherry
    c) array=["apple", "banana", "cherry"]
    d) array = apple banana cherry
  4. How do you find the number of elements in an array in Bash?
    a) array.length
    b) array.size()
    c) ${#array[@]}
    d) length(array)
  5. How do you add an element grape at the end of an existing array?
    a) array+=("grape")
    b) array("grape")
    c) array.push("grape")
    d) array("grape")
  6. What does the following code do? array[0]="apple"; array[1]="banana"
    a) Declares an array with two elements
    b) Assigns values to the first two array indices
    c) Creates an array with string values
    d) Both a and b
  7. How can you assign values to an array using a loop in Bash?
    a) for i in 1 to 5; do array[i]=$i; done
    b) for ((i=0;i<5;i++)); do array[i]=$i; done
    c) for i in {0..5}; do array[i]=$i; done
    d) All of the above
  8. What is the syntax to delete an element from an array?
    a) unset array[0]
    b) delete array[0]
    c) remove array[0]
    d) array[0]=null
  9. How do you declare an empty array in Bash?
    a) array=()
    b) array[]
    c) array=""
    d) array={}
  10. How do you access all elements of an array in Bash?
    a) array[*]
    b) ${array[@]}
    c) array[]
    d) array{}

2. Iterating Over Arrays

  1. How can you iterate over all elements of an array in Bash?
    a) for item in array; do echo $item; done
    b) for item in ${array[@]}; do echo $item; done
    c) for i in array; do echo $i; done
    d) for item in ${array[*]}; do echo $item; done
  2. Which loop construct is most commonly used for iterating over array indices in Bash?
    a) for-in
    b) while
    c) for
    d) until
  3. To iterate over indices of an array, what is the correct syntax in Bash?
    a) for i in ${!array[@]}; do echo $array[$i]; done
    b) for i in ${array[@]}; do echo $array[$i]; done
    c) for i in ${array[@]}; do echo $i; done
    d) for i in ${array[0]}; do echo $array[i]; done
  4. What is the result of the following code:
    array=("a" "b" "c")
    for i in ${array[@]}; do echo $i; done
    a) Prints a, b, and c
    b) Prints the entire array as a single line
    c) Throws an error
    d) Prints nothing
  5. How do you get the index of a specific element (e.g., banana) in an array?
    a) index=$(array.indexOf("banana"))
    b) index=$(echo ${array[@]} | grep -n "banana")
    c) index=$(array["banana"])
    d) index=$(array.index("banana"))
  6. How can you loop through the indices and values of an array in Bash?
    a) for i in ${!array[@]}; do echo $i: ${array[$i]}; done
    b) for i in ${array[@]}; do echo $i: ${array[$i]}; done
    c) for i in ${!array[*]}; do echo $i; done
    d) for i in ${array[*]}; do echo $i; done
  7. Which of the following methods can be used to get the last element of an array?
    a) ${array[-1]}
    b) ${array[${#array[@]}-1]}
    c) ${array[0]}${array[${#array[@]}-1]}
    d) array[${#array[@]}]
  8. What does the following code do?
    array=("red" "green" "blue")
    for color in "${array[@]}"; do echo $color; done
    a) Loops through the entire array and prints each element
    b) Loops through the array indices and prints them
    c) Loops through the elements and prints the entire array
    d) Prints only the first element
  9. How can you break a loop when you find a specific element in an array?
    a) break
    b) exit
    c) continue
    d) stop
  10. Which statement correctly skips the current iteration and moves to the next one in a loop?
    a) skip
    b) continue
    c) break
    d) exit

3. Associative Arrays (Bash 4.x+)

  1. Which version of Bash introduced associative arrays?
    a) Bash 2.x
    b) Bash 3.x
    c) Bash 4.x
    d) Bash 5.x
  2. How do you declare an associative array in Bash?
    a) declare -A array
    b) array=(key1:value1 key2:value2)
    c) array={key1=value1, key2=value2}
    d) array["key1"]="value1"
  3. How do you access the value of a key in an associative array?
    a) ${array[key]}
    b) array[key]
    c) ${array["key"]}
    d) array[key]
  4. What is the correct way to add a new key-value pair to an associative array?
    a) array[key]="value"
    b) array.add("key", "value")
    c) array.push(key, value)
    d) array["key"]=value
  5. How do you iterate over the keys of an associative array?
    a) for key in ${array[@]}; do echo $key; done
    b) for key in ${!array[@]}; do echo $key; done
    c) for key in ${array[@]}; do echo ${array[$key]}; done
    d) for key in ${array[*]}; do echo $key; done
  6. How do you check if a key exists in an associative array?
    a) if [ -v array[key] ]
    b) if [ -e ${array[key]} ]
    c) if [ $array[key] ]
    d) if [ -f ${array[key]} ]
  7. How can you delete an element from an associative array?
    a) unset array[key]
    b) delete array[key]
    c) remove array[key]
    d) array[key]=null
  8. Which of the following commands prints all the keys in an associative array?
    a) echo ${array[@]}
    b) echo ${!array[@]}
    c) echo ${array[*]}
    d) echo ${array[!key]}
  9. How do you check the length of an associative array?
    a) length(array)
    b) echo ${#array[@]}
    c) array.size
    d) array.length()
  10. How do you get the value of a specific key in an associative array?
    a) ${array[key]}
    b) ${array["key"]}
    c) array["key"]
    d) array[key]

Answer Key

QnoAnswer (Option with the text)
1a) array=(1 2 3)
2a) ${array[0]}
3a) array=("apple" "banana" "cherry")
4c) ${#array[@]}
5a) array+=("grape")
6d) Both a and b
7d) All of the above
8a) unset array[0]
9a) array=()
10b) ${array[@]}
11b) for item in ${array[@]}; do echo $item; done
12c) for
13a) for i in ${!array[@]}; do echo $array[$i]; done
14a) Prints a, b, and c
15b) `index=$(echo ${array[@]}
16a) for i in ${!array[@]}; do echo $i: ${array[$i]}; done
17b) ${array[${#array[@]}-1]}
18a) Loops through the entire array and prints each element
19a) break
20b) continue
21c) Bash 4.x
22a) declare -A array
23c) ${array["key"]}
24a) array[key]="value"
25b) for key in ${!array[@]}; do echo $key; done
26a) if [ -v array[key] ]
27a) unset array[key]
28b) echo ${!array[@]}
29b) echo ${#array[@]}
30b) ${array["key"]}

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top