# Awk Programming Questions & Answers – Functions

«
»
This set of Linux / Unix questions and answers focuses on Functions in Awk Programming.

1. Which one of the following statement is not true?
a) awk’s built-in functions can be categorised into three categories: numeric, string and I/O
b) built-in functions are always available to call
c) extra arguments to built-in function causes syntax error
d) we can also define the function in awk program

Explanation: Extra arguments to built-in function causes fatal error.

2. What is the difference between the built-in functions rand() and srand() in awk programming?
a) rand() generates the random number but srand() does not generate the random number
b) rand() generates the same random number always whenever the program runs but srand() generates the different
c) srand() requires the seed() function but rand() does not require to produce the same result
d) none of the mentioned

Explanation: None.

3. Which built-in function returns the arctangent of a/b in radians.
a) atan2(a/b)
b) atan(a/b)
c) atan_2(a/b)
d) none of the mentioned

Explanation: None.

4. Which built-in function divides string into pieces seperated by fieldsep and stores the pieces in array?
a) split()
b) divide()
c) index()
d) sub()

Explanation: None.

5. The built-in function tolower()
a) converts all lowercase characters into uppercase characters
b) returns the string with all lowercase characters
c) changes the nonalphabetic characters
d) none of the mentioned

Explanation: None.

6. What is the output of this program?

1.    #! /usr/bin/awk -f
2.    BEGIN {
3.        print log(1)
4.    }

a) 0
b) syntax error
c) fatal error
d) segmentation fault

Explanation: The function log() is built-in function and need not to be defined. This function calculates the natural logarithm of positive numbers.
Output:
root@ubuntu:/home/sanfoundry# ./test.awk
0
root@ubuntu:/home/sanfoundry#

7. What is the output of this program?

1.    #! /usr/bin/awk -f
2.    BEGIN {
3.        print index("sanfoundry","linux")
4.    }

a) sanfoundry linux
b) sanfoundry
c) 0
d) none of the mentioned

Explanation: The function index() searches the string “sanfoundry” for the first occurence of the string “linux”. The function usually returns the character position but here the string is not found, hence function returns 0.
Output:
root@ubuntu:/home/sanfoundry# ./test.awk
0
root@ubuntu:/home/sanfoundry#

8. What is the output of this pogram?

1.    #! /usr/bin/awk -f
2.    BEGIN {
3.        system("date")
4. 	   print "sanfoundry"
5.    }

a) the program will execute the date command and then program will print “sanfoundry”
b) program will generate fatal error because function system() is neither defined nor built-in function
c) program will generate syntax error because the syntax of function system() is wrong
d) none of the mentioned

Explanation: The function system() allow to execute operating system command and then return to the awk program.
Output:
root@ubuntu:/home/sanfoundry# ./test.awk
Wed Apr 17 10:22:47 IST 2013
sanfoundry
root@ubuntu:/home/sanfoundry#

9. What is the output of the program?

1.    #! /usr/bin/awk -f
2.    BEGIN {
3.        a=int(2.5)
4.        print (a*20)
5.    }

a) 50
b) 40
c) a*20
d) syntax error

Explanation: The function int() returns the integer part of the value. In this program it returns 2.
Output:
root@ubuntu:/home/sanfoundry# ./test.awk
40
root@ubuntu:/home/sanfoundry#

10. What is the output of the program?

1.     #! /usr/bin/awk -f
2.     BEGIN {
3.        print toupper("sAnFoUnDrY_1_\$")
4.     }

a) SANFOUNDRY_1_\$
b) sAnFoUnDrY 1 \$
c) SaNfOuNdRy_1_\$
d) SANFOUNDRY

Explanation: None.
Output:
root@ubuntu:/home/sanfoundry# ./test.awk
SANFOUNDRY_1_\$
root@ubuntu:/home/sanfoundry#

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

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