Linux Interview Questions and Answers on File Management for Freshers

https://www.computersprofessor.com/2018/08/linux-interview-questions-and-answers_5.html
1. Each process has unique
a) fd table
b) file table
c) inode table
d) data block table
Answer: a
2. File descriptor table indexes which kernel structure?
a) struct file
b) strruct fs_struct
c) files_struct
d) struct inode
Answer: a
3. What is the default number of files open per user process?
a) 0
b) 1
c) 2
d) 3
Answer: d
4. The file system information is stored in
a) Boot block
b) Super Block
c) Inode Table
d) Data Block
Answer: b
5. Switch table is used by
a) device special file
b) directory file
c) fifo
d) link file
Answer: a
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
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
8. printf() uses which system call
a) open
b) read
c) write
d) close
Answer: c
9. read() system call on success returns
a) 0
b) -1
c) number of character
d) none
Answer: c
10. Which system call is used to create a hard link?
a) hardlink
b) link
c) symlink
d) ln
Answer: b
11. namei() is
a) ANSI C library function
b) C library function
c) System call
d) kernel routine
Answer: d
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
13. 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
14. 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
15. 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(“World\n”); return 0; }
a) Hello
b) HelloWorld
c) World
d) None
b) HelloWorld
c) World
d) None
Answer: b
16. 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
a) 5
b) 1024
c) 1029
d) 1034
Answer: b
17. Code snippets
str1=”45678\n” str2=”123\n” 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)
b) 123(newline)8(newline)
c) 123(newline)78(newline)
d) 45678(newline)123(newline)
Answer: b
18. Code snippets
str1=”45678\n” str2=”123\n” 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)
b) 123(newline)8(newline)
c) 123(newline)78(newline)
d) 45678(newline)123(newline)
Answer: d
19. 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
b) 1034
c) 2034
d) 2054
Answer: b