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

Leave a Reply

Your email address will not be published. Required fields are marked *