Unix Questions and Answers

Trend Nxt Unix | Mock Test

1) The User interacts with the Unix system or applications using
_____
a) Hardware
b) Shell
c) Device drivers
d) Compiler
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: b) Shell [/bg_collapse]
2) Select the features of an Unix Shell
a) Unix Shell is a command interpreter
b) Unix shell scripts can help in automation
c) Like any programming languages, Unix shell supports variables,
conditional statements, functions, array etc.
d) All of the above
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: d) All of the above [/bg_collapse]
3) Will the following script produce errors?
#!/bin/ksh
until=1
while [$until=1]
do
echo “skfsi”
done
a) Yes
b) No
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: a) Yes [/bg_collapse]
They have error coz script shld be while [ $until=1 ] i.e with spaces near the bracket
4) Which of the following is valid syntax for UNIX commands?
a) Command name followed by arguments
b) Command name followed by options
c) Command name, followed by options, followed by arguments
d) all of the above
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: d) all of the above [/bg_collapse]
5) Which of the following command is used to select first 5 lines
from a file?
a) head o5 filename
b) head +5 filename
c) head 5 filename
d) head -5 filename
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: d) head -5 filename [/bg_collapse]
6) What will be the output of the following shell scripts snippet?
(Here – represents assignment operator)
#!/bin/bash
n-“Test”
echo $n “$n”1 $n1
a) Test n1 1
b) Test $n1 n1
c) Test Test1
d) Test Test1 Test1
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: c) Test Test1 [/bg_collapse]
7) Which of the commands provides information on UNIX documentation?
a) man
b) whatis
c) apropos
d) All of the above
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: d) man [/bg_collapse]
8) The User executes the following commands:
$ export VAR1=Wipro
$ echo $VAR1
Wipro
$ /bin/bash
$ echo $VAR1
xxxxx
$ export VAR1=India
$ exit
$ echo $VAR1
yyyyy
In the above, what would be printed in place of xxxxx and yyyyy?
a) NULL, Wipro
b) Wipro, India
c) Wipro, Wipro
d) Wipro, NULL
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: Wipro, Wipro [/bg_collapse]
9) Which of the following shell variables is used to set the secondary prompt?
Note: Seondary prompt is also known as Continuation Interactive prompt
a) $PROMPT
b) $PS2
c) $INS
d) $HOME
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: b) $PS2 [/bg_collapse]
10) What will be the output of the following shell script snippet?
(Here = represents assignemnt Operator)
n=”Test”
echo $n “$n”1 ‘$n’1
a) Test Test1 $n1
b) Test $n1 n1
c) Test n1 1
d) Test Test1 Test1
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: a) Test Test1 $n1 [/bg_collapse]
11) Which of the following is true about environment variables?
a) Environment variables can be set and must be exported to be available globally
b) They can be listed using the printerv command
c) An environment variable defined in child shell will not be available in the parent shell
d) All of the above
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: All of the above [/bg_collapse]
12) The exit status of the typical Unix command will usually have a value of ?
a) 0 if successful and non-zero on failure
b) Non zero if successful and 0 on failure
c) 1 if successful and -1 on failure
d) -1 if successful and 1 on failure
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: a) 0 if successful and Non-zero on failure [/bg_collapse]
13) Consider the following shell program
if [ $# -eq 0 ]
then
echo “Usage: give multiple integers”
exit 1
fi
output=0
Until [ $# -eq 0 ]
do
output=’expr $output + $1’
shift
done
echo $output
What will be the output of the above program when inputs are given as:
program 16 23 44 70 0 50
a) 16
b) 50
c) 203
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: c) 203 (Addition of all the arguments) [/bg_collapse]
14) What is the command used to allow the members of your working group to write to Wipro.file without impacting the permissions of non-group users?
a) chmod +w Wipro.file
b) chmod u+x Wipro.file
c) chmod g|w Wipro.file
d) chmod g+w Wipro.file
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: d) chmod g+w Wipro.file [/bg_collapse]
15) The Current directory is /home/user1/mydir.
if the following commands are executed on the shell, what will be the output?
pwd=`pwd`
cd ..
echo `pwd` $pwd
a) /home/user1 /home/user1/mydir
b) /home/user1/mydir /home/user1/mydir
c) pwd /home/user1
d) /home/user1/mydir /home/user1
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: a) /home/user1 /home/user1/mydir [/bg_collapse]
16) What is the output of command “cd/” in Unix
a) It will change to user directory
b) It will change to root directory
c) It will change to device directory
d) It will change to etc directory
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: b) It will change to root directory [/bg_collapse]
17) What is the output of following command basename /usr/bin/wipro/wipro.txt
a) /usr/bin/wipro
b) /usr/
c) wipro.txt
d) None of the above
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: c) wipro.txt [/bg_collapse]
18) The exit status of the last command executed is referred by the shell variable
a) $!
b) $”
c) $&
d) $?
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: d) $? [/bg_collapse]
19) Command used to list all the shell variables
a) act
b) disp
c) print
d) none of the above
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: a) act [/bg_collapse]
20) What kill$! will do?
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ]Answer: kill the last processed nbr [/bg_collapse]
21) chown command is used for?
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ]Answer: Change the owner [/bg_collapse]
22) What command is used to read the content of the file?
a) cat
b) fetch
c) both
d) display
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ]Answer: a) cat [/bg_collapse]
23)How the path will be specified in environment file?
a) look
b) manpath
c) Path
d) dir
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: Path[/bg_collapse]
1) Asuming the command line arguments to be first second third, what would be the output of the following script
#!/bin/ksh
while [ $# -gt 0 ]
do
last=”$@”
shift
done
echo $last
a) &nbsp,first second third
b) first second
c) first
d) third
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: d) third [/bg_collapse]
2) what will be the output of the following script snippet?
(( 6>9 ))
echo “$?”
a) 0
b) 1
c) 127
d) Error
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: b) 1 [/bg_collapse]
3) All Unix command invoked by user are interpreted by
a) Hardware
b) Shell
c) Device Drivers
d) Compiler
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: b) Shell [/bg_collapse]
4) $# is a built-in shell variable that holds
a) exit status of the last command executed
b) number of command line args
c) name of the shell script
d) None of the above
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: b) number of command line args [/bg_collapse]
5) What value does $# have?. if the shell script called “testscript” is executed as
$ ./testscript arg1 arg2
a) testscript  arg1  arg2
2) arg1 arg2
c) 2
d) 3
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: c) 2 [/bg_collapse]
6) What will be the statement to print only the 7th line from a file using head and tail commands
a) head -7 |tail -7 filename
b) tail -7 filename
c) head -7 filename|tail -1
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: head -7 filename|tail -1 [/bg_collapse]
1) cat file1 file2 – displays content of both file1 and file2
2) df command – Disk Free
3) du command – shows the disk usage of the files and directories
4) KILL$!: answer covered above
5) $!
6) $# – aruguments one two three “four and five”. How many no of arguments . [bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: 4 [/bg_collapse]
7) foo(((5+3*2) – 4)+1). What is $#?
8) Case Syntax
9) x=c;
y=c;
echo $$x $$$y
What is printed if the current PID is 1234
a) 1234x 1234y
b) xc
c) 1234x 1234c
d) $x $$y
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: 1234x 1234c [/bg_collapse]
10) Which command is used to know if the process is running
a) ps
b) ls
c) awk
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: ps [/bg_collapse]
12) exp var= ( This is Unix Unix assessment )
$ echo ${myvar[3]}
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: Unix [/bg_collapse]
13) echo `set r w x`; IFS=”-“; echo “$*”
a)rwx
b)r w x
c)r-w-x
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: r-w-x [/bg_collapse]
14) What will be the ouptput of the below program,
echo “Process Killed”
kill $$
echo “Process not killed”
a) First echo will be printed
b) second echo statement will be printed
c) both the echo statement will be printed
d) none of the above
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: Both the echo statement will be printed [/bg_collapse]
15) How to perform addition of numbers. parameters passed as 3 and 4
a) a = $3 + $4
b) a = 3 + 4
c) a = `expr $1 + $2`
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: c) a = `expr $1 + $2` [/bg_collapse]
16) Terminal command
a) $PATH
b) $TERM
c) $TERMINAL
d) $CMD
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: $TERM [/bg_collapse]
17) Test command is used to compare
a) Strings
b) Numerics
c) State of file
d) All
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: d) All [/bg_collapse]
18) unset is used as
a) unset a=10
b) unset a
c) unset var a
d) unset $a
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: d) unset $a
unset is used to delete the variables during program execution[/bg_collapse]
6) What will :$((cnt=cnt+1)) do?
7) If ls > /tmp/xx.$$ , then $$ takes
a) pid
b) No. of arguement
c) Login
d) Name
[bg_collapse view=”button-green” color=”#FFF” icon=”arrow” expand_text=”Show Answer” collapse_text=”Hide Answer” ] Answer: pid [/bg_collapse]
————————————–
Variable Description
$0 The filename of the current script.
$n These variables correspond to the arguments with which a script was invoked. Here n is a positive decimal number corresponding to the position of an argument (the first argument is $1, the second argument is $2, and so on).
$# The number of arguments supplied to a script.
$* All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2.
$@ All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2.
$? The exit status of the last command executed.
$$ The process number of the current shell. For shell scripts, this is the process ID under which they are executing.
$! The process number of the last background command.
———————————–
List of Commands
cat – display or concatenate files
cd – change directory
chmod – change the permissions on a file or directory
cp – copy a file
date – display the current date and time
diff – display differences between text files
file – determine the type of a file
find – find files of a specified name or type
ftp – file transfer program
grep – searches files for a specified string or expression
gzip – compress a file
help – display information about bash builtin commands
info – read online documentation
kill – kill a process
lpr – print out a file
ls – list names of files in a directory
man – display an on-line manual page
mkdir – make a directory
more – scan through a text file page by page
mv – move or rename files or directories
nice – change the priority at which a job is being run
passwd – change your password
ps – list processes
pwd – display the name of your current directory
quota – disk quota and usage
rm – remove files or directories
rmdir – remove a directory
sort – sort and collate lines
ssh – secure remote access
tar – create and use archives of files
apropos – Locate commands by keyword lookup
du – Disk Usage
df – Disk Free
——————————-
Environment Variables:
Variable Description
DISPLAY – Contains the identifier for the display that X11 programs should use by default.
HOME – Indicates the home directory of the current user: the default argument for the cd built-in command.
IFS – Indicates the Internal Field Separator that is used by the parser for word splitting after expansion.
LANG – LANG expands to the default system locale; LC_ALL can be used to override this. For example, if its value is pt_BR, then the language is set to (Brazilian) Portuguese and the locale to Brazil.
LD_LIBRARY_PATH – On many Unix systems with a dynamic linker, contains a colon-separated list of directories that the dynamic linker should search for shared objects when building a process image after exec, before searching in any other directories.
PATH – Indicates search path for commands. It is a colon-separated list of directories in which the shell looks for commands.
PWD – Indicates the current working directory as set by the cd command.
RANDOM – Generates a random integer between 0 and 32,767 each time it is referenced.
SHLVL – Increments by one each time an instance of bash is started. This variable is useful for determining whether the built-in exit command ends the current session.
TERM – Refers to the display type
TZ – Refers to Time zone. It can take values like GMT, AST, etc.
—————————–