Unix - Shell Array MCQs
Shell Array MCQs : This section focuses on "Array" in Shell Progamming in Unix. These Multiple Choice Questions (MCQ) should be practiced to improve the unix skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive examinations.
1. What is true about array variable in Shell?
A. Shell supports a different type of variable called an array variable.
B. array variable can hold multiple values at the same time.
C. Arrays provide a method of grouping a set of variables
D. All of the above
View Answer
Ans : D
Explanation: All of the above statement are true about array variable.
2. Which of the following is correct syntax for defining array values?
A. array_name{index}=value
B. array_name(index)=value
C. array_name[index]=value
D. [index]array_name=value
View Answer
Ans : C
Explanation: array_name[index]=value is the correct syntax for defining array values.
3. What is the syntax of array initialization when you are using ksh shell?
A. array_name[index]=value
B. set -A array_name value1 value2 ... Valuen
C. array_name=(value1 ... valuen)
D. ${array_name[index]}=value
View Answer
Ans : B
Explanation: If you are using the ksh shell, here is the syntax of array initialization : set -A array_name value1 value2 ... valuen.
4. What is the syntax of array initialization when you are using bash shell?
A. array_name[index]=value
B. set -A array_name value1 value2 ... Valuen
C. array_name=(value1 ... valuen)
D. ${array_name[index]}=value
View Answer
Ans : C
Explanation: If you are using the bash shell, here is the syntax of array initialization : array_name=(value1 ... valuen)
5. what is the syntax to access array values?
A. ${array_name[index]}
B. #{array_name[index]}
C. $${array_name[index]}
D. $[array_name{index}]
View Answer
Ans : A
Explanation: After you have set any array variable, you access it as follows : ${array_name[index]}
6. A shell variable is capable enough to hold a single value. These variables are called __________________.
A. vector variables
B. scalar variables
C. array variables
D. None of the above
View Answer
Ans : B
Explanation: A shell variable is capable enough to hold a single value. These variables are called scalar variables.
7. Which of the following syntax is used to access all the items in an array?
A. ${array_name[*]}
B. ${array_name[@]}
C. ${array_name[$]}
D. Both A and B
View Answer
Ans : D
Explanation: Option A and Option B is used access all the items in an array.
8. Which of the following code is used to print elements in range?
A. ${arr[@]:1:4}
B. ${arr[@]}
C. ${arr[*]}
D. Both A and C
View Answer
Ans : A
Explanation: ${arr[@]:1:4} will print elements in range.
9. Which of the following code will count Length of in Array?
A. ${#arr[0]}
B. ${#arr}
C. ${#arr[#]}
D. Both A and B
View Answer
Ans : D
Explanation: Option A and Option B code will count Length of in Array.
10. Which of the following is correct code to search in array?
A. ${arr[@]/*[aA]*/}
B. ${arr[#]/*[aA]*/}
C. ${arr[*]/*[aA]*/}
D. Both A and C
View Answer
Ans : D
Explanation: Option A and Option C is correct code to search in array.
Discussion