250+ TOP MCQs on Shell Environment and Answers

Linux / Unix questions and answers focuses on Bash Shell environment. It will be useful for anyone learning Linux Basics, Essentials and Shell programming.

1. To feed standard output of one command to standard input of another in a single shell session
a) IO redirection can be used
b) Named pipes can be used
c) The pipe operator provided by the shell can be used
d) It can not be done
Answer: c
Clarification: None.

2. Which of the following commands allows definition and assignment of environment variables under bash
a) env
b) export
c) environ
d) setenviron
Answer: a
Clarification: None.

3. While executing a command, the shell
a) Executes it in the same process (as shell)
b) Creates a child shell to execute it
c) Loads a special program to take care of the execution
d) None of the mentioned
Answer: b
Clarification: None.

4. Which variable contains current shell process id
a) $*
b) $?
c) $$
d) $!
Answer: c
Clarification: None.

5. Which command is used to debug a shell script program
a) set
b) set -x
c) debug
d) db
Answer: b
Clarification: None.

6. For every successful login, which script will be executed?
a) /etc/inittab
b) /etc/profile
c) /etc/login
d) /etc/init
Answer: b
Clarification: None.

7. Hidden files are
a) Those whose ‘read’ bit is set to ‘h’
b) Permitted for (can be accessed) only superusers
c) Files that begin with a ‘.’
d) Files that cannot be opened by ordinary user for writing
Answer: c
Clarification: None.

8. Shell is ?
a) Command Interpreter
b) Interface between Kernel and Hardware
c) Interface between user and applications
d) Command Compiler
Answer: a
Clarification: None.

250+ TOP MCQs on Functions and Answers

Linux / Unix questions and answers focuses on Functions in Awk Programming.

1. Which one of the following statement is not true?
a) awk’s built-in functions can be categorised into three categories: numeric, string and I/O
b) built-in functions are always available to call
c) extra arguments to built-in function causes syntax error
d) we can also define the function in awk program
Answer: c
Clarification: Extra arguments to built-in function causes fatal error.

2. What is the difference between the built-in functions rand() and srand() in awk programming?
a) rand() generates the random number but srand() does not generate the random number
b) rand() generates the same random number always whenever the program runs but srand() generates the different
c) srand() requires the seed() function but rand() does not require to produce the same result
d) none of the mentioned
Answer: b
Clarification: None.

3. Which built-in function returns the arctangent of a/b in radians.
a) atan2(a/b)
b) atan(a/b)
c) atan_2(a/b)
d) none of the mentioned
Answer: a
Clarification: None.

4. Which built-in function divides string into pieces seperated by fieldsep and stores the pieces in array?
a) split()
b) divide()
c) index()
d) sub()
Answer: a
Clarification: None.

5. The built-in function tolower()
a) converts all lowercase characters into uppercase characters
b) returns the string with all lowercase characters
c) changes the nonalphabetic characters
d) none of the mentioned
Answer: b
Clarification: None.

6. What is the output of this program?

  1.    #! /usr/bin/awk -f
  2.    BEGIN {
  3.        print log(1)
  4.    }

a) 0
b) syntax error
c) fatal error
d) segmentation fault
Answer: a
Clarification: The function log() is built-in function and need not to be defined. This function calculates the natural logarithm of positive numbers.
Output:
[email protected]:/home/# ./test.awk
0
[email protected]:/home/#

7. What is the output of this program?

  1.    #! /usr/bin/awk -f
  2.    BEGIN {
  3.        print index("","linux")
  4.    }

a) linux
b)
c) 0
d) none of the mentioned
Answer: c
Clarification: The function index() searches the string “” for the first occurence of the string “linux”. The function usually returns the character position but here the string is not found, hence function returns 0.
Output:
[email protected]:/home/# ./test.awk
0
[email protected]:/home/#

8. What is the output of this pogram?

  1.    #! /usr/bin/awk -f
  2.    BEGIN {
  3.        system("date")
  4. 	   print ""
  5.    }

