MCQs > IT & Programming > Unix Shell Script MCQs > Basic Unix Shell Script MCQs

Basic Unix Shell Script MCQ

1. Which of the following shows the correct precedence order of operators used in a shell? Operators on left side of comma have higher precedence than those on the right side of comma.

Answer

Correct Answer: ( ) , ** , * or / , + or -

Note: This Question is unanswered, help us to find answer for this one

2.

Suppose there are three files test0.sh, test1.sh, and test2.sh in your current directory. Which of the following options would occur during the execution of read statement?


exec < test0.sh

exec < test2.sh

exec < test3.sh

read line

echo $line

Answer

Correct Answer:

It would read only test3.sh 


Note: This Question is unanswered, help us to find answer for this one

3. Which of the following expressions is valid?

Answer

Correct Answer: tput cup $row $col

Note: This Question is unanswered, help us to find answer for this one

4.

What is the output of the following program?

k=35

echo `[ $k -eq 35 ]``[ $k -eq 50 ]`

Answer

Correct Answer:

A blank line will result


Note: This Question is unanswered, help us to find answer for this one

5.

What is the output of the following program when directory name "home" is given as input?

echo Enter a directory Name

read dirname

case $dirname in

    *) echo any directory name ;;

    c*) echo cobol directory name ;;

    f*) echo fortran directory name ;;

    p*) echo pascal directory name ;;

esac

Answer

Correct Answer:

any directory name


Note: This Question is unanswered, help us to find answer for this one

6.

What is the output of the following program?

1.    x=3 y=5 z=10

 2.    if [ \($x -eq 3\) -a \( $y -eq 5 -o $z -eq 10 \) ]

3.    then

4.    echo $x

5.    else

6.    echo $y

7.    fi

Answer

Correct Answer:

5 and a warning about line 2 will occur


Note: This Question is unanswered, help us to find answer for this one

7.

Suppose you are writing a shell script that accepts five positional parameters from the terminal. What will be the effect of using statement "shift 1" in your shell script and then executing the shell script?

Answer

Correct Answer:

The positional parameters would be shifted by one position towards left i.e. parameter 1 will have value of parameter 2, parameter 2 will have value of parameter 3 and so on. 


Note: This Question is unanswered, help us to find answer for this one

8. Which of the following loops is invalid?

Answer

Correct Answer: while $

Note: This Question is unanswered, help us to find answer for this one

9.

What is the error in the following program?

1.    j=10 k=12

 2.    if test [ $k -ge $j ]

3.    then

4.        k=$j

5.        j=$k

6.    fi

7.    echo $j $k

Answer

Correct Answer:

Output will be 10 12 with a warning message in line 2 


Note: This Question is unanswered, help us to find answer for this one

10.

What will be the output of the following program assuming that the command line arguments are Unix shell scripting?

for argument in *

do

    echo $argument

done

Answer

Correct Answer:

The names of all files in the current directory would be displayed 


Note: This Question is unanswered, help us to find answer for this one

11.

What is the output of the following program?

 for i in a b c d e

do

   echo $i

done

Answer

Correct Answer:

a b c d e


a b c d e


Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

12.

What is the error in the following program?

  1.     j=1

2.     while [ $j -le 10 ]

3.     do

4.     echo $j

5.     j = j + 1

6.     done

Answer

Correct Answer:

Line 5 should be j=`expr $j + 1`


Note: This Question is unanswered, help us to find answer for this one

13. Which of the following is/are correct declaration(s) of a null variable "a" in shell script?

Answer

Correct Answer: All of the above.

Note: This Question is unanswered, help us to find answer for this one

14. A shell script is executed by:

Answer

Correct Answer: an interpreter.

Note: This Question is unanswered, help us to find answer for this one

15.

On executing the statement:

        set -3 + 1

Answer

Correct Answer:

An error would occur  


Note: This Question is unanswered, help us to find answer for this one

16. Which of the following assignments is illegal?

Answer

Correct Answer: c=`1972`

Note: This Question is unanswered, help us to find answer for this one

17. Which of the following variable names is invalid?

Answer

Correct Answer: #regpay

Note: This Question is unanswered, help us to find answer for this one

18. The expression expr -2 % 7 evaluates to:

Answer

Correct Answer: -2

Note: This Question is unanswered, help us to find answer for this one

19. The statement z=`expr 5 / 2` would store which of the following values in z?

Answer

Correct Answer: 2

Note: This Question is unanswered, help us to find answer for this one

20.

What is the error in the following loop statement?

while [ $1 -gt 10 -a \($2 -o -w $3 \) ]

Answer

Correct Answer:

There is no error 


Note: This Question is unanswered, help us to find answer for this one

21. Which of the following is not a shell keyword?

Answer

Correct Answer: ls

Note: This Question is unanswered, help us to find answer for this one

22.

What is the output of the following program?

 terminal=vt100

case $terminal in

    vt100) echo Dec terminal;;

    vt200) echo Old terminal;;

    ansi) echo Commonly used terminal;;

    v*) echo vt series terminal;;

    *) echo Any terminal;;

esac

Answer

Correct Answer:

Dec terminal


Note: This Question is unanswered, help us to find answer for this one

23.

What is the output of the following program?

        i=4 z=12 

        [ $i = 5 -a $z -gt 5 ]

Answer

Correct Answer:



Note: This Question is unanswered, help us to find answer for this one

24.

What is the error in the following shell script code?

#!/bin/sh

1.    a=12.25 b=12.52

2.    if [a=b]

3.    then

4.    echo "\na and b are equal"

5.    fi

Answer

