1. What is the output of this program?
#include<stdio.h>
#include<fcntl.h>
int main()
{
pid_t fd;
char ch;
int count;
fd = open("san.c",O_RDONLY);
do{
count = read(fd,&ch,1);
printf("%c",ch);
}while(count);
return 0;
}
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
View Answer
Explanation: none.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
#include<stdio.h>
#include<fcntl.h>
int main()
{
int fd, count;
char ch;
fd = open("san.c",O_RDONLY);
do{
count = read(fd,&ch,1);
printf("%c",ch);
}while(count);
}
2. What is the output of this program?
#include<stdio.h>
#include<fcntl.h>
int main()
{
int fd, count;
fd = open("sanfoundry.txt",O_WRONLY|O_CREAT);
count = write(fd,"Linux System Programming",5);
if(count != 5)
perror("write");
return 0;
}
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) all of the mentioned
d) none of the mentioned
View Answer
Explanation: This program will write only “Linux” in the file “sanfoundry.txt” because we are writing only 5 bytes with “write” system call.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ls
san san.c
[root@localhost sanfoundry]# ./san
[root@localhost sanfoundry]# ls
san san.c sanfoundry.txt
[root@localhost sanfoundry]# vim sanfoundry.txt
[root@localhost sanfoundry]#
3. What is the output of this program?
#include<stdio.h>
#include<fcntl.h>
int main()
{
int fd, count;
fd = open("san.c",O_RDONLY);
count = write(fd,"Linux",5);
if(count != 5)
perror("write");
return 0;
}
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
View Answer
Explanation: This program will write nothing in the source file “san.c” because we are opening the file in read only mode.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
write: Bad file descriptor
[root@localhost sanfoundry]#
4. What is the output of this program?
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
int main()
{
int fd, count;
char ch, *buff;
buff = (char *)malloc(sizeof(char)*10);
fd = open("san.c",O_RDONLY);
count = read(fd,buff,5);
printf("%d\n",count);
return 0;
}
a) 5
b) 10
c) 0
d) -1
View Answer
Explanation: The “read” system call returns the number of bytes successfully read.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
5
[root@localhost sanfoundry]#
5. In the output of this program, the string “/* Linux */” will be added at the ____ of the source file.
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
int main()
{
int fd;
fd = open("san.c",O_RDWR|O_APPEND);
write(fd,"/* Linux */",11);
return 0;
}
a) end
b) beginning
c) second line
d) third line
View Answer
Explanation: The write system call writes at the end of the file because the file is opened with O_APPEND flag.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
[root@localhost sanfoundry]# vim san.c
[root@localhost sanfoundry]#
6. What is the output of this program?
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
int main()
{
int fd;
char *buff;
buff = (char *)malloc(sizeof(char)*5);
fd = open("sanfoundry.txt",O_RDWR|O_CREAT);
write(fd,"Linux",5);
read(fd,buff,5);
printf("%s\n",buff);
}
a) it will print nothing
b) it will print the string “Linux”
c) segmentation fault
d) none of the mentioned
View Answer
Explanation: We have to use “lseek” system call if we want to read the file from the beginning just after writing into it.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ls
san san.c
[root@localhost sanfoundry]# ./san [root@localhost sanfoundry]# ls
san san.c sanfoundry.txt
[root@localhost sanfoundry]# vim sanfoundry.txt
[root@localhost sanfoundry]#
7. What is the output of this program?
#include<stdio.h>
#include<fcntl.h>
int main()
{
int fd, count;
char ch;
fd = open("sanfoundry.txt",O_RDWR|O_CREAT);
write(fd,"s",1);
lseek(fd,0,SEEK_SET);
write(fd,"d",1);
lseek(fd,0,0);
read(fd,&ch,1);
printf("%c\n",ch);
return 0;
}
a) d
b) s
c) sd
d) none of the mentioned
View Answer
Explanation: Because of “lseek” system call the character “s” is overwritten by character “d” in the file “sanfoundry.txt”.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
d
[root@localhost sanfoundry]#
8. What is the output of this program?
#include<stdio.h>
#include<fcntl.h>
int main()
{
int fd, count;
char ch[10];
fd = open("sanfoundry.txt",O_RDWR|O_CREAT);
write(fd,"linux",5);
lseek(fd,2,SEEK_END);
write(fd,"san",3);
lseek(fd,0,0);
count = read(fd,ch,10);
printf("%s\n",ch);
return 0;
}
a) linux
b) linuxsan
c) linux san
d) none of the mentioned
View Answer
Explanation: The lseek function allows the file offset to be set beyond the end of the file and if the data is latter written this point, subsequent reads of the data in the gap returns NULL.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
linux
[root@localhost sanfoundry]#
9. What is the output of this program?
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
int main()
{
int fd, new_fd;
char *buff;
buff = (char *)malloc(sizeof(char)*8);
fd = open("san.c",O_RDONLY);
new_fd = dup(fd);
close(fd);
read(new_fd,buff,8);
printf("%s\n",buff);
}
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
View Answer
Explanation: The “dup” system creates the a copy of the file descriptor.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
#include
[root@localhost sanfoundry]#
10. What is the output of this program?
#include<stdio.h>
#include<fcntl.h>
int main()
{
int fd, fd2, ret;
fd = open("san.c",O_RDONLY);
ret = close(fd2);
printf("%d\n",ret);
}
a) 0
b) 1
c) -1
d) none of the mentioned
View Answer
Explanation: The “close” system call closes a file descriptor but in the program “fd2” in not a file descriptor. Hence close system call returns -1.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
-1
[root@localhost sanfoundry]#
Sanfoundry Global Education & Learning Series – Linux Administration & Programming.
Here’s the list of Best Books in Linux Commands & Shell Programming.
Here’s the list of Best Books in Linux Kernel, Device-Drivers & System Programming.
To practice all questions on Linux Administration & Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on Linux.
- Buy Information Technology Books
- Apply for Programming Internship
- Apply for Linux Internship
- Buy Linux Books
- Practice Programming MCQs