Awk Programming Questions & Answers – Expressions

DigitalOcean Referral Badge DigitalOcean Referral Badge

1.

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

Answer: Option 'C'

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#

DigitalOcean Referral Badge

2.

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

Answer: Option 'D'

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

DigitalOcean Referral Badge

3.

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

Answer: Option 'A'

sanfoundry provides linux MCQs

root@ubuntu:/home/sanfoundry# chmod +x test.awk
root@ubuntu:/home/sanfoundry# ./test.awk
sanfoundry provides linux MCQs
root@ubuntu:/home/sanfoundry#

DigitalOcean Referral Badge

4.

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 these

Answer: Option 'A'

true

DigitalOcean Referral Badge
    DigitalOcean Referral Badge DigitalOcean Referral Badge

5.

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

Answer: Option 'A'

true

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#

DigitalOcean Referral Badge

6.

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 these

Answer: Option 'C'

the string x matches the regular expression denoted by y

DigitalOcean Referral Badge

7.

Concatenation is performed by

   A.) writing expressions next to one another, with no operator
   B.) conditional operator
   C.) relational operator
   D.) matching operator

Answer: Option 'A'

writing expressions next to one another, with no operator

DigitalOcean Referral Badge

8.

All numeric values are represented within awk in

   A.) double precision floating point
   B.) integer
   C.) exponential notation
   D.) fixed point

Answer: Option 'A'

double precision floating point

DigitalOcean Referral Badge

9.

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

Answer: Option 'D'

comparison expressions does not compare strings for relationship

DigitalOcean Referral Badge
    DigitalOcean Referral Badge DigitalOcean Referral Badge

10.

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 these

Answer: Option 'C'

expression evaluates a value to print, test or pass to a function & also assigns a new value to a variable or field

DigitalOcean Referral Badge

Awk Programming Questions & Answers – Expressions Download Pdf

Recent Posts