0% found this document useful (0 votes)
868 views

OS LAB MCQ Modified

This document contains multiple choice questions about operating system concepts like processes, threads, synchronization, and file I/O. Some key points covered are: 1) When a process makes a system call, its mode changes from user mode to kernel mode. 2) Common system calls include open(), read(), write(), close(), etc. which are used to perform operations like opening, reading, writing and closing files. 3) The fork() system call is used to create child processes from a parent process. 4) Functions like pthread_create() are used to create new threads within a process. 5) Commands like cat, chmod, mv, etc. are used to display

Uploaded by

Mridul Jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
868 views

OS LAB MCQ Modified

This document contains multiple choice questions about operating system concepts like processes, threads, synchronization, and file I/O. Some key points covered are: 1) When a process makes a system call, its mode changes from user mode to kernel mode. 2) Common system calls include open(), read(), write(), close(), etc. which are used to perform operations like opening, reading, writing and closing files. 3) The fork() system call is used to create child processes from a parent process. 4) Functions like pthread_create() are used to create new threads within a process. 5) Commands like cat, chmod, mv, etc. are used to display

Uploaded by

Mridul Jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

1. A system call is a routine built into the kernel and performs a basic function.

A. True

B. False

C. May be

2. When we execute a C program, CPU runs in ____ mode.

A. user

B. kernel

C. supervisory

D. system

3. In ____ mode, the kernel runs on behalf of the user.

A. User

B. kernel

C. supervisory

4. All UNIX and LINUX systems have one thing in common which is ____

A. set of system calls

B. set of commands

C. set of instructions

D. set of text editors

5. For reading input, which of the following system call is used?

A. Write

B. Rd

C. Read

6. Which of the following system call is used for opening or creating a file?

A. Read
B. write

C. open

D. close

7. There are ___ modes of opening a file.

A. 4

B. 3

C. 2

8. Which of the following mode is used for opening a file in both reading and writing?

A. O_RDONLY

B. O_WRONLY

C. O_RDWR

9. open system call returns the file descriptor as ___

A. int

B. float

C. char

D. double

10. Which of the following system call is used for closing a file?

A. Open

B. Lseek

C. Close
D. create

11. close system call returns ____

A. 0

B. 1

C. May be
D. 0 or -1

12. write system call returns -1 when ___________

A. if disk fills up while write is in progress

B. when file doesn’t exist

C. if the file size exceeds the system’s limit


D. if disk fills up while write is in progress and if the file size exceeds

13. ____ system call is used for writing to a file.

A. read

B. write

C. close

D. seek

14. ____ system call is used for positioning the offset pointer.

A. read

B. write

C. open
D. lseek

15. Which of the following offset is used with lseek system call to set the offset pointer to the end of the
file?

A. SEEK_SET

B. SEEK_END

C. SEEK_CUR

D. SEEK_CR
16. The read system calls return 0 when ………..

A. it reaches middle of file

B. it reaches end of the file

C. file is empty
D. none of these

17. When a process makes a system call ,its mode changes from………

A. user to kernel

B. kernel to user

C. restricted to unrestricted

D. unrestricted to restricted

18. The cat command is used to

A. print a file

B. display a file

C. capture a file

D. copy a file

19. When the read system call encounters EOF ,it returns

A. some positive integer

B. some negative integer

C. 0

D. -1

20. Services provided by kernel are………

A. controlling execution of process

B. scheduling processes

C. allocating main memory for an executing process

D. all of these

21. The shell acts as an interface between the ………..

A. user and kernel

B. user and operating system

C. kernel and operating system


D. none of these

22. When a process executes a system call, the execution mode of the process changes from
………..

A. kernel mode to user mode

B. user mode to kernel mode

C. user mode to operating system mode

D. none of these

1. Consider a scenario in which one parent process has single child process created by using

pid = fork();

Then the task to be performed only by the child process should be written as:

a) if(pid < 0) { //specify the task}

b) if(pid > 0) { //specify the task}

c) if(pid = = 0) { //specify the task}

d) if(pid ! 0) { //specify the task}

ans: c

2. A file named "os.txt" already exists in your system. Suppose the text "An operating system
(OS) is system software that manages computer hardware, software resources, and provides
services for computer programs" is written into it. Open this file for reading purpose and
truncate the size to zero.

a. open("os.txt", O_RDONLY | O_TRUNC, 0);

b. open("os.txt", O_TRUNC, 0);

c) open("os.txt", O_READ | O_TRUNC, 0);

d) open("os.txt", O_CREAT | O_TRUNC, 0);

ans: a
3. Consider a file “F1” having some content already written into it. In order to reposition at
the 10th position from the start which of the code will be used.

a) n=open("F1",O_RDONLY);

