Sol Midterm 1
Sol Midterm 1
Sol Midterm 1
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
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
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