250+ TOP MCQs on Shell Programming and Answers

Linux / Unix questions and answers focuses on Bash Shell programming. It will be useful for both fresher & experienced who are learning Linux Basics, Essentials and Shell programming.

1. What will be output of following command:

$ echo "The process id is" $$$$
 
a) The process id is $$
b) The process id is $<pid>$<pid>
c) The process id is <pid><pid>
d) The process id is $$$$

View Answer

Answer: c
Clarification: None.

 
 

2. What would be the current working directory at the end of the following command sequence?

 $ pwd
 /home/user1/proj
 $ cd  src
 $ cd  generic
 $ cd  .
 $ pwd

a) /home/user1/proj
b) /home/user1/proj/src
c) /home/user1
d) /home/user1/proj/src/generic
Answer: d
Clarification: None.

3. How do you print the lines between 5 and 10, both inclusive
a) cat filename | head | tail -6
b) cat filename | head | tail -5
c) cat filename | tail +5 | head
d) cat filename | tail -5 | head -10
Answer: a
Clarification: None.

4. Create a new file “new.txt” that is a concatenation of “file1.txt” and “file2.txt”
a) cp file.txt file2.txt new.txt
b) cat file1.txt file2.txt > new.txt
c) mv file[12].txt new.txt
d) ls file1.txt file2.txt | new.txt
Answer: b
Clarification: None.

5. which of these is NOT a valid variable in bash
a) __ (double underscore)
b) _1var (underscore 1 var )
c) _var_ (underscore var underscore)
d) some-var (some hyphen var)
Answer: d
Clarification: None.

6. What is the output of the following code:

os=Unix
echo 1.$os 2."$os" 3.'$os' 4.$os

a) 1.Unix 2.Unix 3.Unix 4.Unix
b) 1.Unix 2.Unix 3.$os 4.Unix
c) 1.Unix 2.Unix 3.Unix 4.$os
d) 1.Unix 2.$os 3.$os 4.$os
Answer: b
Clarification: None.

7. What is the return value ($?) of this code:

os = Unix
[$osName = UnixName] && exit 2
[${os}Name = UnixName] && exit 3

a) 0
b) 1
c) 2
d) 3
Answer: d
Clarification: None.

8. What is the output of the following program?

x = 3; y = 5; z = 10;
if [( $x -eq 3 ) -a ( $y -eq 5 -o  $z -eq 10 )]
then
    echo $x
else
    echo $y
fi

a) 1
b) 3
c) 5
d) Error
Answer: b
Clarification: None.

9. What is the output of the following program?

[ -n $HOME ]
echo $?
[ -z $HOME ]
echo $?
a) 0
   1
b) 1
   0
c) 0
   0
d) 1
   1

View Answer

Answer: a
Clarification: None.

 
 

10. What is the output of the following program?

b = 
[ -n $b ]
    echo $?
[ -z $b ]
    echo $?
a) 1
   1
b) 2
   2
c) 0
   0
d) 0
   1

View Answer

Answer: c
Clarification: None.

 
 

11. The expression expr -9 % 2 evaluates to:
a) 0
b) 1
c) -1
d) 2
Answer: c
Clarification: None.

12. The statement z = ‘expr 5 / 2’ would store which of the following values in z?
a) 0
b) 1
c) 2
d) 2.5
Answer: c
Clarification: None.

250+ TOP MCQs on Control Statements and Answers

Linux / Unix questions and answers focuses on Control Statements in Awk Programming.1. The break statement
a) jumps out of the innermost for loop
b) jumps out of the innermost while loop
c) jumps out of the innermost do-while loop
d) all of the mentioned

Answer: d
Clarification: None.

2. Which statement skips over the rest of the loop body, causing the next cycle around the loop to begin immediately?
a) continue
b) break
c) next
d) none of the mentioned

Answer: a
Clarification: None.

3. The next statement
a) immediately stops processing the current record
b) go to the next record
c) immediately stops processing the current record & go to the next record
d) none of the mentioned

Answer: c
Clarification: None.

