Linux Interview Questions and Answers – File Management 2

1.

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 these

Answer: Option 'C'

rw-rw-rw- rw——-

2.

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: Option 'D'

100010 30

3.

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

Answer: Option 'B'

HelloWorld

4.

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

Answer: Option 'B'

1024

5.

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)

Answer: Option 'B'

123(newline)8(newline)

6.

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)

Answer: Option 'D'

45678(newline)123(newline)

7.

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.) 1034
   B.) 1024
   C.) 2034
   D.) 2054

Answer: Option 'A'

1034


Linux Interview Questions and Answers – File Management 2 Download Pdf

Recent Posts