Linux Set 5 (30 mcqs)

1. Which one of the following string will print first by this program?

#include<stdio.h>
#include<pthread.h>

void *fun_t(void *arg);
void *fun_t(void *arg)
{
printf(“Sanfoundry\n”);
pthread_exit(“Bye”);
}
int main()
{
pthread_t pt;
void *res_t;
if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
perror(“pthread_create”);
printf(“Linux\n”);
if(pthread_join(pt,&res_t) != 0)
perror(“pthread_join”);
return 0;
}
a) Linux
b) Sanfoundry
c) it can not be predicted
d) none of the mentioned
2. What is the output of this program?

#include<stdio.h>
#include<pthread.h>

void *fun_t(void *arg);
void *fun_t(void *arg)
{
int ret;
ret = pthread_exit(“Bye”);
printf(“%d\n”,ret);
}
int main()
{
pthread_t pt;
void *res_t;
if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
perror(“pthread_create”);
if(pthread_join(pt,&res_t) != 0)
perror(“pthread_join”);
return 0;
}
a) 0
b) 1
c) -1
d) none of the mentioned
3. What is the output of this program?

#include<stdio.h>
#include<pthread.h>

void *fun_t(void *arg);
void *fun_t(void *arg)
{
printf(“Sanfoundry\n”);
pthread_exit(“Bye”);
}
int main()
{
pthread_t pt;
void *res_t;
if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
perror(“pthread_create”);
return 0;
}
a) this program will print the string “Sanfoundry”
b) this program will print nothing
c) segmentation fault
d) run time error
4. What is the output of this program?

#include<stdio.h>
#include<pthread.h>

void *fun_t(void *arg);
void *fun_t(void *arg)
{
printf(“%d\n”,a);
pthread_exit(“Bye”);
}
int main()
{
int a;
pthread_t pt;
void *res_t;
a = 10;
if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
perror(“pthread_create”);
if(pthread_join(pt,&res_t) != 0)
perror(“pthread_join”);
return 0;
}
a) 10
b) 0
c) -1
d) none of the mentioned
5. What is the output of this program?

#include<stdio.h>
#include<pthread.h>

int a;
void *fun_t(void *arg);
void *fun_t(void *arg)
{
printf(“%d\n”,a);
pthread_exit(“Bye”);
}
int main()
{
pthread_t pt;
void *res_t;
a = 10;
if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
perror(“pthread_create”);
if(pthread_join(pt,&res_t) != 0)
perror(“pthread_join”);
return 0;
}
a) 10
b) 0
c) -1
d) none of the mentioned
6. What is the output of this program?

#include<stdio.h>
#include<pthread.h>

int a;
void *fun_t(void *arg);
void *fun_t(void *arg)
{
a = 20;
pthread_exit(“Bye”);
}
int main()
{
pthread_t pt;
void *res_t;
a = 10;
if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
perror(“pthread_create”);
if(pthread_join(pt,&res_t) != 0)
perror(“pthread_join”);
printf(“%d\n”,a);
return 0;
}
a) 10
b) 20
c) segmentation fault
d) none of the mentioned
7. Which one of the following statement is not true about this program?

#include<stdio.h>
#include<pthread.h>

void *fun_t(void *arg);
void *fun_t(void *arg)
{
printf(“%d\n”,getpid());
pthread_exit(“Bye”);
}
int main()
{
pthread_t pt;
void *res_t;
if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
perror(“pthread_create”);
if(pthread_join(pt,&res_t) != 0)
perror(“pthread_join”);
printf(“%d\n”,getpid());
return 0;
}
a) both printf statements will print the same value
b) both printf statements will print the different values
c) this program will print nothing
d) none of the mentioned
8. What is the output of this program?

#include<stdio.h>
#include<pthread.h>
#include<fcntl.h>

int fd;
void *fun_t(void *arg);
void *fun_t(void *arg)
{
char buff[10];
int count;
count = read(fd,buff,10);
printf(“%d\n”,count);
pthread_exit(“Bye”);
}
int main()
{
pthread_t pt;
void *res_t;
fd = open(“san.c”,O_RDONLY);
if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
perror(“pthread_create”);
if(pthread_join(pt,&res_t) != 0)
perror(“pthread_join”);
return 0;
}
a) 10
b) 0
c) -1
d) segmentation fault
9. What is the output of this program?

#include<stdio.h>
#include<pthread.h>
#include<fcntl.h>

