Linux Shell Programming Questions and Answers – Functions

This set of Linux / Unix questions and answers focuses on Functions in Linux Shell Programming.

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
View Answer

Answer: b
Explanation: None.

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
View Answer

Answer: a
Explanation: None.

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
View Answer

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
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

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
View Answer

Answer: a
Explanation: None.

6. What is the output of this program?

Note: Join free Sanfoundry classes at Telegram or Youtube
  1.    #!/bin/sh	
  2.    var="Sanfoundry"
  3.    san_function() {
  4.        var="Linux"
  5.        echo $var
  6.    }
  7.    san_function
  8.    exit 0

a) Sanfoundry
b) Linux
c) Command not found
d) None of the mentioned
View Answer

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#
advertisement

7. What is the output of this program?

  1.    #!/bin/sh
  2.    san_function() {
  3.        echo "Welcome to the Sanfoundry"
  4.        printf "World of Linux\n"
  5.    }
  6.    unset -f san_function
  7.    san_function
  8.    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
View Answer

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#
advertisement

8. What is the output of this program?

  1.    #!/bin/bash
  2.    function san_function1 {
  3.        echo "This is first function"	
  4.    }
  5.    san_function2() {
  6.        echo "This is second function"
  7.    }
  8.    san_function1
  9.    san_function2
  10.    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
View Answer

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?

  1.    #!/bin/sh
  2.    echo "Just call the function"
  3.    san_function
  4.    san_function() {
  5.       echo "This is a function"
  6.    }
  7.    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
View Answer

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?

  1.    #!/bin/sh
  2.    san_function1() {
  3.        a=5
  4.        echo "This is the first function"
  5.        san_function2
  6.    }
  7.    san_function2() {
  8.        echo "This is the second function" 
  9.        san_function3
  10.    }
  11.    san_function3() {
  12.        echo "This is the third function"
  13.    }
  14.    san_function1
  15.    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
View Answer

Answer: a
Explanation: None.
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#

Sanfoundry Global Education & Learning Series – Linux Administration & Programming.

Here’s the list of Best Books in Linux Commands & Shell Programming.
Here’s the list of Best Books in Linux Kernel, Device-Drivers & System Programming.

To practice all questions on Linux Administration & Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on Linux.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.