lseek(n,10,SEEK_BEG);

b) n=open("F1");

lseek(n,10,SEEK_SET);

c) open("F1",O_RDWR);

lseek(n,10,SEEK_START);

d) n=open("F1",O_RDWR);

lseek(n,10,SEEK_SET);

Ans: d

4. Create a new thread "th_new". Newly created thread executes a non parameterised function
named "function_new".

a) pthread_create(&th_new, NULL, function_new, NULL);

b) pthread_create(pthread_t, function_new, NULL, NULL);

c) pthread_create(th_new, NULL, NULL, function_new);

d) pthread_create(*th_new, NULL, function_new, NULL);

ans: a

5. Consider a file named "file" having some content. Code to read 13 characters from it and
print all the read characters on screen will be

a.)

char buffer[13];

fd=open("file",O_RDWR);

read(fd,buffer,13);

write(1,buffer,13);

(b)
char buffer[13];

fd=open("file",O_RDONLY);

read(0,buffer,13);

write(1,buffer,13);

(c)

char buffer[13];

fd=open("file",O_RDWR);

read(0,buffer,13);

write(1,buffer,13);

(d)

char buffer[13];

fd=open("file",O_RDWR);

read(fd,buffer,13);

write(fd,buffer,13);

Ans: A

MCQ questions

1. Consider the following code


int main()
{
fork();
printf(“Hello World\n”);
fork();
printf(“Hello World\n”);
}

How many times Hello World will be printed?

a) 4
b) 6
c) 8
d) 5

Ans- b
2. Consider the following code
int a=5
int main()
{
int p;
p=fork();
if(p==0){
a+=15;
return 0;
}
else {
wait(NULL);
printf("Parent:value is=%d",a)
return 0;
}
}
What will be the value of ‘a’ printed in parent process?

a) 15
b) 20
c) 5
d) Parent will not execute

Ans-c

3. Consider the following code


int main()
{
int fd,n;
char buf[40];
fd=open("seeking",O_RDWR);
n=lseek(fd,10,SEEK_SET);
printf("The cursor is at position %d\n",n);
}
What is the value printed for ‘n’ in the above code?
a) 0
b) 11
c) 10
d) Nothing will be printed

Ans-c

4. A file named "test.txt" already exists in your system. Which of the following syntax will be
used to open the file for reading purpose?
a) open("test.txt", O_RDONLY);
b) open("test.txt", O_CREAT|O_RDONLY, 0664);
c) open("test.txt", O_READ);
d) open("test.txt", O_CREATE|O_RDWR);

Ans-a

5. Consider a file named "test" with some content in it. Which of the following options will be
used to read 10 characters from the file and print all the read characters on screen
a.)
char buf[20];
fd=open("test",O_RDWR);
read(fd,buf,10);
write(1,buf,10);

(b)
char buf[20];
fd=open("test",O_RDONLY);
read(0,buf,10);
write(1,buf,10);

(c)
char buf[20];
fd=open("test",O_RDWR);
read(0,buf,10);
write(fd,buf,10);

(d)
char buf[20];
fd=open("test",O_RDWR);
read(fd,buf,10);
write(fd,buf,10);

6. Consider the code


int main()
{
char buf[30];
int n;
n=read(0,buf,20);
printf("The value of n is %d\n",n);
}

After running the above code, the input to the code is given as “CSE325 CA”. What will be
the value of ‘n’ printed?

a) 9
b) 20
c) 30
d) 10
Ans-d

7. What will be output of the following code?


int main()
{
int p;
p=fork();
if(p==0)
{
printf("The Child\n”);
}
else
{
printf(The Parent\n");
}
}
a) The Child
The Parent
b) The Parent
The Child
c) The Child
d) The process can run in any order.

Ans-d

8. Assume there is file named as “file.txt”. Currently the permission of a file are: read, write
execute for user, read for group and no permissions for others. What will be used to set the
permissions to read and execute for user, read and write for group and read for others.
a) chmod 764 file.txt
b) chmod 564 file.txt
c) chmod 664 file.txt
d) chmod 561 file.txt

Ans -b

9. Assume there is a file named as “test.txt”. If you want to rename a file to demo.txt. which of
the following command will be used.
a) rename test.txt demo.txt
b) rn test.txt demo.txt
c) mv test.txt demo.txt
d) There is no command to rename a file.

Ans-c

10. Consider the following code


int main()
{
fork();
fork();
fork();
}
How many child process are created using above code
a) 8
b) 7
c) 3
d) 10

Ans-b

11. Which of the following is true for the following code?


