Computer_Science_211a_Final_Examination_Fall_2003

pdf

School

Western University *

*We aren’t endorsed by this school

Course

2

Subject

Computer Science

Date

Jan 9, 2024

Type

pdf

Pages

17

Uploaded by ProfHippopotamusMaster1442

Report
Computer Science 211a Final Examination Fall 2003 THE UNIVERSITY OF WESTERN ONTARIO LONDON, CANADA COMPUTER SCIENCE 211a FINAL EXAMINATION 6 DECEMBER 2003 3 HOURS Closed book. No calculators or electronic devices of any kind. A sheet of reference notes is provided. 135 marks in total Part I -- Multiple choice, True/False Choose the best answer from the choices given. Circle your answer on the paper, and fill in the answer on the Scantron form. NAME: _______________________________________ STUDENT NUMBER: ___________________________ Question Part I. ________
Part II 30. ________ 31. ________ 32. ________ 33. ________ 34. ________ TOTAL _________ Computer Science 211a Final Examination Fall 2003 1. [1 mark] You can forward your future email by creating a .forward file in your home directory of your Unix account. a) True b) False 2. [1 mark] You may log in to your Unix account remotely. a) True b) False 3. [1 mark] Linux is an operating system that is very similar to the Unix operating system. a) True
b) False 4. [1 mark] Grep is a Unix utility. a) True b) False 5. [1 mark] cd $HOME will change to a subdirectory $HOME of your current directory. a) True b) False 6. [1 mark] It is possible to use a question mark in a Unix file name. a) True b) False 7. [1 mark] A Unix hard link is a pointer to another file, and deleting the original file will invalidate the hard link. a) True b) False 8. [1 mark] You have to compile a shell script before you can run it. a) True b) False
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
9. [1 mark] GDB is a database management system that manages your C source codes. a) True b) False 14. [1 mark] The following is a portion of a program: { int x = 3; x = f(&x); printf("%d", x); } Assume that f() is a well-behaved function. What will be printed out by the printf statement? a) 3 b) x c) The return value of the function call f(&x) d) %d e) The answer is unknown because the function call may change the value of x. 15. [2 marks] The following is a portion of a C program: { int **z, *y, x = 5;
z = &y; y = &x; (*z)++; (*y)++; printf("%d", x); } What will be printed out by the printf statement? a) 5 b) 6 c) 0 d) The result is not predictable. e) %d 16. [1 mark] The following is a portion of a C program: { char a[] = "unix&c"; char *p = a; p = &p[3]; printf("%c", *(p + 1)); } What will be printed by printf? a) n
b) i c) x d) & e) c 17. [2 marks] The following is a portion of a C program: struct book { float price; char title[100]; }; int main() { struct book x, y; x.price = 3.5; strcpy(x.title, "Unix Handbook"); y = x; puts(y.title); return 0; } What is the output of the puts statement? a) Unix Handbook. b) Unix Handbook followed by very many whitespace characters.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
c) The program is wrong because the \0 is missing at the end of the string "Unix Handbook". d) The program is wrong because y.title is not initialized. 18. [2 marks] The following is a portion of a C program: int main() { int x, y; x = 0; y = 3; printf("%d", ((x > y) ? (x++) : (y++))); } What will be printed out? a) 1 b) 2 c) 3 d) 4 e) There is a parenthesis missing at the line of the printf statement. 19. [2 marks] The following is a portion of a C program: { int x, y, count = 0; for (x = 0, y = 5; (x++) <= (--y); count++); printf("%d", count);
} What is the output of the printf statement? a) 1 b) 2 c) 3 d) 4 e) 3 f) 2 23. [2 marks] The following is a portion of a C program: { int ary[5]; char *p, *q; p = (char *)(&ary[1]); q = (char *)(&ary[3]); printf("%d", q - p); } What is the output? a) Program is wrong because you cannot point a char * pointer to an int array element. b) Program is wrong because you cannot subtract a pointer from another. c) Unpredictable because ary is not initialized.
d) The value depends on sizeof(int) for the computer platform being used. 24. [1 mark] A Bourne shell script can be invoked with at most 9 positional parameters. a) True b) False 25. [1 mark] The shell script variable $# has its value decreased by 1 each time the shell script program executes the shift command. a) True b) False 26. [1 mark] The shell script variable $* holds a list of all the files in the current working directory. a) True b) False 27. [1 mark] All variables inside a shell script program are stored as strings. a) True b) False 28. [1 mark] A colon (":") on a line by itself is a Unix command that does nothing.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
a) True b) False 29. [2 marks] The following Bourne shell script will fail to run properly because of a syntax error: #!/bin/sh word="" # word is the empty string if test -z $word then echo "Empty string" else echo "Non-empty string" fi a) True b) False h) [3 marks] Remove a subdirectory of the current working directory and all the files in it. The subdirectory's name is junk. And you are required to run the command in the background. Answer: . i) [4 marks] Write only the second line of filex to filey. Answer: .
31. [10 marks] revstr is supposed to be a function that reverses a null character-ended string that is stored in a char array. The parameter str points to the char array that stores the string. The function will change the content of the char array to reverse the string. The return value of the function is a pointer that is equal to the parameter str. The main function calls revstr and then prints the reversed string to the standard output. However, there are many syntax errors and bugs that may cause runtime errors and/or make the program perform differently than intended. Try to fix all of the errors by writing the correct statements in the corresponding lines at the right. Do not fix anything that is correct, even if it is ugly coding. #include <stdio.h> __________________________________________ #include <string.h> __________________________________________ char * revstr(char * str) { __________________________________________ int n, len; __________________________________________ char ch; __________________________________________ len = strlen(&str[0]);
__________________________________________ for (n = 0; n < len; n++) { __________________________________________ ch = (*str) + n; __________________________________________ strcpy(&str[n], &str[len - n]); __________________________________________ strcpy(&str[n], &str[len - n]); __________________________________________ str[len - n] = ch; __________________________________________ } __________________________________________ return str[0]; __________________________________________ } __________________________________________ int main() { __________________________________________ char * str, *p; __________________________________________
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
return _________________; __________________________________________ } b) [7 marks] Finish the program by filling in the blanks. The program reads all integers provided as command line parameters, sums them up, and prints out the sum value. (Hint: argv[0] is the program name and should be excluded from the integer list.) #include ____________________________ #include ____________________________ int main(int argc, char * argv[]) { int n, x, sum; for (__________________________________________________) { x = atoi(____________________________); sum = ____________________________; } printf (____________________________); return 0; }
c) [9 marks] Write a function that finds the second-largest int value in an array. The return value should be a pointer to that int element. The array contains size elements, where size is no less than 2. int * secondLargest( int * ary, int size ) { __________________________________________ } 33. [27 marks] This question makes use of the LinkedList ADT used with Assignment 5. The function prototypes for LinkedList are included on the sheet of reference notes handed out with the exam. a) [6 marks] Recall that the function initializeList() requires that a LinkedList actually exist in order for it to be initialized. Write a new function of type void, called createList, that actually allocates a new LinkedList dynamically and initializes it to the empty list. The new list must be returned to the calling module through the function’s parameter list. (You must supply the parameter list.) void createList ( ) { __________________________________________ } b) [4 marks] Write a function called compareList that compares the two LinkedLists pointed to by parameters a and b, and that returns zero if the two lists are of the same size, a negative value if a is shorter than b, and a value greater than 0 if a is longer than b.
int compareList (LinkedList *a, LinkedList *b) { __________________________________________ } e) [10 marks] Suppose that a binary search tree ADT uses your TreeNodes from part d) in order to build trees. Complete the function addToTree, whose job is to insert an element into a binary search tree. (The function header for addToTree is given below.) The basic algorithm for inserting into a binary search tree is: If the tree t is empty Then build a new node containing LinkedList ll and return a reference to this node Else If ll is “smaller” than the LinkedList inside t Then insert ll into the left subtree of t recursively Else insert ll into the right subtree of t recursively Return a reference to the tree root You may use your function from part b) in your solution. TreeNode * addToTree( TreeNode *t, LinkedList *ll ) { __________________________________________ }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
34. [18 marks] a) [4 marks] Describe in words what the following Bourne shell script does: #!/bin/sh for k in * do if [ -f $k ] then n="`echo $k | grep the `" if [ -n "$n" ] then echo $k fi fi done b) [4 marks] Describe in words what the following Bourne shell script does: for k in $* do
if grep $k the > /dev/null then echo $k fi done [Rough work only This page will not be marked]