100% found this document useful (1 vote)
387 views6 pages

Sol Midterm 1

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

University of Windsor

School of Computer Science Course 03-60-256 Instructor B. Boufama Final Exam Worth 15% Duration 75 minutes Student Name: Student Number:

Important: Closed book and no calculators; Answer directly on these test sheets

Short questions: 3 marks each


1. Unix philosophy includes the following : A complex problem should be solved, if possible, by combining multiple existing utilities. What is the name of the mechanism used by Unix to achieve this goal? Your answer: Pipes 2. What is the data type of a user ID in Unix? Your answer: integer 3. A shell external command is an executable program that is usually stored in /usr/bin/ Your answer (Yes/No) : Yes 4. A shell is a programming language similar to C used for specic applications. Your answer(Yes or No) : No 5. The standard error stream(stderr) of the standard I/O library can be redirected to a le using >. Your answer (Yes/No) : No 6. A process ID is usually a number equal to the user ID. Your answer(Yes or No) : No 7. fork() is system call that causes the caller process to die while a new one will start. Your answer(Yes or No) : No 1

8. A Unix le descriptor is a nonnegative integer that can be used by any Input/Output functions, such as fgets() and fwrite(), to access the le. Your answer (Yes/No) : No 9. When doing Input/Output operations, the goal of buering is to minimize the number of les kept open at any time. Your answer(Yes or No) : No 10. When using the standard I/O library, interactive devices, such as the keyboard, are typically fully buered Your answer(Yes or No) : No 11. EOF is a special ASCII character that is always found at the end of every le in the disk. Your answer(Yes or No) : No 12. The functions fgets() and fputs() are the best choice for binary input/output operations. Your answer(Yes or No) : No

Tracing: 4 marks each


1. The C-Shell script more $1 | head -$2 | tail -1 takes two arguments, a le name and an integer i. In a few words, what does it do? Hints: 1) head -n delivers the rst n lines of a le 2) tail -n delivers the last n lines of a le Your answer : Displays line i of the input le 2. What will be printed on the screen by this function? void fff(FILE *f1, int data[]){ fwrite(data, 4, 10, f1); rewind(f1); printf("Value= %ld\n", ftell(f1)); } Your answer : Value= 0 2

3. What will be printed on the screen by this function? void fff(FILE *f1, int data[]){ fwrite(data, 4, 10, f1); rewind(f1); fseek(f1, 0, SEEK_END); printf("Value= %ld\n", ftell(f1)); } Your answer : Value= 40 4. When running the program below, how many seconds will pass before you can see something on the screen? int main(int argc, char *argv[]){ char ar[10] ="Windsor\n"; int i=0; while(1){ fputc(ar[i], stdout); i = (i+1) % 10; // i should not exceed 9 sleep(1); // sleep one second } } Your answer : 7 (8 will also be accepted as valid answer) 5. Assuming that the screen is buered with a 100-byte buer. When running the program below, how many seconds will pass before you can see something on the screen? int main(int argc, char *argv[]){ while(1){ fputc(0, stdout); sleep(1); // sleep one second } } Your answer : 99 (100 will also be accepted as valid answer) 3

6. Assuming that the screen is buered with a 100-byte buer. when running the program below, how many seconds will pass before you can see something on the screen? int main(int argc, char *argv[]){ int i=1; while(1){ if(i==50) fputc(\n, stdout); else fputc(0, stdout); i++; sleep(1); // sleep one second } } Your answer : 49 (50 will also be accepted as valid answer) 7. Consider the follwoing function: long int fff(FILE *fp){ char ch; long int i=0; do{ ch=fgetc(fp); i++; } while(ch != EOF); i--; // To avoid counting EOF return(i); } What should we change to make this function always yield the correct result? (Assume that there are no I/O error) Your answer : change the type of ch to int 8. What will be printed by the following: echo Windsor | tr -d [:digit:] Your answer : Windsor 4

9. What will be printed by the following: echo Feb. 18 2003 | tr -d [:digit:] Your answer : Feb. 10. In a few words, what does the following script do? set file = $< set name=$file:r mv $file "$name.old" Your answer : Changes a lename extension to .old 11. The utility ls lists les and subdirectories. Suppose we have 16 les and 9 subdirectories in our current directory. What will be printed if we type the following: ls -F | grep -v / | wc -w Your answer : 16 12. In a few words, what does the following script do? mkdir archive foreach file (ls) if (-f $file && $file:e == $1) then mv $file archive/. endif end Your answer : move all regular les with a given extension to the subdirectory archive

Coding(16 marks)

Write a short C-shell script to append the user-name to the root names of all text les (*.txt) in the current directory. For example, if the current directory of the user smith12 has two text les, bb.txt and dd.txt, their names would be changed to bb smith12.txt and dd smith12.txt. Hints: you might use Unix command whoami to get the user-name

#!/bin/csh set userName = whoami foreach file ( ls *.txt) set name = $file:r mv $file $name"_"$userName.txt end

You might also like