int main()
{
int p;
p=fork();
if(p==0)
{
printf("The Child Process with ID %d\n”, getpid());
printf(“My parent is having ID %d\n, getppid());
}
else
{
sleep(5);
printf(The Parent Process with ID %d\n", getpid());
printf(“My child is having ID %d\n”, p)
}
}

a) This will create a zombie process


b) This will create an orphan process
c) This is normal execution of child and parent.
d) None of them

Ans-a

12. Which of the following is true with respect to process creation?


a) The child never duplicates the calling process
b) The child will run the code of another program using execl() function.
c) The child process will always run and complete before parent process
d) When fork returns zero, then it is considered as a parent process

Ans-b

1. Consider a program named “read.c”. The command

$gcc -o read read.c


will create an executable file named as

1. read.out

2. a,out

3. read

4. read.c

Ans: 3

2. The following code

int main( )

pid_t p, q;

p=fork();

if(p = = 0)

q=fork();

1. Creates two child process of the parent process

2. Creates a hierarchy of three processes

3. Creates a hierarchy of 4 processes

4. There are a total of 4 processes

Ans: 2

3. Consider a scenario in which one parent process has single child process created by using

pid = fork();

Then the task to be performed only by the parent process should be written as:
a) if(pid < 0) { //specify the task}

b) if(pid > 0) { //specify the task}

c) if(pid = = 0) { //specify the task}

d) if(pid ! 0) { //specify the task}

ans: b

4. What does the write() system call returns on success?

a) data that it writes

b) number of bytes actually written

c) -1

d) it returns nothing

Ans: b

5. What does the open() system call returns on success?

a) data that it writes

b) name of the file

c) file descriptor

d) it returns nothing

Ans: c

6. Suppose a file “f1” has the following contents:

1234567890

What will be the contents of the file after this code is executed?

int main()

char b[10];

int n;
n=open(“f1”,O_RDWR);

read(n,b,3);

write(n,”OK”,2);

a) OK
b) OK34567890
c) 123OK67890
d) 1234567890

Ans: c

7. Suppose a file “f1” has the following contents:

1234567890

What will be the contents of the file after this code is executed?

int main()

char b[10];

int n;

n=open(“f1”,O_RDONLY);

read(n,b,3);

write(n,”OK”,2);

a) OK
b) OK34567890
c) 123OK67890
d) 1234567890

Ans: d

8. Suppose a file “f1” has the following contents:

1234567890

What will be the contents of the file after this code is executed?

int main()
{

char b[10];

int n;

n=open(“f1”,O_RDWR);

read(n,b,3);

lseek(n,2,SEEK_CUR);

write(n,b,2);

a) 123
b) 1234567890
c) 1234512390
d) 1234512890

Ans: d

9. A file named "os.txt" already exists in your system. Suppose the text "An operating system
(OS) is” is written into it. The code to open the file for writing “system software” in the end
is

a. int n;

n=open("os.txt", O_RDWR | O_APPEND);

write(n,”system software”,15);

b. int n;

n=open("os.txt", O_RDWR);

write(n,”system software”,15);

c. int n;

n=open("os.txt", O_RDWR | O_APPEND);

write(1,”system software”,15);

d. int n;

n=open("os.txt", O_RDWR);

write(1,”system software”,15);
Ans: a

10. Consider a file “F1” having some content already written into it. In order to reposition
the cursor at the 10th position from the beginning which of the following code will be used.

a) n=open("F1",O_RDONLY);

lseek(n,10,SEEK_BEG);

b) n=open("F1");

lseek(n,10,SEEK_SET);

c) open("F1",O_RDWR);

lseek(n,10,SEEK_START);

d) n=open("F1",O_RDWR);

lseek(n,10,SEEK_SET);

Ans: d

11. Consider a file “F1” having the content “Operating” already written into it. In order to
print “ing” on the screen which of the following code will be used.

a) int n;

char buff[10];

n=open(“F1”, O_RDWR);

read(n,buff,3);

write(1,buff,3);

b) int n;

char buff[10];

n=open(“F1”, O_RDWR);

lseek(n,-4,SEEK_END);

read(n,buff,3);

write(1,buff,3);

c) int n;
char buff[10];

n=open(“F1”, O_RDWR);

lseek(n,-4,SEEK_END);

write(1,buff,3);

d) int n;

char buff[10];

n=open(“F1”, O_RDWR);

lseek(n,4,SEEK_SET);

read(n,buff,3);

write(1,buff,3);

Ans: b

12. Consider a file named "file" having some content. Code to read 13 characters from it and
print all the read characters on screen will be

a.)

char buffer[13];

fd=open("file",O_RDWR);

read(fd,buffer,13);

write(1,buffer,13);

(b)

char buffer[13];

fd=open("file",O_RDONLY);

read(0,buffer,13);

write(1,buffer,13);

(c)
char buffer[13];

fd=open("file",O_RDWR);