4. If the argument is supplied to the exit statement,
a) its value is used as the exit status code for the awk process
b) syntax error will generate
c) exit returns status 0
d) exit returns status 1

Answer: a
Clarification: None.

5. Which statement instructs gawk to stop processing the current data file?
a) next
b) nextfile
c) exit
d) exitfile

Answer: b
Clarification: None.

6. What is the output of this program?

  1.    #! /usr/bin/awk -f
  2.    BEGIN {
  3.        a=5
  4.        while (a<5) {
  5.            print ""
  6.            a++;
  7.        }
  8.    }

a) nothing will print
b) “” will print 5 times
c) program will generate syntax error
d) none of the mentioned

Answer: a

7. What is the output of this program?

  1.    #! /usr/bin/awk -f
  2.    BEGIN {
  3.        a=0
  4.        do {
  5.            print ""
  6.            a++
  7.        } while (a<5)
  8.    }

a) “” will print 4 times
b) “” will print 5 times
c) nothing will print
d) syntax error

Answer: b

8. What is the output of this program?

  1.    #! /usr/bin/awk -f
  2.    BEGIN {
  3.        a=6
  4.        do {
  5.            print ""
  6.            a++
  7.        } while (a<5)
  8.    }

a) nothing will print
b) “” will print 5 times
c) “” will print 4 times
d) “” will print only 1 time

Answer: d

9. What is the output of this program?

  1.    #! /usr/bin/awk -f
  2.    BEGIN {
  3.        for(i=0;i<=5;i++) {
  4.            print i
  5.            i++
  6.        }
  7.    }

a) 0,2,4 will print
b) 1,3,5 will print
c) 1,2,3,4,5 will print
d) syntax error because i is not initialised

Answer: a

10. The command “awk ‘{if (“9″>”10”) print “” else print “linux”}’”
a) will print “”
b) will print “linux”
c) will generate syntax error
d) none of the mentioned

Answer: c

250+ TOP MCQs on Stages of Compilation and Answers

1. The lines in our code that begin with the “#” character are
a) preprocessor directives
b) macros
c) header files
d) none of the mentioned

Answer: a
Clarification: None.

2. Command line parameters are passed by the
a) preprocessor
b) assembler
c) compiler
d) linker

Answer: d
Clarification: None.

3. In the preprocessing stage of compilation
a) header files are actually expanded and included in the source code of the program
b) macros are replaced by their respective values
c) all the comments are stripped off
d) all of the mentioned

Answer: d
Clarification: None.

4. Which compilation step makes sure that all the undefined symbols in the code are resolved.
a) linking
b) compiling
c) preporcessing
d) none of the mentioned

Answer: a
Clarification: An undefined symbol is one for which there is no definition available.

5. Which one of the following is not true?
a) we need to manually go through all intermediate stages to generate an executable in gcc
b) gcc compilation process always contains 4 stages
c) gcc compilation process always contains 3 stages
d) none of the mentioned

Answer: a
Clarification: None.

6. Preprocessed files are given the file extension _____ for c++ programs.
a) .i
b) .ii
c) .iii
d) none of the mentioned

Answer: b
Clarification: None.

7. The assmebly code generated depends upon the
a) processor archietecture
b) ram size
c) both processor archietecture and RAM size
d) none of the mentioned

Answer: c
Clarification: None.

8. The COFF stands for
a) common object file format
b) combined operation for file formats
c) combined object file format
d) none of the mentioned

Answer: a
Clarification: None.

250+ TOP MCQs on File Management 1 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. Each process has unique
a) fd table
b) file table
c) inode table
d) data block table
Answer: a
Clarification: None.

2. File descriptor table indexes which kernel structure?
a) struct file
b) strruct fs_struct
c) files_struct
d) struct inode
Answer: a
Clarification: None.

3. What is the default number of files open per user process?
a) 0
b) 1
c) 2
d) 3
Answer: d
Clarification: None.

4. The file system information is stored in
a) Boot block
b) Super Block
c) Inode Table
d) Data Block
Answer: b
Clarification: None.