a) the program will execute the date command and then program will print “”
b) program will generate fatal error because function system() is neither defined nor built-in function
c) program will generate syntax error because the syntax of function system() is wrong
d) none of the mentioned
Answer: a
Clarification: The function system() allow to execute operating system command and then return to the awk program.
Output:
[email protected]:/home/# ./test.awk
Wed Apr 17 10:22:47 IST 2013

[email protected]:/home/#

9. What is the output of the program?

  1.    #! /usr/bin/awk -f
  2.    BEGIN {
  3.        a=int(2.5)
  4.        print (a*20)
  5.    }

a) 50
b) 40
c) a*20
d) syntax error
Answer: b
Clarification: The function int() returns the integer part of the value. In this program it returns 2.
Output:
[email protected]:/home/# ./test.awk
40
[email protected]:/home/#

10. What is the output of the program?

  1.     #! /usr/bin/awk -f
  2.     BEGIN {
  3.        print toupper("_1_$")
  4.     }

a) _1_$
b) 1 $
c) _1_$
d)
Answer: a
Clarification: None.
Output:
[email protected]:/home/# ./test.awk
_1_$
[email protected]:/home/#

contest

250+ TOP MCQs on Static Libraries and Answers

Linux / Unix questions and answers focuses on static libraries.1. In Linux, the static library has the extension of
a) .a
b) .b
c) .c
d) .d

Answer: a
Clarification: None.

2. The library contains the
a) pre-compiled object files
b) source files
c) header files
d) none of the mentioned

Answer: a
Clarification: None.

3. Libraries can be linked with ____ to create executables.
a) other libraries
b) other object files
c) both other libraries and object files
d) none of the mentioned

Answer: c
Clarification: None.

4. In Linux, the static libraries can be created by
a) ar command
b) as command
c) ap command
d) aq command

Answer: a
Clarification: None.

5. If a program is linked against a static library then
a) machine code of the used function is copied in the executable
b) used function definition is copied into the preprocessd code
c) used function definition is provided at the time of compiling
d) none of the mentioned

Answer: a
Clarification: None.

6. To use the static library in the program
a) header file must be provided to in the source code
b) path for the library must be specified
c) header file must be provided to in the source code & path for the library must be specified
d) none of the mentioned

Answer: c
Clarification: None.

7. Which gcc option is used to specify the library?
a) -c
b) -l
c) -a
d) -o

Answer: b
Clarification: None.

8. In linux, the static libraries are mostly installed in
a) /usr/lib
b) /usr/local/lib
c) both /usr/lib and /usr/local/lib
d) none of the mentioned

Answer: c
Clarification: None.

9. Which one of the following command can list the symbols defined in a library?
a) mn
b) nm
c) nn
d) mm

Answer: b
Clarification: None.

10. The archive(ar) utility in linux can
a) create a new static library
b) insert the object files into the static library
c) replace the object files into the static library
d) all of the mentioned

Answer: d

250+ TOP MCQs on File Management 2 and Answers

Here is a listing of Linux / Unix Technical Interview Questions & Answers for experienced IT professionals as well as fresh engineering graduates. These questions can be attempted by anyone focusing on Linux Development and Systems programming.

1. Given a code snippet below?

    #define PERMS  (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
    int main() 
    {
        int fd1, fd2;
        umask(0);
        fd1 = open(“file1”, O_CREAT | O_RDWR, PERMS)
        umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
        fd2 = open(“file2”, O_CREAT | O_RDWR, PERMS)
        return 0;
    }

The newly created files file1 and file2 will have the permissions respectively
a) rw-rw-rw- r——–
b) r——– rw-rw-rw-
c) rw-rw-rw- rw——-
d) None of the mentioned
Answer: c
Clarification: None.

2. Below is the code

    int main() 
   {
        int fd1, fd2;
        struct stat buff1, buff2;
        fd1 = open(“1.txt”, O_RDWR);
        fd2 = open(“2.txt”, O_RDWR | O_APPEND);
        lseek(fd1, 10000, SEEK_SET);
        write(fd1, “abcdefghij”, 10);
        write(fd2, “abcdefghij”, 10);
        fstat(fd1, &buff1);
        fstat(fd2, &buff2);
        printf(“ %d %d”, buff1.st_size, buff2.st_size);
        return 0;
    }