read(0,buffer,13);

write(1,buffer,13);

(d)

char buffer[13];

fd=open("file",O_RDWR);

read(fd,buffer,13);

write(fd,buffer,13);

Ans: A

13. The following code will result in a :

int main()

pid_t p;

p=fork();

if(p==0)

printf(“Child process”);

else

sleep(3);

printf("I am parent having PID %d\n",getpid());

}
1. zombie process

2. orphan process

3. no child process

4. two child processes

Ans: 1

14. The system call used to make the parent process wait for the child process to finish is

1. wait

2. fork

3. system

4. exec

Ans: 1

15. Out the following which header file(s) are required for the fork() system call

1. sys/types.h

2. unistd.h

3. stdio.h

a) only (1)

b) only (2)

c) both (1) and (2)

d) (1), (2) and (3)

Ans: c

16. Consider a directory D1 having a file F1. The command to delete D1 will be

1. rm D1

2. rm -r D1

3. rmdir D1
4. rmdir -r D1

Ans: b

17. The command to exit the nano editor is

1. Ctrl+x

2. Shift+x

3. Ctrl+q

4. Shift+q

Ans: 1

18. The use of sleep() function within a process makes the

1. process to sleep for specified number of seconds

2. process to sleep for specified number of minutes

3. process to terminate after specified number of seconds

4. process to terminate after specified number of seconds

Ans: 1

19. The system call used to reposition the curson within the file is

1. write

2. read

3. open

4. lseek

Ans: 4

20. What will be the output of the following code

int main()

{
printf(“First Output”);

execl(“\bn\ps”,”ps”,NULL);

printf(“Second Output”):

a) First Output followed by the list of running processes, followed by Second Output

b) First Output followed by the list of running processes

c) List of running processes, followed by Second Output

d) List of running processes

Ans: b

Q21. What will be the value of the variable “f1”. The contents of the file “seeking” are

1234567890abcdefghijx1x2x3x4x5

int n,f,f1;

f=open("seeking",O_RDWR);//1234567890abcdefghijx1x2x3x4x5

f1=lseek(f,10,SEEK_SET);

printf("Pointer is at %d position\n",f1);

a) 10

b) 9

c) 11

d) 1

Ans: a

Q. The following code will create how many child processes

int main()

{
pid_t p,q;

p=fork();

if(p==0)

fork();

else

fork();

a) 3

b) 4

c) 7

d) 8

Ans: a

1. What is the output of this program?


<header files>
int main()
{
pid_t p;
p = fork();
printf("%d\n",p);
return 0;
}
a) it will print "0"
b) it will print the PID of the child process
c) it will print "0" & the PID of the child process
d) none of the mentioned

Ans: c

2. Which of the following code synipt is correct in order to create 7 child processes of one
parent process
a) int main()
{
fork();
printf("Child Process\n");
return 0;
}
b) int main()
{
fork();
fork();
printf("Child Process\n");
int func1()
{
fork();
fork();
}
return 0;
}
c) int main()
{
fork();
fork();
printf("Child Process\n");
return 0;
}
d) int main()
{
fork();
fork();
fork();
printf("Child Process\n");
return 0;
}
Ans: d

3. Which of the following is correct to read 13 characters from a file named first.txt and write
into another non existing file final.txt.
a) <header files>
<main function>
int first, final, n;
char b[50];
first= open("first.txt", O_RDONLY);
n=read(first,buf,13);
final=open("final.txt", O_CREAT | O_WRONLY);
write(final,buf,n);

b) <header files>
<main function>
int first, final, n;
char b[50];
first= open("first.txt", O_RDONLY);
n=read(first,b,13);
final=open("final.txt", O_CREAT | O_WRONLY);
write(final,b,n);

c) <header files>
<main function>
int first, final, n;
char b[50];
first= open("first.txt", O_RDONLY);
n=read(first,b,13);
final=open("final.txt", O_WRONLY);
write(final,buf,n);
d) <header files>
<main function>
int first, final, n;
char b[50];
first= open("first.txt", O_RDONLY);
n=read(first,b,13);
final=open("final.txt", O_CREAT,O_WRONLY);
write(final,b,n);
Ans: b

4. What is the output of this program?


<header files>
int main()
{
fork();
printf("CSE325 CA1\n");
return 0;
}
a) CSE325 CA1 will be printed one time
b) the string "CSE325 CA1" will be printed twice
c) CSE325 CA1 will be printed thrice
d) none of the mentioned
Ans: b

5. Total how many times "CA1CSE325" will be printed?


<header files>
int main()
{
fork();fork();printf("CA1CSE325");
}
a) only 1 time
b) 2 times
c) 3 times
d) 4 times

Ans: d

You might also like