250+ TOP MCQs on Functions and Answers

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
Answer: c
Clarification: 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
Answer: b
Clarification: 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
Answer: a
Clarification: 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()
Answer: a
Clarification: 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
Answer: b
Clarification: 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
Answer: a
Clarification: The function log() is built-in function and need not to be defined. This function calculates the natural logarithm of positive numbers.
Output:
[email protected]:/home/# ./test.awk
0
[email protected]:/home/#

7. What is the output of this program?

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

a) linux
b)
c) 0
d) none of the mentioned
Answer: c
Clarification: The function index() searches the string “” 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:
[email protected]:/home/# ./test.awk
0
[email protected]:/home/#

8. What is the output of this pogram?

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

a) the program will execute the date command and then program will print “”
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
Answer: a
Clarification: The function system() allow to execute operating system command and then return to the awk program.
Output:
[email protected]:/home/# ./test.awk
Wed Apr 17 10:22:47 IST 2013

[email protected]:/home/#

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
Answer: b
Clarification: The function int() returns the integer part of the value. In this program it returns 2.
Output:
[email protected]:/home/# ./test.awk
40
[email protected]:/home/#

10. What is the output of the program?

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

a) _1_$
b) 1 $
c) _1_$
d)
Answer: a
Clarification: None.
Output:
[email protected]:/home/# ./test.awk
_1_$
[email protected]:/home/#

contest

Leave a Reply

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