0% found this document useful (0 votes)
43 views8 pages

Sample Test 02

Uploaded by

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

Sample Test 02

Uploaded by

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

Course COMP-8567 Student Name:

Instructor B. Boufama Student Number:


Test 02 Section:

Closed book and no calculators; Answer directly on these sheets;


Do not forget to write your name and your student ID

There are 25 questions, 4 marks each

1. What will be printed by the program below?


int main(){
int a[5]={5, 0, 10, 15, 25}
int *pt=a+2;
printf("%d %d %d\n", *(pt+1), *(a+1), pt-a);
}
Your answer :

2. What will be printed by the program below?


int main(){
void *pt;
pt = malloc(2*sizeof(double));
((double *)pt)[0] = 3.5;
((double *)pt)[1] = 1;
printf("%lf %lf\n", ((double *)pt)[0], ((double *)pt)[1]);
}
Your answer :

3. What will be printed by the program below?


void func1(int x){
printf("%d\n", x*x + 2*x -10);
}
void func2(int x){
printf("%d\n", 3*x*x - 3*x +1);
}
int main(){
void (*pt)(int);
pt=func1;
pt(1);
pt=func2;
pt(1);
}

Your answer :

1
4. What will be printed by the program below?
int *func(){
static int ar[3]={1, 5, 10};
return(ar);
}
int main(){
int *pt=func();
printf("%d %d\n", *pt, pt[2]);
}
Your answer :
5. Running a program under Bash resulted in a process P, with its pid=2000. Let P create a
process P1 and let P1 create a process P2. What will be the pgids of P1 and P2?
Your answer :
pgid(P1)= pgid(P2)=
6. What will be printed by the program below?
int main(int argc, char *argv[]){
int buffer[4] = {0, 1, 5, 3};
int *ptr;
for(ptr=buffer+3; ptr >= buffer; ptr--)
printf("%d ", *ptr);
}
Your answer :
7. In a single sentence, what does the shell-script below do?
AA=1000000000
for BB in ‘ls *.$1‘; do
CC=‘cat $BB | wc -c‘
if [[ $AA -ge $CC ]]; then # -ge is great or equal
AA=$CC ; DD=$BB
fi
done
echo $DD
Your answer :
8. What will be printed by the following bash line?
sort file.txt | uniq | wc -w, assuming file.txt conatins the following lines:
Prince
King
Queen
Princess
King
Hints: sort alphabatically sorts lines of text files and uniq omits repeated lines, by keep-
ing a single occurence.
Your answer :

2
9. In a single sentence, what does the program below do?

int main(int argc, char *argv[]){


DIR *dp;
struct dirent *dirp;
int n=0;

dp = opendir("./");
while( (dirp=readdir(dp))!=NULL)
if(dirp->d_type == DT_DIR)
n++;
printf("%d\n", n)
closedir(dp);
}

Your answer :

10. What will appear on the screen after 6 seconds, when the program below executes:

void action(){}
int main(int argc, char *argv[]){
int pid;
signal(SIGUSR1, action);
pid=fork();
ProcessJob(pid);
}
void ProcessJob(int pid){
while(1)
if(pid){
printf("Parent working\n");
sleep(1);
pause();
kill(pid, SIGUSR1);
}else{
pause();
printf("Child working\n");
sleep(1);
kill(getppid(), SIGUSR1);
}
}

Your answer :

3
11. Assuming we cannot use kill to terminate this script, explain in a single sentence how can
we terminate it?

A=0
trap ’
echo Nice try
’ TSTP # TSTP is CTR-Z
trap ’
echo Nice try
let "A = $A + 1"
if [[ $A -eq 5 ]]; then
exit 0
fi
’ INT #INT is CTR-C

while : ; do
echo Still alive
sleep 3
done

Your answer :

12. Correct the following bash script so that it will work properly.

dir=/bin/
for BB in ls dir ; do
if [ -f $BB ] ; then
chmod +x BB
fi
done

