Awk Programming Questions & Answers – Control Statements

This set of Linux / Unix questions and answers focuses on Control Statements in Awk Programming.

1. The break statement
a) jumps out of the innermost for loop
b) jumps out of the innermost while loop
c) jumps out of the innermost do-while loop
d) all of the mentioned
View Answer

Answer: d
Explanation: None.

2. Which statement skips over the rest of the loop body, causing the next cycle around the loop to begin immediately?
a) continue
b) break
c) next
d) none of the mentioned
View Answer

Answer: a
Explanation: None.

3. The next statement
a) immediately stops processing the current record
b) go to the next record
c) immediately stops processing the current record & go to the next record
d) none of the mentioned
View Answer

Answer: c
Explanation: None.

4. If the argument is supplied to the exit statement,
a) its value is used as the exit status code for the awk process
b) syntax error will generate
c) exit returns status 0
d) exit returns status 1
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

5. Which statement instructs gawk to stop processing the current data file?
a) next
b) nextfile
c) exit
d) exitfile
View Answer

Answer: b
Explanation: None.

6. What is the output of this program?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1.    #! /usr/bin/awk -f
  2.    BEGIN {
  3.        a=5
  4.        while (a<5) {
  5.            print "sanfoundry"
  6.            a++;
  7.        }
  8.    }

a) nothing will print
b) “sanfoundry” will print 5 times
c) program will generate syntax error
d) none of the mentioned
View Answer

Answer: a
Explanation: The condition of while statement is false so commands inside the loop will not execute.
Output:
root@ubuntu:/home/sanfoundry# ./test.awk
root@ubuntu:/home/sanfoundry#
advertisement

7. What is the output of this program?

  1.    #! /usr/bin/awk -f
  2.    BEGIN {
  3.        a=0
  4.        do {
  5.            print "sanfoundry"
  6.            a++
  7.        } while (a<5)
  8.    }

a) “sanfoundry” will print 4 times
b) “sanfoundry” will print 5 times
c) nothing will print
d) syntax error
View Answer

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

8. What is the output of this program?

  1.    #! /usr/bin/awk -f
  2.    BEGIN {
  3.        a=6
  4.        do {
  5.            print "sanfoundry"
  6.            a++
  7.        } while (a<5)
  8.    }

a) nothing will print
b) “sanfoundry” will print 5 times
c) “sanfoundry” will print 4 times
d) “sanfoundry” will print only 1 time
View Answer

Answer: d
Explanation: Even the condition is false of do-while loop, the body is executed once.
Output:
root@ubuntu:/home/sanfoundry# ./test.awk
sanfoundry
root@ubuntu:/home/sanfoundry#

9. What is the output of this program?

  1.    #! /usr/bin/awk -f
  2.    BEGIN {
  3.        for(i=0;i<=5;i++) {
  4.            print i
  5.            i++
  6.        }
  7.    }

a) 0,2,4 will print
b) 1,3,5 will print
c) 1,2,3,4,5 will print
d) syntax error because i is not initialised
View Answer

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

10. The command “awk ‘{if (“9″>”10”) print “sanfoundry” else print “linux”}'”
a) will print “sanfoundry”
b) will print “linux”
c) will generate syntax error
d) none of the mentioned
View Answer

Answer: c
Explanation: Semicolon is required just before the else statement to parse the statement.
Output:
root@ubuntu:/home/sanfoundry# awk ‘{if (“9″>”10”) print “sanfoundry” else print “linux”}’
awk: {if (“9″>”10”) print “sanfoundry” else print “linux”}
awk: ^ syntax error
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.