Correct Answer:

On line 2, [a=b] should be replaced with [ $a -eq $b ]   


Note: This Question is unanswered, help us to find answer for this one

25. The exit statement is used in a loop to:

Answer

Correct Answer: terminate the execution of a script.

Note: This Question is unanswered, help us to find answer for this one

26.

What is the output of the following program?

i=1 j=1 k=1 while [ $i -lt 10 ] do while [ $j -lt 10 ] do while [ $k -lt 10 ] do echo $i $j $k k='expr $k + 1' break 3 done j='expr $j + 1' done i='expr $i + 1'

Answer

Correct Answer: 1 1 1

Note: This Question is unanswered, help us to find answer for this one

27. Precedence hierarchy decides which operator:

Answer

Correct Answer: is used first.

Note: This Question is unanswered, help us to find answer for this one

28.

What is the error in the following program?

 1.    j=10 k=12

2.    if [ $k>=$j ]

3.    then

4.    k=$j

5.    j=$k

6.    fi

7.    echo $j $k

Answer

Correct Answer:

There is no error


Note: This Question is unanswered, help us to find answer for this one

29.

The break statement is used to exit from:

Answer

Correct Answer:

a

Note: This Question is unanswered, help us to find answer for this one

30.

What is the output of the following program?

i=1

for [ i -le 10 ]

do

echo $i

i=

Answer

Correct Answer:

An error will occur


Note: This Question is unanswered, help us to find answer for this one

31. Which of the following assignments is illegal?

Answer

Correct Answer: d = 25

Note: This Question is unanswered, help us to find answer for this one

32.

What is the error in the following program?

1.    while who | grep aa12 | wc -l

2.    do

3.        echo false

4.    done

Answer

Correct Answer:

There is no error


Note: This Question is unanswered, help us to find answer for this one

33.

Which of the following lines have an error?

 1. echo enter your name

2. read filename

3. read name < $filename

4. echo $name

Answer

Correct Answer:



Note: This Question is unanswered, help us to find answer for this one

34. Which of the following is true about the "continue" statement?

Answer

Correct Answer: It continues the control to the beginning of the loop, bypassing the statements below it, inside the loop.

Note: This Question is unanswered, help us to find answer for this one

35. What will be the output of the following program assuming that the command line arguments are dog parrot cuckoo? for argument in $* do echo $argument done

Answer

Correct Answer: dog parrot cuckoo

Note: This Question is unanswered, help us to find answer for this one

36. Which of the following expressions is correct?

Answer

Correct Answer: a=`expr $b \* \( $c + $d \ )

Note: This Question is unanswered, help us to find answer for this one

37.

If x=11 and y=6, then what is the exit status of the following expression?

[ $x -eq 11 -a $y -ne 89 ]

Answer

Correct Answer:



Note: This Question is unanswered, help us to find answer for this one

38. Which of the following statements is incorrect regarding if-then-fi?

Answer

Correct Answer: The indentation is mandatory otherwise an error will occur on execution.

Note: This Question is unanswered, help us to find answer for this one

39. What would be the condition to check whether file with name "xyz" is a regular file?

Answer

Correct Answer: if [ -f xyz ]

Note: This Question is unanswered, help us to find answer for this one

40.

What is the output of the following shell script code?

suite=3

case $suite in

    1) echo Diamond ;;

    2) echo Spade ;;

    3) echo Heart ;;

    4) echo Club ;;

Answer

Correct Answer:

Heart 


Note: This Question is unanswered, help us to find answer for this one

41. Which of the following statements is false?

Answer

Correct Answer: Output of echo statement can be redirected to a file.

Note: This Question is unanswered, help us to find answer for this one

42.

What is the output of the following program?

z='[$x -lt 10 ]'

echo x=$x y=$y z=$z

x=3

 y=

Answer

Correct Answer:

None of the above 


Note: This Question is unanswered, help us to find answer for this one

43.

If x=11 and y=6, then what is the exit status of the following expression?

        [ ! $x -gt 9 -a ! $y -ne 23 ]

Answer

Correct Answer:



Note: This Question is unanswered, help us to find answer for this one

44. Which of the following is true about "until" loop?

Answer

Correct Answer: It cannot test for conditions.

Note: This Question is unanswered, help us to find answer for this one

45.

What is the error in the following program?

1.    x=10

 2.    if [ x -ge 2 ]

3.    then

4.        echo $x

5.    fi

Answer

Correct Answer:

Line 2 should be if [ $x -ge 2 ]


Note: This Question is unanswered, help us to find answer for this one

46.

What is the output of the following program?

        a=300 

        [ -n $a ]

        echo $?

        [ -z $a ]

        echo $?

Answer

Correct Answer:

0 1


Note: This Question is unanswered, help us to find answer for this one

47.

What is the output of the following program?

b= [ -n $b ]

echo $?

[ -z $b ]

echo $?

Answer

Correct Answer:

An error will occur


Note: This Question is unanswered, help us to find answer for this one

48.

 What is the error in the following shell script code?

1.    until=1

2.    while [ $until -eq 5 ]

3.    do

4.    echo until cannot be used as a variable name

5.    until=`expr $until + 1`

6.    done

Answer

Correct Answer:

Line 1 should be until=$1


Note: This Question is unanswered, help us to find answer for this one

49. A shell variable cannot start with:

Answer

Correct Answer: A number and a special symbol other than the underscore

Note: This Question is unanswered, help us to find answer for this one

50. Which of the following is/are allowed in an arithmetic statement involving expr command?

Answer

Correct Answer: ( )
%
3-5

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one