CS计算机代考程序代写 compiler CS211a Final Examination – 15 December 2001 Page 2 of 13

CS211a Final Examination – 15 December 2001 Page 2 of 13
CS 211a Final Examination – 15 December 2001 Page 12 of 13

THE UNIVERSITY OF WESTERN ONTARIO

LONDON CANADA

COMPUTER SCIENCE 211a

FINAL EXAMINATION

15 DECEMBER 2001

3 HOURS

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.

1. [1 mark] The unix utility cvs has the same functionality as the utility make.

a) True

b) False

2. [1 mark] Running the program

#include

int main( ) {

system( “ls –lR” );

return 0;

}

is equivalent to executing the unix command ls –lR
a) True

b) False

3. [1 mark] The standard C function system can be used to invoke a shell script from inside a C program.

a) True

b) False

4. [1 mark] The main reason for a C programmer to use a union instead of a struct would be to save memory while the program is running.

a) True

b) False

5. [2 marks] Suppose that, when we run a program containing the declaration FILE *f; , the function call f = fopen( “abc.dat”, “w” ); returns the value NULL. This could happen:

a) Because the file abc.dat is not found in the current working directory.

b) Because we forgot to use the statement #include , and the function fopen was not found.

c) Because the user does not have write privileges in the current working directory.

d) For all the reasons listed above.

6. [1 mark] It is possible to write the entire contents of an array of float values to a disk file with a single call to the C function fwrite.

a) True

b) False

7. [2 marks] Suppose that we wish to add a function createSet, of type void, to a Set ADT. Which prototype can we use for this function?

a) void createSet( Set s );

b) void createSet( Set &s );

c) void createSet( Set *s );

d) void createSet( Set **s );

e) None of the above

8. [2 marks] Suppose that the user green used ls -l and got the following output:
-rwxr-x— 7 brown user 512 Sep 5 21:21 unix
what happens if he uses the command cat unix immediately?

a) The file names in the subdirectory unix will be displayed.

b) The system gives an error message.

c) If green is also in the group user, the content of the file will be displayed.

d) Depending on whether unix is a directory or a file, the result will be different.

9. [1 mark] If an executable file ls is in current working directory, then typing ls will always execute the ls file in the current directory, instead of execute the ordinary unix utility ls.
a) True

b) False

10. [3 marks] What is the output of the following program?

#include
int main(){
int x = 1, y = 2;
float z;
z = x / (float) y + 0.6;
printf(“%d\n”, (int)z );
return 0;
}
a) 0

b) 1

c) 2

d) 3

11. [3 marks] What is the output of the following program?

#include
#include
struct point{
int x;
int y;
};
int main(){
struct point a, b={3,4};
memcpy(&a,&b,sizeof(struct point));
printf(“%d,”, a.x);
b.x = 5;
printf(“%d\n”, a.x);
return 0;
}
a) 3,5

b) 3,3

c) The output is not predictable.

d) The compiler will give a compilation error because a.x is not initialized.

12. [3 marks] What’s the output of the following program?

#include
int main(){
int x = 13, y = 2;
int z, w;
z = (x > y) ? x : (w=y);
printf(“%d,%d”, z, w );
return 0;
}
a) 13,2

b) unpredictable because w is not initialized.

c) 2,2

d) compilation error because w is not initialized.

13. [2 marks] If we wish to free up all dynamically allocated space in a C program before it finishes execution, and the program used no function other than malloc to allocate space, then the program must make one call to free for each call it made to malloc.

a) True

b) False

14. [3 marks] What’s the output of the following program?

#include
int x = 5;
int inc(int *x){
(*x)++;
return *x;
}
int main(){
int x = 13;
inc(&x);
printf(“%d”, x );
return 0;
}
a) 5

b) 13

c) 6

d) 14

15. [3 marks] What’s the output of the following program?

#include
int main(){
int i, sum;
for(i=0,sum=0;i<100;i++,sum += i); printf(“sum=%d\n”, sum ); return 0; } a) 5050 b) 4950 c) A list of numbers, of which the first one is 0 and the last one is 5050 d) A list of numbers, of which the first one is 0 and the last one is 4950 16. [3 marks] What’s the output of the following program? #include
int count(){
static int x=0;
return ++x;
}
int main(){
int k;
for(k=0;k<10;k++){ printf(“%d\n”,count()); } return 0; } a) A list from 0 to 9. b) A list from 1 to 10. c) A list of ten 0s. d) A list of ten 1s. e) The output is unpredictable. 17. [3 marks] What’s the output of the following program? #include
#define DEBUG
int main(){
printf(“Happy holiday ”);
#ifdef(DEBUG)
printf(“dear programmer!”);
#elif
printf(“dear user!”);
#endif
return 0;
}
a) Happy holiday dear programmer!

b) Happy holiday dear user!

c) Happy holiday

d) Happy holiday dear programmer!dear user!

Questions 18 through 28 all use the following information:

Suppose that we have a program the type declaration

typedef struct thing {

int value;

char *name;

int list[5];

} Thing;
and the function prototypes

void funcOne( int *k );

void funcTwo( char c );
and the variable declarations

Thing t, *p;

