250+ TOP MCQs on Functions and Answers

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

Answer: b
Clarification: 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

Answer: a
Clarification: 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

Answer: c
Clarification:’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
Clarification: None.

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
Clarification: None.

6. What is the output of this program?

  1.    #!/bin/sh	
  2.    var=""
  3.    san_function() {
  4.        var="Linux"
  5.        echo $var
  6.    }
  7.    san_function
  8.    exit 0

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

Answer: b

7. What is the output of this program?

  1.    #!/bin/sh
  2.    san_function() {
  3.        echo "Welcome to the "
  4.        printf "World of Linuxn"
  5.    }
  6.    unset -f san_function
  7.    san_function
  8.    exit 0

a) Welcome to the
b) World of Linux
c) both Welcome to the and World of Linux
d) nothing will print

Answer: d

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

Answer: c

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

Answer: d

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

Answer: a

Leave a Reply

Your email address will not be published. Required fields are marked *