Before running the program, the file 1.txt and 2.txt size is 20 each. What is the output?
a) 30 30
b) 100020 20
c) 100030 30
d) 100010 30
Answer: d
Clarification: None.

3. What is stored in logfile as per below mentioned code if we execute ./a.out > logfile?

    int main() 
    {
        int fd;
        close(1);
        fd = open(“logfile”,O_RDWR, 0744);
        write(fd, “Hello”, 5);
        printf(“Worldn”);
        return 0;
    }

a) Hello
b) HelloWorld
c) World
d) None
Answer: b
Clarification: None.

4. For the below mentioned code,

   int main() 
   {
        int fd;
        fd = open(“logfile”, O_CREAT|O_RDWR, 0600);
        lseek(fd, 5, SEEK_CUR);
        write(fd, “Hello”, 5);
        return 0;
    }

What is the logfile size now if it’s initially was 1024 bytes?
a) 5
b) 1024
c) 1029
d) 1034
Answer: b
Clarification: None.

5. Code snippets

    str1=”45678n”
    str2=”123n”
    f1 = fopen(file1,RDWR,RWX)
    f2 = fopen(file1,RDWR,RWX)
    write(f1,str1,len_str1)
    write(f2,str2,len_str2)
 
    o/p:

a) 12378
b) 123(newline)8(newline)
c) 123(newline)78(newline)
d) 45678(newline)123(newline)
Answer: b
Clarification: None.

6. Code snippets

    str1=”45678n”
    str2=”123n”
    f1 = fopen(file1,RDWR,RWX)
    f2 = dup(f1)
    write(f1,str1,len_str1)
    write(f2,str2,len_str2)
 
    o/p:

a) 12378
b) 123(newline)8(newline)
c) 123(newline)78(newline)
d) 45678(newline)123(newline)
Answer: d
Clarification: None.

7. Code snippet (file1 size is 2024)

    f1 = fopen (file1, RDWR, RWX)
    lseek(f1,1024,SEEK_SET)
    write(f1,buf,10) 
    What is offset now.

a) 1024
b) 1034
c) 2034
d) 2054
Answer: b
Clarification: None.

250+ TOP MCQs on Unix Domain Sockets and Answers

Linux Debugging questions and answers focuses on Unix Domain Sockets.1. What is the output of this program?

  1.    #include
  2. 
    
  3.    int main()
  4.    {
  5.        int fd_socket;
  6.        fd_socket = socket(AF_UNIX,SOCK_STREAM,0);
  7.        printf("%dn",fd_socket);
  8.        return 0;
  9.    }

a) -1
b) 0
c) any integer value
d) none of the mentioned

Answer: d

2. In this program, the third argument of the socket() is used for _____ potocol.

  1.    #include
  2.    #include<sys/types.h>
  3.    #include<sys/socket.h>
  4.    int main()
  5.    {
  6.        int fd_socket;
  7.        if(socket(AF_UNIX,SOCK_STREAM,0) == -1)
  8.            perror("socket");
  9.        return 0;
  10.    }

a) TCP/IP
b) UDP
c) both TCP/IP and UDP
d) none of mentioned

Answer: a
Clarification: None.

3. By this program the soket “san_sock” will create

  1.    #include
  2.    #include<sys/types.h>
  3.    #include<sys/un.h>
  4.    #include<sys/socket.h>
  5. 
    
  6.    int main()
  7.    {
  8.        struct sockaddr_un add_server;
  9.        int fd_server;
  10.        fd_server = socket(AF_UNIX,SOCK_STREAM,0);
  11.        if(fd_server == -1)
  12.            perror("socket");
  13.        add_server.sun_family = AF_UNIX;
  14.        strcpy(add_server.sun_path,"san_sock");
  15.        if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0)
  16.            perror("bind");
  17.        return 0;
  18.    }