void *fun_t(void *arg);
void *fun_t(void *arg)
{
pthread_exit(“Bye”);
printf(“Sanfoundry\n”);
}
int main()
{
pthread_t pt;
void *res_t;
if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
perror(“pthread_create”);
if(pthread_join(pt,&res_t) != 0)
perror(“pthread_join”);
printf(“%s\n”,res_t);
return 0;
}
a) Sanfoundry
b) Bye
c) segementation fault
d) run time error
10. What is the output of this program?

#include<stdio.h>
#include<pthread.h>

void *fun_t(void *arg);
void *fun_t(void *arg)
{
sleep(1);
}
int main()
{
pthread_t pt;
void *res_t;
if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
perror(“pthread_create”);
if(pthread_join(pt,&res_t) != 0)
perror(“pthread_join”);
printf(“%s\n”,res_t);
return 0;
}
a) this process will pause
b) segmentation fault
c) run time error
d) none of the mentioned

11. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.
4.       int main()
5.       {
6.   pid_t fd;
7.   char ch;
8.   int count;
9.   fd = open(“san.c”,O_RDONLY);
10.   do{
11.       count = read(fd,&ch,1);
12.       printf(“%c”,ch);
13.   }while(count);
14.   return 0;
15.       }
a) it will print nothing
b) it will print the source code of the source file “san.c”
c) segmentation fault
d) none of the mentioned

12. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.
4.       int main()
5.       {
6.   int fd, count;
7.   fd = open(“sanfoundry.txt”,O_WRONLY|O_CREAT);
8.   count = write(fd,”Linux System Programming”,5);
9.   if(count != 5)
10.       perror(“write”);
11.   return 0;
12.       }
a) it will create a file “sanfoundry.txt” in the present working directory
b) it will write the string “Linux System Programming” in the file “sanfoundry.txt”
c) both (a) and (b)
d) none of the mentioned

13. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.
4.       int main()
5.       {
6.   int fd, count;
7.   fd = open(“san.c”,O_RDONLY);
8.   count = write(fd,”Linux”,5);
9.   if(count != 5)
10.       perror(“write”);
11.   return 0;
12.       }
a) it will write the string “Linux” in the beginning of source file “san.c”
b) it will write the string “Linux” in the end of the source file “san.c”
c) segmentation fault
d) none of the mentioned

14. What is the output of this program?
1.       #include<stdio.h>
2.       #include<stdlib.h>
3.       #include<fcntl.h>
4.
5.       int main()
6.       {
7.   int fd, count;
8.   char ch, *buff;
9.   buff = (char *)malloc(sizeof(char)*10);
10.   fd = open(“san.c”,O_RDONLY);
11.   count = read(fd,buff,5);
12.   printf(“%d\n”,count);
13.   return 0;
14.       }
a) 5
b) 10
c) 0
d) -1

15. In the output of this program, the string “/* Linux */” will be added at the ____ of the source file.
1.       #include<stdio.h>
2.       #include<stdlib.h>
3.       #include<fcntl.h>
4.
5.       int main()
6.       {
7.   int fd;
8.   fd = open(“san.c”,O_RDWR|O_APPEND);
9.   write(fd,”/* Linux */”,11);
10.   return 0;
11.       }
a) end
b) beginning
c) second line
d) third line

16. What is the output of this program?
1.       #include<stdio.h>
2.       #include<stdlib.h>
3.       #include<fcntl.h>
4.
5.       int main()
6.       {
7.   int fd;
8.   char *buff;
9.   buff = (char *)malloc(sizeof(char)*5);
10.   fd = open(“sanfoundry.txt”,O_RDWR|O_CREAT);
11.   write(fd,”Linux”,5);
12.   read(fd,buff,5);
13.   printf(“%s\n”,buff);
14.       }
a) it will print nothing
b) it will print the string “Linux”
c) segmentation fault
d) none of the mentioned

17. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.
4.       int main()
5.       {
6.   int fd, count;
7.   char ch;
8.   fd = open(“sanfoundry.txt”,O_RDWR|O_CREAT);
9.   write(fd,”s”,1);
10.   lseek(fd,0,SEEK_SET);
11.   write(fd,”d”,1);
12.   lseek(fd,0,0);
13.   read(fd,&ch,1);
14.   printf(“%c\n”,ch);
15.   return 0;
16.       }
a) d
b) s
c) sd
d) none of the mentioned

18. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.
4.       int main()
5.       {
6.   int fd, count;
7.   char ch[10];
8.   fd = open(“sanfoundry.txt”,O_RDWR|O_CREAT);
9.   write(fd,”linux”,5);
10.   lseek(fd,2,SEEK_END);
11.   write(fd,”san”,3);
12.   lseek(fd,0,0);
13.   count = read(fd,ch,10);
14.   printf(“%s\n”,ch);
15.   return 0;
16.       }
a) linux
b) linuxsan
c) linux san
d) none of the mentioned

19. What is the output of this program?
1.       #include<stdio.h>
2.       #include<stdlib.h>
3.       #include<fcntl.h>
4.
5.       int main()
6.       {
7.   int fd, new_fd;
8.   char *buff;
9.   buff = (char *)malloc(sizeof(char)*8);
10.   fd = open(“san.c”,O_RDONLY);
11.   new_fd = dup(fd);
12.   close(fd);
13.   read(new_fd,buff,8);
14.   printf(“%s\n”,buff);
15.       }
a) this program will not print anything
b) this program will print “#include”
c) this program will give the segmentation fault
d) this program will give the syntax error

20. What is the output of this program?
1.#include<stdio.h>
2.#include<fcntl.h>
3.
4.int main()
5.{
6.    int fd, fd2, ret;
7.    fd = open(“san.c”,O_RDONLY);
8.    ret = close(fd2);
9.    printf(“%d\n”,ret);
10.}
a) 0
b) 1
c) -1
d) none of the mentioned

21. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.       #include<sys/stat.h>
4.       #include<semaphore.h>
5.
6.       int main()
7.       {
8.   sem_t* sem_id;
9.   sem_id = sem_open(“sem_value”,O_CREAT,0666,0);
10.   if(sem_id == SEM_FAILED)
11.       perror(“sem_open”);
12.   sem_wait(sem_id);
13.   printf(“Sanfoundry\n”);
14.   if(sem_close(sem_id) == -1)
15.       perror(“sem_close”);
16.   return 0;
17.       }
a) this program will print the string “Sanfoundry”
b) this process will block
c) segmentaion fault
d) none of the mentioned

22. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.       #include<sys/stat.h>
4.       #include<semaphore.h>
5.
6.       int main()
7.       {
8.   sem_t* sem_id;
9.   int value;
10.   sem_id = sem_open(“sem_value”,O_CREAT,0666,0);
11.   if(sem_id == SEM_FAILED)
12.       perror(“sem_open”);
13.   if(sem_getvalue(sem_id,&value) == -1)
14.       perror(“sem_getvalue”);
15.   printf(“%d\n”,value);
16.   sem_wait(sem_id);
17.   printf(“Sanfoundry\n”);
18.   if(sem_close(sem_id) == -1)
19.       perror(“sem_close”);
20.   return 0;
21.       }
a) 0
b) Sanfoundry
c) both (a) and (b)
d) none of the mentioned

23. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.       #include<sys/stat.h>
4.       #include<semaphore.h>
5.
6.       int main()
7.       {
8.   sem_t* sem_id;
9.   sem_id = sem_open(“sem_value”,O_CREAT,0666,0);
10.   if(sem_id == SEM_FAILED)
11.       perror(“sem_open”);
12.   sem_post(sem_id);
13.   printf(“Sanfoundry\n”);
14.   if(sem_close(sem_id) == -1)
15.       perror(“sem_close”);
16.   return 0;
17.       }
a) this process will block
b) this program will print the string “Sanfoundry”
c) segmentation fault
d) none of the mentioned

24. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.       #include<sys/stat.h>
4.       #include<semaphore.h>
5.
6.       int main()
7.       {
8.   sem_t* sem_id;
9.   sem_id = sem_open(“sem_value”,O_CREAT,0666,0);
10.   if(sem_id == SEM_FAILED)
11.       perror(“sem_open”);
12.   if(sem_close(sem_id) == -1)
13.       perror(“sem_close”);
14.   sem_wait(sem_id);
15.   printf(“Sanfoundry\n”);
16.   return 0;
17.       }
a) this process will block
b) this program will print the string “Sanfoundry”
c) segmentation fault
d) none of the mentioned

25. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.       #include<sys/stat.h>
4.       #include<semaphore.h>
5.
6.       int main()
7.       {
8.   sem_t* sem_id;
9.   int value;
10.   sem_id = sem_open(“new_13”,O_CREAT,0666,3);
11.   if(sem_id == SEM_FAILED)
12.       perror(“sem_open”);
13.   sem_wait(sem_id);
14.   sem_wait(sem_id);
15.   sem_wait(sem_id);
16.   sem_wait(sem_id);
17.   sem_post(sem_id);
18.   sem_post(sem_id);
19.   sem_getvalue(sem_id,&value);
20.   printf(“%d\n”,value);
21.   if(sem_close(sem_id) == -1)
22.       perror(“sem_close”);
23.   return 0;
24.       }
a) 2
b) 3
c) 0
d) none of the mentioned

26. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.       #include<sys/stat.h>
4.       #include<sys/mman.h>
5.
6.       int main()
7.       {
8.   int s_id;
9.   s_id = shm_open(“shared_mem”,O_CREAT|O_RDWR,0666);
10.   printf(“%d\n”,s_id);
11.   if(shm_unlink(“shared_mem”) == -1)
12.       perror(“shm_unlink”);
13.   return 0;
14.       }
a) -1
b) 1
c) 2
d) 3

27. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.       #include<sys/stat.h>
4.       #include<sys/mman.h>
5.
6.       int main()
7.       {
8.   int s_id;
9.   int *ptr;
10.   s_id = shm_open(“shared_mem”,O_CREAT|O_RDWR,0666);
11.   if(s_id == -1)
12.       perror(“shm_open”);
13.   ptr = mmap(NULL,100,PROT_WRITE,MAP_PRIVATE,s_id,0);
14.   if(ptr == MAP_FAILED);
15.       perror(“mmap”);
16.   if(munmap(ptr,100) == -1)
17.       perror(“munmap”);
18.   if(shm_unlink(“shared_mem”) == -1)
19.       perror(“shm_unlink”);
20.   return 0;
21.       }
a) mmap: Success
b) mmap: Failure
c) munmap: Success
d) munmap: Failure

28. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.       #include<sys/stat.h>
4.       #include<sys/mman.h>
5.
6.       int main()
7.       {
8.   int s_id;
9.   int *ptr;
10.   s_id = shm_open(“shared_mem”,O_CREAT|O_RDWR,0666);
11.   if(s_id == -1)
12.       perror(“shm_open”);
13.   ptr = mmap(NULL,100,PROT_WRITE,MAP_PRIVATE,s_id,0);
14.   if(ptr == MAP_FAILED);
15.       perror(“mmap”);
16.   ptr = mmap(ptr,100,PROT_WRITE,MAP_PRIVATE,s_id,0);
17.   if(ptr == MAP_FAILED);
18.       perror(“mmap”);
19.   if(munmap(ptr,100) == -1)
20.       perror(“munmap”);
21.   if(shm_unlink(“shared_mem”) == -1)
22.       perror(“shm_unlink”);
23.   return 0;
24.       }
a) mmap: Success
mmap: Success
b) mmap: Success
mmap: Failure
c) segmentation fault
d) none of the mentioned

29. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.       #include<errno.h>
4.       #include<sys/stat.h>
5.       #include<sys/mman.h>
6.
7.       int main()
8.       {
9.   int s_id;
10.   s_id = shm_open(“shared_mem”,O_CREAT|O_RDWR,0666);
11.   if(s_id != EACCES)
12.       perror(“Permission granted\n”);
13.   return 0;
14.       }
a) Permission granted
: Success
b) Permission granted
c) segmentation fault
d) none of the mentioned

30. What is the output of this program?
1.#include<stdio.h>
2.#include<fcntl.h>
3.#include<errno.h>
4.#include<sys/stat.h>
5.#include<sys/mman.h>
6.
7.int main()
8.{
9.    int s_id;
10.    s_id = shm_open(“shared_memory”,O_TRUNC,0666);
11.    if(s_id == -1)
12.        perror(“shm_open\n”);
13.    return 0;
14.}
a) this program will give an error because OTRUNC is not a valid flag
b) this program will give an error
c) this program will give segmentation fault
d) none of the mentioned

Answers

1-b 2-d 3-b 4-d 5-a
6-b 7-a 8-a 9-b 10-b
11-a 12-a 13-d 14-a 15-a
16-a 17-d 18-a 19-b 20-c
21-b 22-a 23-b 24-c 25-d
26-d 27-a 28-a 29-a 30-b

Sharing is caring!

Leave a Comment

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

shares