5. Switch table is used by
a) device special file
b) directory file
c) fifo
d) link file
Answer: a
Clarification: None.

6. What is the use of fcntl function?
a) locking a file
b) reading the file descriptor flag
c) changing the file status flag
d) all of the mentioned
Answer: d
Clarification: None.

7. Which function can be used instead of the dup2 to duplicate the file descriptor?
a) read()
b) open()
c) stat()
d) fcntl()
Answer: d
Clarification: None.

8. printf() uses which system call
a) open
b) read
c) write
d) close
Answer: c
Clarification: None.

9. read() system call on success returns
a) 0
b) -1
c) number of character
d) none
Answer: c
Clarification: None.

10. Which system call is used to create a hard link?
a) hardlink
b) link
c) symlink
d) ln
Answer: b
Clarification: None.

11. namei() is
a) ANSI C library function
b) C library function
c) System call
d) kernel routine
Answer: d
Clarification: None.

12. dup2(1,0)
a) closes the stdout and copies the stdin descriptor to stdout
b) closes the stdin and copies the stdout descriptor to stdin
c) will produce compilation error
d) None of the mentioned
Answer: b
Clarification: None.

250+ TOP MCQs on POSIX IPCs – Message Queues, Shared Memory and Semaphores and Answers

Linux Debugging questions and answers focuses on POSIX IPCs i.e. Message Queues, Shared Memory and Semaphores.

1. What is the output of this program?

  1.    #include
  2.    #include
  3.    #include
  4.    #include
  5.    #include
  6.    #include
  7.  
  8.    struct data_st{
  9.        long int id;
  10.        char buff[11];
  11.    };
  12.    int main()
  13.    {
  14.        int m_id;
  15.        struct data_st data1, data2;
  16.        m_id = msgget((key_t)181,0666|IPC_CREAT);
  17.        if(m_id == -1)
  18.            perror("msgget");
  19.        data1.id = 1;
  20.        strcpy(data1.buff,"");
  21.        if(msgsnd(m_id,&data1,11,0) == -1)
  22.            perror("msgsnd");
  23.        if(msgrcv(m_id,&data2,11,0) == -1)
  24.            perror("msgrcv");
  25.        printf("%sn",data2.buff);
  26.        if(msgctl(m_id,IPC_RMID,0) != 0)
  27.            perror("msgctl");
  28.        return 0;
  29.    }

a) this program will print the string “”
b) this program will give an error
c) this program will give segmentaion fault
d) none of the mentioned
Answer: b
Clarification: The fourth argument of the function msgrcv() is missing in this program.
Output:
[[email protected] ]# gcc -o san san.c
san.c: In function ‘main’:
san.c:24:2: error: too few arguments to function ‘msgrcv’
/usr/include/sys/msg.h:73:16: note: declared here
[[email protected] ]#

2. What is the output of this program?

  1.    #include
  2.    #include
  3.    #include
  4.    #include
  5.    #include
  6.    #include
  7.  
  8.    struct data_st{
  9.        long int id;
  10.        char buff[11];
  11.    };
  12.    int main()
  13.    {
  14.        int m_id;
  15.        struct data_st data1, data2;
  16.        m_id = msgget((key_t)181,0666|IPC_CREAT);
  17.        if(m_id == -1)
  18.            perror("msgget");
  19.        data1.id = 1;
  20.        strcpy(data1.buff,"");
  21.        if(msgsnd(m_id,&data1,11,0) == -1)
  22.            perror("msgsnd");
  23.        if(msgctl(m_id,IPC_RMID,0) != 0)
  24.            perror("msgctl");
  25.        if(msgrcv(m_id,&data2,11,1,0) == -1)
  26.            perror("msgrcv");
  27.        printf("%sn",data2.buff);
  28.        return 0;
  29.    }

a) this program will print the string “”
b) this program will print the garbage value
c) this program will give segmentation fault
d) none of the mentioned
Answer: b
Clarification: The message queue has been removed before recieving the message. Hence the program prints the grabage value of the buffer.
Output:
[[email protected] ]# ./san
msgrcv: Invalid argument
Ѕ�
[[email protected] ]#