a) in the /tmp directory
b) in the /usr directory
c) in the present working directory
d) none of the mentioned

Answer: c

4. What is the length of of the queue for pending connections in this program?

  1.    #include
  2.    #include<sys/types.h>
  3.    #include<sys/un.h>
  4.    #include<sys/socket.h>
  5. 
    
  6.    int main()
  7.    {
  8.        struct sockaddr_un add_server;
  9.        int fd_server;
  10.        fd_server = socket(AF_UNIX,SOCK_STREAM,0);
  11.        if(fd_server == -1)
  12.            perror("socket");
  13.        add_server.sun_family = AF_UNIX;
  14.        strcpy(add_server.sun_path,"server_sock2");
  15.        if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0)
  16.            perror("bind");
  17.        if( listen(fd_server,3) != 0)
  18.            perror("listen");
  19.        return 0;
  20.    }

a) 0
b) 1
c) 2
d) 3

Answer: d
Clarification: The second argument of listen() specifies the length for the queue for pending connections.

5. What is the output of the program?

  1.    #include
  2.    #include<sys/types.h>
  3.    #include<sys/un.h>
  4.    #include<sys/socket.h>
  5. 
    
  6.    int main()
  7.    {
  8.        struct sockaddr_un add_server, add_client;
  9.        int fd_server, fd_client;
  10.        int len;
  11.        char ch;
  12.        fd_server = socket(AF_UNIX,SOCK_STREAM,0);
  13.        if(fd_server == -1)
  14.            perror("socket");
  15.        add_server.sun_family = AF_UNIX;
  16.        strcpy(add_server.sun_path,"san_sock");
  17.        if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0)
  18.            perror("bind");
  19.        if( listen(fd_server,3) != 0)
  20.            perror("listen");
  21.        len = sizeof(add_client);
  22.        fd_client = accept(fd_server,(struct sockaddr*)&add_client,&len);
  23.        printf("n");
  24.        return 0;
  25.    }

a) the program will print the string “”
b) the process will remain block
c) segmentation fault
d) none of the mentioned

Answer: b

6. What is the output of this program?

  1.    #include
  2.    #include<sys/types.h>
  3.    #include<sys/socket.h>
  4. 
    
  5.    int main()
  6.    {
  7.        int fd;
  8.        fd = socket(AF_UNIX,SOCK_STREAM,0);
  9.        printf("%dn",fd);
  10.        return 0;
  11.    }

a) 0
b) 1
c) 2
d) 3

Answer: d

7. What is the output of this program?

  1.    #include
  2.    #include<sys/types.h>
  3.    #include<sys/un.h>
  4.    #include<sys/socket.h>
  5.    #include
  6. 
    
  7.    int main()
  8.    {
  9.        struct sockaddr_un addr;
  10.        int fd;
  11.        fd = socket(AF_UNIX,SOCK_STREAM,0);
  12.        if (fd == -1)
  13.            perror("socket");
  14.        addr.sun_family = AF_UNIX;
  15.        strcpy(addr.sun_path,"san_sock");
  16.        if (bind(4,(struct sockaddr*)&addr,sizeof(addr)) == -1)
  17.            printf("Sanfoudnryn");
  18.        return 0;
  19.    }

a) this program will print the string “”
b) this program will not print the string “”
c) segmentation fault
d) none of the mentioned

Answer: a

8. What this program is not able to connect with any client program?

  1.    #include
  2.    #include<sys/types.h>
  3.    #include<sys/un.h>
  4.    #include<sys/socket.h>
  5. 
    
  6.    int main()
  7.    {
  8.        struct sockaddr_un add_server, add_client;
  9.        int fd_server, fd_client;
  10.        int len;
  11.        char ch;
  12.        fd_server = socket(AF_UNIX,SOCK_STREAM,0);
  13.        if(fd_server == -1)
  14.            perror("socket");
  15.        add_server.sun_family = AF_UNIX;
  16.        strcpy(add_server.sun_path,"san_sock");
  17.        if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0)
  18.            perror("bind");
  19.        len = sizeof(add_client);
  20.        fd_client = accept(fd_server,(struct sockaddr*)&add_client,&len);
  21.        printf("n");
  22.        return 0;
  23.    }

