Awk Programming Questions & Answers – Variables & Array

This set of Linux / Unix questions and answers focuses on Variables and Arrays in Awk Programming.

1. In awk, the built-in variable FS is
a) input field seperator
b) output field seperator
c) record seperator
d) subscript seperator
View Answer

Answer: a
Explanation: None.

2. What is FNR?
a) FNR is the current record number in the current file
b) FNR is the number of fields in the current input record
c) FNR is an array contains the value of environment
d) None of the mentioned
View Answer

Answer: a
Explanation: None.

3. RSTART is set by invoking the
a) match function
b) index function
c) asort function
d) split function
View Answer

Answer: a
Explanation: None.

4. Which one of the following is used by awk to control the conversion of numbers to string?
a) RS
b) OFMT
c) SUBSEP
d) RSTART
View Answer

Answer: b
Explanation: None.
advertisement
advertisement

5. In awk program, the name of the array can not be same with the
a) name of variable
b) value of the array element
c) name of variable & value of the array element
d) none of the mentioned
View Answer

Answer: a
Explanation: None.

6. What is the output of the program?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1.    #! /usr/bin/awk -f
  2.    #This filename is text.awk
  3.    BEGIN {
  4.        print FILENAME
  5.    }

a) test.awk
b) program will print nothing
c) syntax error
d) fatal error
View Answer

Answer: b
Explanation: The built-in variable FILENAME is the name of file that awk is currently reading and in this program there is no file listed on the command line.
Output:
root@ubuntu:/home/sanfoundry# ./test.awk

advertisement

root@ubuntu:/home/sanfoundry#

7. What is the output of the program?

advertisement
  1.    #! /usr/bin/awk -f
  2.    BEGIN {
  3.        a[1]="sanfoundry"
  4.        a[2]="sanfoundry"
  5.        for(i=1;i<3;i++) {
  6.            print a[i]
  7.        }
  8.    }

a) “sanfoundry” will print 2 times
b) “sanfoundry” will print 3 times
c) program will generate error becasue 2 array elements have the same value
d) program will generate syntax error
View Answer

Answer: a
Explanation: None.
Output:
root@ubuntu:/home/sanfoundry# ./test.awk
sanfoundry
sanfoundry
root@ubuntu:/home/sanfoundry#

8. What is the output of the program?

  1.    #! /usr/bin/awk -f
  2.    BEGIN {
  3.        a[1]="sanfoundry"
  4.        delete a[1]
  5.        print a[1]
  6.    }

a) program will print “sanfoundry”
b) program will print nothing
c) program will generate syntax error
d) program will generate fatal error
View Answer

Answer: b
Explanation: The delete command deletes the array element.
Output:root@ubuntu:/home/sanfoundry# ./test.awk

root@ubuntu:/home/sanfoundry#

9. What is the output of the program?

  1.    #! /usr/bin/awk -f
  2.    BEGIN {
  3.        a["linux","MCQ"]="sanfoundry"
  4.        print a["linux","MCQ"]	   
  5.    }

a) sanfoundry
b) linux MCQ
c) a[“linux”,”MCQ”].
d) syntax error
View Answer

Answer: a
Explanation: None.
Output:
root@ubuntu:/home/sanfoundry# ./test.awk
sanfoundry
root@ubuntu:/home/sanfoundry#

10. What is the output of the program?

  1.     #! /usr/bin/awk -f
  2.     BEGIN {
  3.         a[1,1]=0
  4.         a[1,2]=1
  5.         a[2,1]=2
  6.         a[2,2]=3
  7. 	for(i=1;i<3;i++) {
  8. 	    for(j=1;j<3;j++) {
  9. 	    print a[i,j]
  10. 	    }
  11. 	}
  12.     }

a) 0 1 2 3
b) 0 2
c) 1 3
d) syntax error
View Answer

Answer: b
Explanation: None.
Output:
root@ubuntu:/home/sanfoundry# ./test.awk
0
2
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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.