3. What is the output of this program?

  1.    #include
  2.    #include
  3.    #include
  4.    #include
  5.    #include
  6.    #include
  7.  
  8.    struct data_st{
  9.        long int id;
  10.        char buff[11];
  11.    };
  12.    int main()
  13.    {
  14.        int m_id,ret;
  15.        struct data_st data1, data2;
  16.        m_id = msgget((key_t)181,0666|IPC_CREAT);
  17.        if(m_id == -1)
  18.            perror("msgget");
  19.        data1.id = 1;
  20.        strcpy(data1.buff,"");
  21.        ret = msgsnd(m_id,&data1,11,0);
  22.        printf("%dn",ret);
  23.        if(msgrcv(m_id,&data2,11,1,0) == -1)
  24.            perror("msgrcv");
  25.        if(msgctl(m_id,IPC_RMID,0) != 0)
  26.            perror("msgctl");
  27.        return 0;
  28.    }

a) 0
b) 1
c) -1
d) none of the mentioned
Answer: a
Clarification: The function msgsnd() returns 0 when there is no error.
Ouptut:
[[email protected] ]# gcc -o san san.c
[[email protected] ]# ./san
0
[[email protected] ]#

4. What is the output of this program?

  1.    #include
  2.    #include
  3.    #include
  4.    #include
  5.    #include
  6.    #include
  7.  
  8.    struct data_st{
  9.        long int id;
  10.        char buff[11];
  11.    };
  12.    int main()
  13.    {
  14.        int m_id,ret;
  15.        struct data_st data1, data2;
  16.        m_id = msgget((key_t)181,0666|IPC_CREAT);
  17.        if(m_id == -1)
  18.            perror("msgget");
  19.        data1.id = 1;
  20.        strcpy(data1.buff,"");
  21.        if(msgsnd(m_id,&data1,11,0) == -1)
  22.            perror("msgsnd");
  23.        ret = msgrcv(m_id,&data2,11,1,0);
  24.        printf("%dn",ret);     
  25.        if(msgctl(m_id,IPC_RMID,0) != 0)
  26.            perror("msgctl");
  27.        return 0;
  28.    }

a) 0
b) -1
c) 1
d) 11
Answer: d
Clarification: The function msgrcv() returns the number of bytes actually copied into its second argument.
Output:
[[email protected] ]# gcc -o san san.c
[[email protected] ]# ./san
11
[[email protected] ]#

5. What is the output of this pogram?

  1.    #include
  2.    #include
  3.    #include
  4.    #include
  5.    #include
  6.  
  7.    static int sem_p(void);
  8.    static int sem_v(void);
  9.    union semun{
  10.        int val;
  11.        struct semid_ds *buf;
  12.        unsigned short array;
  13.    };
  14.    int sem_id;
  15.    struct semid_ds myds;
  16.    struct sembuf mybuf;
  17.    union semun myun;
  18.    static int sem_p(void)
  19.    {
  20.        mybuf.sem_num = 0;
  21.        mybuf.sem_op = -1;
  22.        mybuf.sem_flg = SEM_UNDO;
  23.        semop(sem_id,&mybuf,1);
  24.    }
  25.    static int sem_v(void)
  26.    {
  27.        mybuf.sem_num = 0;
  28.        mybuf.sem_op = 1;
  29.        mybuf.sem_flg = SEM_UNDO;
  30.        semop(sem_id,&mybuf,1);
  31.    }
  32.    int main()
  33.    {
  34.        int wfd, rfd;
  35.        sem_id = semget((key_t)911,1,0666 | IPC_CREAT);
  36.        myun.val = 1;
  37.        semctl(sem_id,0,SETVAL,myun);
  38.        sem_p();
  39.        printf("n");
  40.        sem_v();
  41.        semctl(sem_id,0,IPC_RMID,myun);
  42.        return 0;
  43.    }