a) the listen() is missing
b) the connect() is missing
c) the read() and write() are missing
d) none of the mentioned

Answer: a
Clarification: None.

9. What is the output of this program?

  1.    #include
  2.    #include<sys/types.h>
  3.    #include<sys/un.h>
  4.    #include<sys/socket.h>
  5. 
    
  6.    int main()
  7.    {
  8.        struct sockaddr_un add_server, add_client;
  9.        int fd_server, fd_client;
  10.        int len;
  11.        char ch;
  12.        fd_server = socket(AF_UNIX,SOCK_STREAM,0);
  13.        if(fd_server == -1)
  14.            perror("socket");
  15.        add_server.sun_family = AF_UNIX;
  16.        strcpy(add_server.sun_path,"san_sock");
  17.        if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0)
  18.            perror("bind");
  19.        len = sizeof(add_client);
  20.        fd_client = connect(fd_server,(struct sockaddr*)&add_client,&len);
  21.        printf("n");
  22.        return 0;
  23.    }

a) this program will print the string “”
b) segmentation fault
c) error
d) none of the mentioned

Answer: c

10. What is the output of this program?

  1.     #include
  2.     #include<sys/types.h>
  3.     #include<netinet/in.h>
  4.     #include<sys/socket.h>
  5.     #include
  6. 
    
  7.     int main()
  8.     {
  9.         struct sockaddr_in addr;
  10.         int fd;
  11.         fd = socket(AF_UNIX,SOCK_STREAM,0);
  12.         if (fd == -1)
  13.             perror("socket");
  14.         addr.sun_family = AF_UNIX;
  15.         strcpy(addr.sun_path,"san_sock");
  16.         if (bind(4,(struct sockaddr*)&addr,sizeof(addr)) == -1)
  17.             printf("Sanfoudnryn");
  18.         return 0;
  19.     }

a) error
b) “”
c) segmentation fault
d) none of the mentioned

Answer: a

250+ TOP MCQs on Linux Environment and Answers

Linux / Unix interview questions and answers focuses on various Unix administration commands. It will be useful for anyone learning basic Unix Administration as well as preparing for interviews on Unix.

1. SVR4 was developed by
a) Sun Microsystems
b) AT&T
c) University of Berkeley
d) Sun and AT&T jointly
Answer: d
Clarification: None.

2. Which of these is not a Unix Flavor?
a) BSD
b) MAC
c) AIX
d) IRIX
Answer: b
Clarification: None.

3. Which of the following statement is FALSE ?
a) Unix supports multiple users
b) Linux is an open source operating system and the source code is shared
c) Shell takes care of inter process communication
d) Shell provides the feature of I/O Redirection
Answer: c
Clarification: None.

4. Which of the following UNIX flavor is from IBM?
a) BSD
b) Solaris
c) HP-UX
d) AIX
Answer: d
Clarification: None.

5. x86-32 uses which programming model?
a) IP16
b) IP32
c) ILP16
d) ILP32
Answer: d
Clarification: None.

6. What are the sizes of (Integer/Long/Pointer) in LP64 programming model?
a) 8/8/8
b) 4/4/8
c) 4/8/8
d) 4/8/4
Answer: c
Clarification: None.

7. Which among the following is used to write small programs to control Unix functionalities?
a) Shell Commands
b) Shell Script
c) Filters
d) C Language
Answer: b
Clarification: None.

8. What control character signals the end of the input file?
a) ctrl + a
b) ctrl + b
c) ctrl + c
d) ctrl + d
Answer: d
Clarification: None.

9. How do you get help about the command “cp”?
a) help cp
b) man cp
c) cd ?
d) none of the mentioned
Answer: b
Clarification: None.

Here’s the list of Best Reference Books in Linux Commands & Shell Programming.