Awk Programming Interview Preparation Guide Download PDF
Awk Programming frequently Asked Questions in various Awk Programming job Interviews by interviewer. The set of questions here ensures that you offer a perfect answer posed to you. So get preparation for your new job hunting
40 Awk Programming Questions and Answers:
Table of Contents
2 :: What is the output of this program?
#! /usr/bin/awk -f
BEGIN {
two=2;
two++;
print two
}
a) two
b) three
c) 2
d) 3
d) 3
Output:
root@ubuntu:/home/google# chmod +x test.awk
root@ubuntu:/home/google# ./test.awk
3
root@ubuntu:/home/google#
Output:
root@ubuntu:/home/google# chmod +x test.awk
root@ubuntu:/home/google# ./test.awk
3
root@ubuntu:/home/google#
3 :: What is the output of this program?
#! /usr/bin/awk -f
BEGIN {
var1="google"
var2="linux"
print var1" provides "var2" MCQs "
}
a) google provides linux MCQs
b) var1 provides var2 MCQs
c) provides MCQs
d) syntax error
a) google provides linux MCQs
Output:
root@ubuntu:/home/google# chmod +x test.awk
root@ubuntu:/home/google# ./test.awk
google provides linux MCQs
root@ubuntu:/home/google#
Output:
root@ubuntu:/home/google# chmod +x test.awk
root@ubuntu:/home/google# ./test.awk
google provides linux MCQs
root@ubuntu:/home/google#
4 :: What is the output of this program?
#! /usr/bin/awk -f
BEGIN {
one=10;
two=3;
print (one%two)+10
}
a) (one%two)+10
b) 13
c) 11
d) syntax error
c) 11
Explanation:
The remainder of 10/3 is 1. remainder is added to 10.
Output:
root@ubuntu:/home/google# chmod +x test.awk
root@ubuntu:/home/google# ./test.awk
11
root@ubuntu:/home/google#
Explanation:
The remainder of 10/3 is 1. remainder is added to 10.
Output:
root@ubuntu:/home/google# chmod +x test.awk
root@ubuntu:/home/google# ./test.awk
11
root@ubuntu:/home/google#
5 :: What is the output of this program?
#! /usr/bin/awk -f
BEGIN {
a=10;
b=10;
print a==b ? "true":"false"
}
a) true
b) false
c) syntax error
d) none of the mentioned
a) true
Output:
root@ubuntu:/home/google# chmod +x test.awk
root@ubuntu:/home/google# ./test.awk
true
root@ubuntu:/home/google#
Output:
root@ubuntu:/home/google# chmod +x test.awk
root@ubuntu:/home/google# ./test.awk
true
root@ubuntu:/home/google#
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 the mentioned
c) the string x matches the regular expression denoted by y
7 :: What is the output of this program?
#! /usr/bin/awk -f
BEGIN {
print "20"<"9" ? "true":"false"
}
a) true
b) false
c) syntax error
d) none of the mentioned
a) true
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/google# chmod +x test.awk
root@ubuntu:/home/google# ./test.awk
true
root@ubuntu:/home/google#
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/google# chmod +x test.awk
root@ubuntu:/home/google# ./test.awk
true
root@ubuntu:/home/google#
8 :: All numeric values are represented within awk in:
a) double precision floating point
b) integer
c) exponential notation
d) fixed point
a) double precision floating point
9 :: Concatenation is performed by:
a) writing expressions next to one another, with no operator
b) conditional operator
c) relational operator
d) matching operator
a) writing expressions next to one another, with no operator
10 :: Which command on the command line provides the same output as this executable awk script?
#! /usr/bin/awk -f
BEGIN {
print "google"
}
a) awk 'BEGIN {print "google"}'
b) awk 'print "google"'
c) awk 'print {google}'
d) none of the mentioned
a) awk 'BEGIN {print "google"}'
11 :: 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
d) comparison expressions does not compare strings for relationship
12 :: What is the output of the command awk 'BEGIN {printf "%cn",65}'
a) A
b) 65
c) syntax error
d) none of the mentioned
a) A
Explanation:
The ASCII value of A is 65.
Explanation:
The ASCII value of A is 65.
13 :: The command "awk {print $1} san.txt" will:
a) print the first line of file san.txt
b) print the first field of every line in san.txt
c) generate syntax error
d) none of the mentioned
b) print the first field of every line in san.txt
14 :: Which one of the following statement is not true about the format-control letters for printf statement in awk program?
a) "c" prints a number as an ASCII character
b) "d" prints a decimal integer
c) "h" prints an unsigned hexadecimal integer
d) "o" prints an unsigned octal integer
c) "h" prints an unsigned hexadecimal integer
Explanation:
"x" prints and unsigned hexadecimal integer
Explanation:
"x" prints and unsigned hexadecimal integer
15 :: Which one of the following is not true?
a) in typical awk program, all input is read either from standard input or specified files
b) awk language divides its input into records and fields
c) awk reads an input record and the record is automatically seperated by the interpreter into pieces called "fields"
d) the number of fields need to be a constant
d) the number of fields need to be a constant
Explanation:
The number of fields does not need to be a constant.
Explanation:
The number of fields does not need to be a constant.
16 :: In awk program, the statement "print" with no items:
a) is equivalent to "print $0″
b) prints the entire current record
c) both (a) and (b)
d) none of the mentioned
c) both (a) and (b)
18 :: An awk program can be run by:
a) including the program in the command that runs awk
b) putting it into a file and run with a command
c) running an executable awk script
d) all of the mentioned
d) all of the mentioned
Explanation:
The method used to run awk program depends on the program size and input files.
Explanation:
The method used to run awk program depends on the program size and input files.
19 :: Which one of the following is not true?
a) nawk is the new version of awk
b) gawk is the GNU version of awk
c) linux users have the gawk
d) nawk does not provide the additional capabilities in comparison of awk
d) nawk does not provide the additional capabilities in comparison of awk
20 :: What is the output of the program?
#! /usr/bin/awk -f
BEGIN {
a[1,1]=0
a[1,2]=1
a[2,1]=2
a[2,2]=3
for(i=1;i<3;i++) {
for(j=1;j<3;j++) {
print a[i,j]
}
}
}
a) 0 1 2 3
b) 0 2
c) 1 3
d) syntax error
b) 0 2
Output:
root@ubuntu:/home/google# ./test.awk
0
2
root@ubuntu:/home/google#
Output:
root@ubuntu:/home/google# ./test.awk
0
2
root@ubuntu:/home/google#
21 :: What is the output of the program?
#! /usr/bin/awk -f
BEGIN {
a["linux","MCQ"]="google"
print a["linux","MCQ"]
}
a) google
b) linux MCQ
c) a["linux","MCQ"]
d) syntax error
a) google
Output:
root@ubuntu:/home/google# ./test.awk
google
root@ubuntu:/home/google#
Output:
root@ubuntu:/home/google# ./test.awk
root@ubuntu:/home/google#
22 :: What is the output of the program?
#! /usr/bin/awk -f
BEGIN {
a[1]="google"
a[2]="google"
for(i=1;i<3;i++) {
print a[i]
}
}
a) "google" will print 2 times
b) "google" will print 3 times
c) program will generate error becasue 2 array elements have the same value
d) program will generate syntax error
a) "google" will print 2 times
Output:
root@ubuntu:/home/google# ./test.awk
google
google
root@ubuntu:/home/google#
Output:
root@ubuntu:/home/google# ./test.awk
root@ubuntu:/home/google#
23 :: Define the output of the program?
#! /usr/bin/awk -f
BEGIN {
a["linux","MCQ"]="google"
print a["linux","MCQ"]
}
a) google
b) linux MCQ
c) a["linux","MCQ"]
d) syntax error
a) google
Output:
root@ubuntu:/home/google# ./test.awk
google
root@ubuntu:/home/google#
Output:
root@ubuntu:/home/google# ./test.awk
root@ubuntu:/home/google#
24 :: What is the output of the program?
#! /usr/bin/awk -f
#This filename is text.awk
BEGIN {
print FILENAME
}
a) test.awk
b) program will print nothing
c) syntax error
d) fatal error
b) program will print nothing
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/google# ./test.awk
root@ubuntu:/home/google#
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/google# ./test.awk
root@ubuntu:/home/google#