13. What will appear on the screen when the program below executes:
main(){
int n, status;

n = 10 + 10*(!fork());

wait(&status);
printf("%d\n", n);
exit(0);
}
Your answer :

4
14. How many "Hi"s will this program print?

main(){
for(int i=1; i <=2; i++)
fork();
if(!fork())
printf("Hi\n");
}

Your answer :

15. What will be printed by the following program?

int main(int argc, char *argv[]){


int n, m=-1000, fd, i;
char data[10][1000];

fd=open(argv[1], O_CREAT|O_WRONLY|O_TRUNC);
for(i=0; i< 10; i++)
write(fd, data[i], 1000);
n=lseek(fd, m, SEEK_CUR);
printf("Value= %d\n", n);
}

Your answer : value=

16. What will appear on the screen when the following program runs?

void action(){}
int main(int argc, char *argv[]){
alarm(1);
if(!fork())
signal(SIGALRM, action);

sleep(2);
printf("Friday\n");
exit(0);
}

Your answer :

5
17. What will be printed by the following program

void myHandler(){
fprintf(stderr, " Night ");
exit(50);
}
int main(){
int n;
signal(SIGALRM, myHandler);
if(!fork()){
alarm(1);
fprintf(stderr, "Morning ");
sleep(2);
}
sleep(1);
fprintf(stderr, " Afternoon ");
wait(&n);
fprintf(stderr, "%d\n", WEXITSTATUS(n));
}
Hint: WEXITSTATUS(n) extracts the exit status from variable n
Your answer :

18. What will be printed by the following program


void action(){
fprintf(stderr, "And ");
exit(0);
}
int main(){

if(fork()){
signal(SIGUSR1, action);
fprintf(stderr, "Butter ");
pause();
}else{
sleep(1);
kill(getppid(), SIGUSR1);
exit(0);
}
fprintf(stderr, "Bread ");
}
Your answer :

6
19. What will be printed by the following program
void action(){
fprintf(stderr, "South ");
exit(0);
}
int main(){
if(fork()){
signal(16, action);
pause();
fprintf(stderr, "Ottawa ");
}else{
fprintf(stderr, "Windsor ");
sleep(1);
kill(getppid(), 16);
}
sleep(2);
fprintf(stderr, "%d\n", getppid());
}
Your answer :

20. What will appear on the screen when the following program runs?
int main(int argc, char *argv[]){
if (fork())
printf("Windsor\n");
if(!fork())
pause();
printf("Bye\n");
exit(0);
}
Your answer :
21. In a few words, what does the following Bash script do?
#!/bin/bash
cat $1 | head -n $2 | tail -n 1
Your answer :

Hints:
1) head -n d delivers the first d lines of a file
2) tail -n d delivers the last d lines of a file

7
22. What will appear on the screen when the following program runs?
int main(int argc, char *argv[]){
printf("Hello World\n");
alarm(1)
sleep(2);
printf("Bye\n");
exit(0);
}
Your answer :
23. Consider the program
int main(int argc, char *argv[]){
int fd;
char buffer1[10] = "One ";
char buffer2[10] = "Two ";

fd = open("file.txt", O_WRONLY|O_CREAT|O_TRUNC);
write(fd, buffer1, strlen(buffer1));
write(STDOUT_FILENO, buffer2, strlen(buffer2));
dup2(fd, STDOUT_FILENO); // STDOUT_FILENO is the screen
write(fd, buffer1, strlen(buffer1));
write(STDOUT_FILENO, buffer2, strlen(buffer2));
close(fd);
}
• What will appear on the screen?
Your answer :
• What will the file file.txt contain?
Your answer :

24. What will be printed by the program below?


void *Func(void *arg){
printf("%d\n", *(int *)arg);
*(int *) arg = 20;
pthread_exit(arg);
}
int main(){
pthread_t tid;
int *tRet=malloc(sizeof(int));
*tRet=100;
pthread_create(&tid, NULL, Func, tRet);
pthread_join(tid, (void **)&tRet);
printf("%d\n", *tRet);
exit(0);
}
Your answer :

You might also like