Awk Programming Questions & Answers – Expressions

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

1. What is expression in awk programming?
a) expression evaluates a value to print, test or pass to a function
b) expression assigns a new value to a variable or field
c) expression evaluates a value to print, test or pass to a function & also assigns a new value to a variable or field
d) none of the mentioned
View Answer

Answer: c
Explanation: None.

2. Which one of the following is not true?
a) there are 3 types of constant expressions: numeric, string and regular
b) arithmetic operators are used to evaluate expressions
c) assignment expression is an expression that stores a value into a variable
d) comparison expressions does not compare strings for relationship
View Answer

Answer: d
Explanation: None.

3. All numeric values are represented within awk in
a) double precision floating point
b) integer
c) exponential notation
d) fixed point
View Answer

Answer: a
Explanation: None.

4. Concatenation is performed by
a) writing expressions next to one another, with no operator
b) conditional operator
c) relational operator
d) matching operator
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

5. The comparison expression “x ~ y” will true if
a) x is not equal to y
b) the string x does not match the regular expression denoted by y
c) the string x matches the regular expression denoted by y
d) none of the mentioned
View Answer

Answer: c
Explanation: None.

6. What is the output of this program?

  1.    #! /usr/bin/awk -f
  2.    BEGIN {
  3.        print "20"<"9" ? "true":"false"	      
  4.    }

a) true
b) false
c) syntax error
d) none of the mentioned
View Answer

Answer: a
Explanation: The operands of relational operators are converted to, and compared as string if both are not numbers. Strings are compared by comparing the characters of each. Hence 20 is less then 9.
Output:
root@ubuntu:/home/sanfoundry# chmod +x test.awk
root@ubuntu:/home/sanfoundry# ./test.awk
true
root@ubuntu:/home/sanfoundry#
advertisement

7. What is the output of this program?

  1.    #! /usr/bin/awk -f
  2.    BEGIN {
  3.        a=10;
  4.        b=10;
  5.        print a==b ? "true":"false"	      
  6.    }

a) true
b) false
c) syntax error
d) none of the mentioned
View Answer

Answer: a
Explanation: None.
Output:
root@ubuntu:/home/sanfoundry# chmod +x test.awk
root@ubuntu:/home/sanfoundry# ./test.awk
true
root@ubuntu:/home/sanfoundry#
advertisement

8. What is the output of this program?

  1.    #! /usr/bin/awk -f
  2.    BEGIN {
  3.        var1="sanfoundry"
  4.        var2="linux"
  5.        print var1" provides "var2" MCQs "	      
  6.    }

a) sanfoundry provides linux MCQs
b) var1 provides var2 MCQs
c) provides MCQs
d) syntax error
View Answer

Answer: a
Explanation: None.
Output:
root@ubuntu:/home/sanfoundry# chmod +x test.awk
root@ubuntu:/home/sanfoundry# ./test.awk
sanfoundry provides linux MCQs
root@ubuntu:/home/sanfoundry#

9. What is the output of this program?

  1.    #! /usr/bin/awk -f
  2.    BEGIN {
  3.        two=2;
  4.        two++;
  5.        print two 
  6.    }

a) two
b) three
c) 2
d) 3
View Answer

Answer: d
Explanation: None.
Output:
root@ubuntu:/home/sanfoundry# chmod +x test.awk
root@ubuntu:/home/sanfoundry# ./test.awk
3
root@ubuntu:/home/sanfoundry#

10. What is the output of this program?

  1.     #! /usr/bin/awk -f
  2.     BEGIN {
  3.         one=10;
  4.         two=3;
  5.         print (one%two)+10 
  6.     }

a) (one%two)+10
b) 13
c) 11
d) syntax error
View Answer

Answer: c
Explanation: The remainder of 10/3 is 1. remainder is added to 10.
Output:
root@ubuntu:/home/sanfoundry# chmod +x test.awk
root@ubuntu:/home/sanfoundry# ./test.awk
11
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.

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.