0% found this document useful (0 votes)
30 views4 pages

OS Lab Exam Advanced Code Questions

The document contains a series of multiple-choice questions related to operating systems, specifically focusing on code outputs, memory allocation strategies, process synchronization, and system-level commands. Each question is followed by four answer options, with the correct answer indicated for each. Topics covered include fork, pthreads, semaphore synchronization, and system commands in C.

Uploaded by

Ananya Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views4 pages

OS Lab Exam Advanced Code Questions

The document contains a series of multiple-choice questions related to operating systems, specifically focusing on code outputs, memory allocation strategies, process synchronization, and system-level commands. Each question is followed by four answer options, with the correct answer indicated for each. Topics covered include fork, pthreads, semaphore synchronization, and system commands in C.

Uploaded by

Ananya Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Operating Systems Lab Exam – Code-

Based MCQs (Advanced)


1. Q1. What is the output of the following code?
#include <stdio.h>
#include <unistd.h>
int main() {
fork();
fork();
printf("X\n");
return 0;
}

 A. X X
 B. X X X
 C. X X X X
 D. X

Answer: C

2. Q2. What will be the output of this pthreads code?


#include <stdio.h>
#include <pthread.h>
void* print(void* arg) {
for (int i = 0; i < 2; i++)
printf("Thread\n");
return NULL;
}
int main() {
pthread_t t1, t2;
pthread_create(&t1, NULL, print, NULL);
pthread_create(&t2, NULL, print, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return 0;
}

 A. Thread printed once


 B. Thread printed twice
 C. Thread printed four times
 D. Compilation error

Answer: C

3. Q3. What will this memory allocation strategy result in?


int memory[5] = {100, 500, 200, 300, 600};
int process = 212; // Best-fit allocation

 A. Allocated at 100
 B. Allocated at 200
 C. Allocated at 300
 D. Allocation fails

Answer: C

4. Q4. Identify the output of this code involving wait and fork:
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
if (fork() == 0) {
printf("Child\n");
} else {
wait(NULL);
printf("Parent\n");
}
return 0;
}

 A. Parent then Child


 B. Child then Parent
 C. Both at same time
 D. Parent only

Answer: B

5. Q5. Consider this code for semaphore synchronization:


#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
sem_t s;
void* critical(void* arg) {
sem_wait(&s);
printf("In critical section\n");
sem_post(&s);
return NULL;
}
int main() {
pthread_t t1, t2;
sem_init(&s, 0, 1);
pthread_create(&t1, NULL, critical, NULL);
pthread_create(&t2, NULL, critical, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
sem_destroy(&s);
return 0;
}

 A. Only one thread prints


 B. Threads print in parallel
 C. Both print, one after the other
 D. Deadlock occurs

Answer: C

6. Q6. What is the output of this system-level command in C?


#include <stdlib.h>
int main() {
system("echo OS > file.txt");
return 0;
}

 A. Nothing
 B. OS is printed to terminal
 C. Creates file.txt with OS
 D. Compilation error

Answer: C

7. Q7. How many times will "Hi" be printed?


#include <stdio.h>
#include <unistd.h>
int main() {
fork();
fork();
fork();
printf("Hi\n");
return 0;
}

 A. 2
 B. 4
 C. 6
 D. 8

Answer: D

You might also like