Linux Shell Programming Questions and Answers on Functions for Freshers

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?
  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
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?
  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
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?
  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

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

Related

Visual Basic Multiple Choice Questions

Top 80 Visual Basic Multiple Choice Questions 1. The Visual Basic Code Editor will automatically detect certain types of errors as you are entering code. A. TrueB. False Ans: A 2. Keywords are a...

Data Structures Multiple Choice Questions and Answers on Stack using Array

1. Which of the following real world scenarios would you associate with a stack data structure? a) piling up of chairs one above the otherb) people standing in a line to be serviced at a counterc) ...

Data Structures Multiple Choice Questions and Answers on Inorder Traversal

1. For the tree below, write the in-order traversal. a) 2, 7, 2, 6, 5, 11, 5, 9, 4b) 2, 7, 5, 2, 6, 9, 5, 11, 4c) 2, 5, 11, 6, 7, 4, 9, 5, 2d) 2, 7, 5, 6, 11, 2, 5, 4, 9 Answer: d Explanation: In...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item