a) this program will print the string “”
b) this process will remain block
c) this program will print the string “” & process will remain block
d) none of the mentioned
Answer: a
Clarification: The function sem_p() will increment the value of semaphore but it will not block the process.
Output:
[[email protected] ]# gcc -o san san.c
[[email protected] ]# ./san

[[email protected] ]#

6. What is the output of this pogram?

  1.    #include
  2.    #include
  3.    #include
  4.    #include
  5.    #include
  6.  
  7.    static int sem_p(void);
  8.    static int sem_v(void);
  9.    union semun{
  10.        int val;
  11.        struct semid_ds *buf;
  12.        unsigned short array;
  13.    };
  14.    int sem_id;
  15.    struct semid_ds myds;
  16.    struct sembuf mybuf;
  17.    union semun myun;
  18.    static int sem_p(void)
  19.    {
  20.        mybuf.sem_num = 0;
  21.        mybuf.sem_op = -1;
  22.        mybuf.sem_flg = SEM_UNDO;
  23.        semop(sem_id,&mybuf,1);
  24.    }
  25.    static int sem_v(void)
  26.    {
  27.        mybuf.sem_num = 0;
  28.        mybuf.sem_op = 1;
  29.        mybuf.sem_flg = SEM_UNDO;
  30.        semop(sem_id,&mybuf,1);
  31.    }
  32.    int main()
  33.    {
  34.        int wfd, rfd;
  35.        sem_id = semget((key_t)911,1,0666 | IPC_CREAT);
  36.        myun.val = 0;
  37.        semctl(sem_id,0,SETVAL,myun);
  38.        sem_p();
  39.        printf("n");
  40.        sem_v();
  41.        semctl(sem_id,0,IPC_RMID,myun);
  42.        return 0;
  43.    }

a) this program will print the string “”
b) this process will remain block
c) this program will print the string “” & process will remain block
d) none of the mentioned
Answer: b
Clarification: The initial value of semaphore in this program is 0. Hence the function sem_p() will block the process.
Output:
[[email protected] ]# gcc -o san san.c
[[email protected] ]# ./san
^Z
[24]+ Stopped ./san
[[email protected] ]#

7. What is the output of this program?

  1.    #include
  2.    #include
  3.    #include
  4.    #include
  5.    #include
  6.  
  7.    static int sem_p(void);
  8.    static int sem_v(void);
  9.    union semun{
  10.        int val;
  11.        struct semid_ds *buf;
  12.        unsigned short array;
  13.    };
  14.    int sem_id;
  15.    struct semid_ds myds;
  16.    struct sembuf mybuf;
  17.    union semun myun;
  18.    static int sem_p(void)
  19.    {
  20.        mybuf.sem_num = 0;
  21.        mybuf.sem_op = -1;
  22.        mybuf.sem_flg = SEM_UNDO;
  23.        semop(sem_id,&mybuf,1);
  24.    }
  25.    static int sem_v(void)
  26.    {
  27.        mybuf.sem_num = 0;
  28.        mybuf.sem_op = 1;
  29.        mybuf.sem_flg = SEM_UNDO;
  30.        semop(sem_id,&mybuf,1);
  31.    }
  32.    int main()
  33.    {
  34.        int wfd, rfd;
  35.        sem_id = semget((key_t)911,1,0666 | IPC_CREAT);
  36.        myun.val = 0;
  37.        semctl(sem_id,0,IPC_RMID,myun);
  38.        semctl(sem_id,0,SETVAL,myun);
  39.        sem_p();
  40.        printf("n");
  41.        sem_v();
  42.        return 0;
  43.    }

a) this process will remain block
b) this program will print the string “”
c) this program will print the string “” & process will remain block
d) none of the mentioned
Answer: b
Clarification: The semaphore has been removed before calling the function sem_p(). Hence the function sem() will not affect the process.
Output:
[[email protected] ]# gcc -o san san.c
[[email protected] ]# ./san

[[email protected] ]#

