CIS 506 - Midterm - Fall 1995

Name ______

Closed book and notes.

Carefully PRINT the LETTER of the best answer in the space provided.

1.The length of a string in C is determined by

A. A count kept in the first byte of the string.

B. The size of the string when declared.______

C. The position of the first binary zero byte scanning from right to left.

D. The position of the first binary zero byte scanning from left to right.

2.If the variables S and T are pointers to strings, the statement S=T;

A. Compares the two strings.

B. Copies the bytes of string T into the storage area pointed by S.______

C. Is syntactically invalid.

D. Copies the contents of T into S.

3.If the variables S and T are the names of two character arrays, the statement S=T

A. Compares the two strings.

B. Copies the bytes of string T into the storage area pointed by S.______

C. Is syntactically invalid.

D. Copies the contents of T into S.

4.The following function

fun5(char *p, char c)

{

char *s;

s = p;

while (*s++ != c);/* scan for c in string p */______

return s-p;

}

A. Always returns the address of where the character in c is found in the string pointed to by p.

B. Contains a syntax error.

C. Returns the position number of where the character in c is found in the string pointed to by p;

D. Contains a logic error.

5.If integers take 2 bytes, characters take 1 byte, and long takes 4 bytes, how many bytes are occupied by the variable x given the following declarations?

struct y {

int a;

char b;

char c;

} x;

A. There is a syntax error.

B. 2______

C. 4

D. x is not a variable.

6.If integers take 2 bytes, characters take 1 byte, and long takes 4 bytes, how many bytes are occupied by the variable x given the following declarations?

union y {

int a;

char b;

char c;

} x;

A. There is a syntax error.

B. 2______

C. 4

D. x is not a variable.

7.A two dimensional array of size [8,4] is to be stored in an one dimensional array by row with size of [32]. Assuming subscripts start at ONE, which element of the one dimensional array corresponds to element [2,2]?

A. 5

B. 6______

C. 7

D. 8

E. None of the above.

8.A stack can be implemented using which of the following data storage?

A. An array.

B. A linked list.______

C. Any of the above.

D. None of the above.

9.The code implementing push should check for ______and pop should check for ______. Which answer best fills the blanks in order?

A. push() and pop()

B. Underflow and overflow.______

C. Overflow and underflow.

D. Syntax errors and runtime errors.

10.A stack can be used to store a mixture of data types.

A. Never

B. For any implementation of stacks.______

C. Only if implemented using pointers.

D. Only if implemented using arrays.

11.Parenthesis in infix expressions are _____ when converting to postfix.

A. ignored

B. not allowed.______

C. checked for proper matching only.

D. never placed on the operand stack.

E. none of the above.

12.A - B / ( C * D $ E) when converted to postfix is ($ is exponentiation)

A. A B - C D * E $ /

B.A B C D * E $ / -______

C.A B - C D E $ * / -

D.A B C D E $ * / -

E.Illegal expression to convert.

13.A - B / ( C * D $ E) when converted to prefix is ($ is exponentiation)

A. / $ E * D C - B A

B.- / * $ A B C D E______

C.- A / B * C $ D E

D.- / A B $ * C D E

E.None of the above

14.When a binary search is used, it is assumed that the data

A. is in sorted order.

B. contains no duplicate.______

C. is all of the same type.

D. all of the above.

E. only A and B.

15.A binary search must always

A. must use recursive algorithm

B. should be done using priority queues implemented using linked list______

C. can be used only to search for integer values

D. all of the above.

E. none of the above.

16.When recursion is used

A. A function must call itself directly or indirectly

B. Cannot use any global variables______

C. Cannot use any static variables

D. All of the above

E. Only A and B

17.A stack is

A. a FIFO queue

B. a LIFO queue______

C. Linked list

D. Both B and C

E. None of the above

18.A linked list

A. can be implemented in an array using subscripts for pointers

B. can be implemented using real memory addresses for pointers______

C. must have a pointer to the first element

D. can contain a variety of data types in the same list

E. all of the above

F. none of the above

Use the following information for next 6 questions.

struct node {

int key;

struct node *next;

} ;

struct node *p, *head;

Assume that p has been initialized to point to some node in a single linked list and head points to the beginning of the single linked list. However, it is possible that the value of head may be NULL.

19.p->next

A. is syntactically incorrect

B. is an address______

C. is an entire node

D. is a value type int

E. none of the above

20.*(p->next->key)

A. is syntactically incorrect

B. is an address______

C. is an entire node

D. is a value type int

E. none of the above

21.*p->key

A. is syntactically incorrect

B. is an address______

C. is an entire node

D. is a value type int

E. none of the above

22.(10 points) Write a c function insert(struct node *ps, int x) where the function inserts a new node into a single linked list after the node pointed to by ps. Assume list pointer head is globally visible and getnode() function allocates a new node and return a pointer to the node, if allocated. Otherwise getnode() returns NULL.

23.(10 points) Write a c function int delete(struct node *ps) which deletes a node pointed to by ps from a single linked list and returns the value from the deleted node if any. If any illegal operation is done or value does not exist, -1 is returned. Again assume that the list head is globally visible.

24.(10 points) Assume that a single linked list has been built. Write a c function void printlst() which prints all information stored in the list, one information per line. Again assume that the list head is globally visible. If the list is empty, the function must print a message to that effect.