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

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

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

Answer: a
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()
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

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

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

Answer: a
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#
advertisement

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

Answer: c
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#
advertisement

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

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

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

Answer: a
Explanation: None.
Output:
root@ubuntu:/home/sanfoundry# ./test.awk
SANFOUNDRY_1_$
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.

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.