8. What is the output of second program if we run the san1 first and after that we run san2 in the different terminal?

  1.    /*This is san1.c*/
  2.    #include
  3.    #include
  4.    #include
  5.    #include
  6.  
  7.    int main()
  8.    {
  9.        int shm_id;
  10.        char *addr;
  11.        struct shmid_ds ds;
  12.        shm_id = shmget((key_t)1234,10,0666|IPC_CREAT);
  13.        if(shm_id == -1){
  14.            perror("shmget");
  15.        }
  16.        addr = (char*)shmat(shm_id,NULL,SHM_RND);
  17.        if(addr == (char *)-1){
  18.            perror("shmat");
  19.        }
  20.        strcpy(addr,"");
  21.        if (shmdt(addr) != 0){
  22.            perror("shmdt");
  23.        }
  24.        sleep(10);      
  25.        if( shmctl(shm_id,IPC_RMID,0) == -1){
  26.            perror("shmctl");
  27.        }
  28.        return 0;
  29.    }
  30.    /*This is san2.c*/
  31.    #include
  32.    #include
  33.    #include
  34.  
  35.    int main()
  36.    {
  37.        int shm_id;
  38.        char *addr;
  39.        struct shmid_ds ds;
  40.        shm_id = shmget((key_t)1234,10,0666|IPC_CREAT);
  41.        if(shm_id == -1){
  42.            perror("shmget");
  43.        }
  44.        addr = (char*)shmat(shm_id,NULL,SHM_RND);
  45.        if(addr == (char *)-1){
  46.            perror("shmat");
  47.        }
  48.        printf("%sn",addr);
  49.        if (shmdt(addr) != 0){
  50.            perror("shmdt");
  51.        }
  52.        return 0;
  53.     }

a) the program will print the string “”
b) the program will nothing
c) segmentaion fault
d) none of the mentioned
Answer: a
Clarification: The process of san1.c has written the string “” in the shared memory and the process of san2.c accessed the string from the shared memory. This is valid only for 10 seconds from the execution of first program san1.c.
Output1:
[[email protected] ]# gcc -o san1 san1.c
[[email protected] ]# ./san1

Output2:
[[email protected] ]# gcc -o san2 san2.c
[[email protected] ]# ./san2

[[email protected] ]#

9. What is the output of second program if we run the san1 first and after that we run san2 in the different terminal?

  1.    /*This is san1.c*/
  2.    #include
  3.    #include
  4.    #include
  5.    #include
  6.  
  7.    int main()
  8.    {
  9.        int shm_id;
  10.        char *addr;
  11.        struct shmid_ds ds;
  12.        shm_id = shmget((key_t)1234,10,0666|IPC_CREAT);
  13.        if(shm_id == -1){
  14.            perror("shmget");
  15.        }
  16.        addr = (char*)shmat(shm_id,NULL,SHM_RND);
  17.        if(addr == (char *)-1){
  18.            perror("shmat");
  19.        }
  20.        strcpy(addr,"");
  21.        if (shmdt(addr) != 0){
  22.            perror("shmdt");
  23.        }
  24.        if( shmctl(shm_id,IPC_RMID,0) == -1){
  25.            perror("shmctl");
  26.        }
  27.        return 0;
  28.    }
  29.    /*This is san2.c*/
  30.    #include
  31.    #include
  32.    #include
  33.  
  34.    int main()
  35.    {
  36.        int shm_id;
  37.        char *addr;
  38.        struct shmid_ds ds;
  39.        shm_id = shmget((key_t)1234,10,0666|IPC_CREAT);
  40.        if(shm_id == -1){
  41.            perror("shmget");
  42.        }
  43.        addr = (char*)shmat(shm_id,NULL,SHM_RND);
  44.        if(addr == (char *)-1){
  45.            perror("shmat");
  46.        }
  47.        printf("%sn",addr);
  48.        if (shmdt(addr) != 0){
  49.            perror("shmdt");
  50.        }
  51.        return 0;
  52.     }

a) the program will print the string “”
b) the program will nothing
c) segmentaion fault
d) none of the mentioned
Answer: b
Clarification: The process of san1.c has written the string “” in the shared memory and the process of san2.c could not access the string from the shared memory due to delay.
Output1:
[[email protected] ]# gcc -o san1 san1.c
[[email protected] ]# ./san1