Suppose that t and p have been initialized to (nonempty) Things.

18. [2 marks] The function call funcOne( t.value ); is syntactically correct.

a) True

b) False

19. [2 marks] The function call funcOne( p->list ); is syntactically correct.

a) True

b) False

20. [2 marks] The function call funcOne( &(t.list[2]) ); is syntactically correct.

a) True

b) False

21. [2 marks] The function call funcOne( &(p.value) ); is syntactically correct.

a) True

b) False

22. [2 marks] The function call funcOne( &(p->list[2]) ); is syntactically correct.

a) True

b) False

23. [2 marks] The function call funcTwo( t.name[2] ); is syntactically correct.

a) True

b) False

24. [2 marks] The function call funcTwo( p->name ); is syntactically correct.

a) True

b) False

25. [2 marks] The function call funcTwo( *( (*p).name ) ); is syntactically correct.

a) True

b) False

26. [2 marks] The function call funcTwo( (*p).name[0] ); is syntactically correct.

a) True

b) False

27. [2 marks] The function call funcTwo( **p.name ); is syntactically correct.

a) True

b) False

28. [2 marks] The function call funcTwo( (char)(p->num) ); is syntactically correct.

a) True

b) False

Part II — Put your answers in the space provided on the question paper.

29. [6 marks] Give simple UNIX commands (you may use options, redirection and pipes) to accomplish each of the following tasks:

a) Execute the program a.out at the background.

Answer: _____________________________________

b) Kill a process whose id is 33838.

Answer: _____________________________________

c) Display the path name of the current working directory.

Answer: _____________________________________

30. [8 marks] Write a C function initArray that dynamically allocates and returns an array that can hold n strings, and that initializes each string to NULL. n should be a parameter passed to the function. You are required to provide the function prototype, and you should indicate which C libraries your function uses. (Space for the strings should not be set aside in this function.)

31. [24 marks] The following questions concern the implementation of string functions that are equivalent to ones found in string.h . In parts a), b), and c), you are not allowed to call functions from string.h in your solutions; however, your functions can call other functions that you’ve written as part of the answer to this question.

a) [4 marks] Complete the function myStrchr by filling in the blanks appropriately. myStrchr performs the same task as strchr from string.h: it returns a pointer to the first occurrence of a given character in the parameter string, if the character is in fact found; if it isn’t found, the function returns NULL.

char *myStrchr(char * ary, char b){

char *p;

for(p=ary; *p != ‘\0’; ++){

if( ) return p;
}
return NULL;
}

b) [6 marks] Write a function myStrlen that performs the same task as strlen from string.h.

c) [6 marks] Write a function myStrdup that performs the same task as strdup from string.h.

d) [8 marks] Complete the function buildFileName, started below. You are allowed to use functions from string.h in your solution to this question.

char *buildFileName( char *oldName, char *ext )

/* oldName represents a file name, and ext represents a file extension. This function should return a file name that consists of the portion of oldName, up to and including the first period found in the name, followed by ext. If there is no period in oldName, a period and ext are added to the end of the result string. The parameter strings should not be modified by the function. Examples:

oldName ext Result string

abc.def.ghi jklmn abc.jklmn hello there hello.there mm.bbb x.y.z mm.x.y.z */

32. [20 marks]

a) [10 marks] Write a C program to accept a list of integers as command line arguments; convert them from strings to integers; compute the largest one and output it to standard output.

Hint: Standard library function int atoi(const char *str) can be used to interpret a string to an integer. The prototype of atoi is in string.h .
#include

#include

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

b) [10 marks] Write a Bourne shell script that performs an equivalent task to the C program from part a) of this question. That is, it takes a list of integers as command line arguments, determines the largest one, and outputs it to standard output.

33. [15 marks] Consider the makefile given below, and answer the questions that follow it.

CC = gcc

HDIR = ../Include

INCPATH = -I$(HDIR)

export: Main

Main: Main.o Node.o Tree.o

$(CC) -o Main Main.o Node.o Tree.o

Main.o: Main.c $(HDIR)/*.h

$(CC) -c $(INCPATH) Main.c

Node.o: Node.c $(HDIR)/NodeTypes.h $(HDIR)/NodeInterface.h

$(CC) -c $(INCPATH) Node.c

Tree.o: Tree.c $(HDIR)/NodeTypes.h $(HDIR)/NodeInterface.h \

$(HDIR)/TreeTypes.h $(HDIR)/TreeInterface.h

$(CC) -c $(INCPATH) Tree.c

clean: rm -f *.o Main core

a) What directory is Node.c found in? Answer:

b) What directory is TreeTypes.h found in? Answer:

c) List the names of three macros in this makefile. Answer:

d) List, in order, the unix commands that are executed when the sequence of commands make clean; make is entered. (Expand any macros when you write your answer.)

e) Suppose that we give the command make, and then edit TreeTypes.h. List the unix commands that are executed when we then type make again.

Scrap paper — nothing written on this sheet will be marked.

NAME: _______________________________________�

STUDENT NUMBER: ___________________________

Question

1-28. ________

29. ________

30. ________

31. ________

32. ________

33. ________

TOTAL

_________