Linux Shell Programming Questions and Answers on Functions for Freshers

https://www.computersprofessor.com/2018/07/linux-shell-programming-questions-and.html
1. When the return value of any function is not specified within the function, what function returns?
a) nothing
b) exit status of the last command executed
c) 0
d) none of the mentioned
Answer: b
2. Parameters can be passed to a function
a) by using the parameter variables $1, $2, $3…….
b) by using the environment variables
c) by using the parameter & environment variables
d) none of the mentioned
Answer: a
3. Which of the following command provides the list of the functions defined in the login session?
a) declare -f
b) declare -F
c) both declare -f and -F
d) none of the mentioned
Answer: c
Explanation:’declare -F’ provides just the name of the functions and ‘declare -f’ provides their definitions also.
4. The keyword ‘local’ is used
a) to define a variable within a function for its local scope
b) to redefine any global variable
c) this is not a valid keyword
d) none of the mentioned
Answer: a
5. Functions improves the shell’s programmability significantly, because
a) when we invoke a function, it is already in the shell’s memory, therefore a function runs faster than seperate scripts
b) function will not provides a piece of code for repetative tasks
c) all of the mentioned
d) none of the mentioned
Answer: a
6. What is the output of this program?
#!/bin/sh
var="Sanfoundry"
san_function() {
var="Linux"
echo $var
}
san_function
exit 0
a) Sanfoundry
b) Linux
c) Command not found
d) None of the mentioned
b) Linux
c) Command not found
d) None of the mentioned
Answer: b
Explanation: If local variable name is same as the global variable, it overlays the variable, but only within the function.
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
Linux
root@ubuntu:/home/sanfoundry#
7. What is the output of this program?
#!/bin/sh
san_function() {
echo "Welcome to the Sanfoundry"
printf "World of Linux\n"
}
unset -f san_function
san_function
exit 0
a) Welcome to the Sanfoundry
b) World of Linux
c) both Welcome to the Sanfoundry and World of Linux
d) nothing will print
b) World of Linux
c) both Welcome to the Sanfoundry and World of Linux
d) nothing will print
Answer: d
Explanation: Function definition was deleted before calling the function. command ‘unset -f function_name’ deletes the function definition.
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
./test.sh: 6: san_function: not found
root@ubuntu:/home/sanfoundry#
8. What is the output of this program?
#!/bin/bash
function san_function1 {
echo "This is first function"
}
san_function2() {
echo "This is second function"
}
san_function1
san_function2
exit 0
a) This is the first function
b) This is the second function
c) This is the first function
This is the second function
d) program will generate error because first function definition is not correct
b) This is the second function
c) This is the first function
This is the second function
d) program will generate error because first function definition is not correct
Answer: c
Explanation: In bash shell, functions can be defined in both the ways, used in the script.
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
This is first function
This is second function
root@ubuntu:/home/sanfoundry#
9. What is the output of this program?
#!/bin/sh
echo "Just call the function"
san_function
san_function() {
echo "This is a function"
}
exit 0
a) only first string will print without any error
b) only second string will print without any error
c) both strings will print
d) none of the mentioned
b) only second string will print without any error
c) both strings will print
d) none of the mentioned
Answer: d
Explanation: Function must be defined prior to call. Hence only first string will print and program will generate an error also.
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
Just call the function
./test.sh: 3: san_function: not found
root@ubuntu:/home/sanfoundry#
10. What is the output of this program?
#!/bin/sh
san_function1() {
a=5
echo "This is the first function"
san_function2
}
san_function2() {
echo "This is the second function"
san_function3
}
san_function3() {
echo "This is the third function"
}
san_function1
exit 0
a) This is the first function
This is the second function
This is the third function
b) This is the first function
This is the third function
This is the second function
c) This is the second function
This is the first function
This is the third function
d) This is the third function
This is the first function
This is the second function
This is the second function
This is the third function
b) This is the first function
This is the third function
This is the second function
c) This is the second function
This is the first function
This is the third function
d) This is the third function
This is the first function
This is the second function
Answer: a
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
This is the first function
This is the second function
This is the third function
root@ubuntu:/home/sanfoundry#
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
This is the first function
This is the second function
This is the third function
root@ubuntu:/home/sanfoundry#