Output2:
[[email protected] ]# gcc -o san2 san2.c
[[email protected] ]# ./san2

[[email protected] ]#

10. What is the output of second program if we run the san1 first and after that we run san2 in the different terminal?

  1.     /*This is san1.c*/
  2.     #include
  3.     #include
  4.     #include
  5.     #include
  6.  
  7.     int main()
  8.     {
  9.         int shm_id;
  10.         char *addr;
  11.         struct shmid_ds ds;
  12.         shm_id = shmget((key_t)1234,10,0666|IPC_CREAT);
  13.         if(shm_id == -1){
  14.             perror("shmget");
  15.         }
  16.         addr = (char*)shmat(shm_id,NULL,SHM_RND);
  17.         if(addr == (char *)-1){
  18.             perror("shmat");
  19.         }
  20.         strcpy(addr,"");
  21.         if (shmdt(addr) != 0){
  22.             perror("shmdt");
  23.         }
  24.         sleep(10);      
  25.         if( shmctl(shm_id,IPC_RMID,0) == -1){
  26.             perror("shmctl");
  27.         }
  28.         return 0;
  29.     }
  30.     /*This is san2.c*/
  31.     #include
  32.     #include
  33.     #include
  34.  
  35.     int main()
  36.     {
  37.         int shm_id;
  38.         char *addr;
  39.         struct shmid_ds ds;
  40.         shm_id = shmget((key_t)111,10,0666|IPC_CREAT);
  41.         if(shm_id == -1){
  42.             perror("shmget");
  43.         } 
  44.         addr = (char*)shmat(shm_id,NULL,SHM_RND);
  45.         if(addr == (char *)-1){
  46.             perror("shmat");
  47.         } 
  48.         printf("%sn",addr);
  49.         if (shmdt(addr) != 0){
  50.             perror("shmdt");
  51.         }
  52.         return 0;
  53.      }

a) the program will print the string “”
b) the program will nothing
c) segmentaion fault
d) none of the mentioned
Answer: a
Clarification: The process of san1.c has written the string “” in the shared memory and the process of san2.c could not access that shared memory because the key is different.
Output1:
[[email protected] ]# gcc -o san1 san1.c
[[email protected] ]# ./san1

Output2:
[[email protected] ]# gcc -o san2 san2.c
[[email protected] ]# ./san2

[[email protected] ]#

250+ TOP MCQs on Linux Environment and Answers

Linux / Unix questions and answers is useful for accessments, entrace exams and other competitive exams on Linux.

1. Solaris is the name of a flavor of UNIX from
a) HP
b) IBM
c) Digital Equipment Corp
d) Sun Microsystems
Answer: d
Clarification: None.

2. Which of the following is “NOT” a UNIX variant ?
a) Solaris
b) AIX
c) IRIX
d) AS400
Answer: d
Clarification: None.

3. The system calls in UNIX is written using which language
a) C
b) C++
c) Assembly Language
d) Fortran
Answer: a
Clarification: None.

4. Which of the following enables multi-tasking in UNIX?
a) Time Sharing
b) Multi programming
c) Multi user
d) Modularity
Answer: a
Clarification: None.

5. Which of the following is considered as the super daemon in Unix?
a) sysinit
b) init
c) inetd
d) proc
Answer: b
Clarification: None.

6. Unix is which kind of Operating System?
a) Multi User
b) Multi Processes
c) Multi Tasking
d) All of the mentioned
Answer: d
Clarification: None.

7. SVR4 stands for?
a) Standard Version Release 4
b) System Version Release 4
c) Standard Five Release 4
d) System Five Release 4
Answer: d
Clarification: None.

8. Lp0 device file is used to access:
a) Floppy
b) Cdrom
c) Printer
d) Tape drive
Answer: c
Clarification: None.

9. Syntax of any Unix command is:
a) command [options] [arguments].
b) command options [arguments].
c) command [options] [arguments].
d) command options arguments
Answer: a
Clarification: None.