CS 113 FINAL QUIZ (with solutions)
1.
Which are valid identifiers? Answer with YES(valid)/NO(invalid)
a.
_12
b.
1abd NO
(it starts with an integer)
c.
sw2d_f
d.
a;bc
e.
a23
f.
_a1
2.
What's wrong with this? (explain briefly)
a.
int a[77]; void *ptr; ptr=&a; /* a compile error */
Since a is the name of an array, it cannot be pointer at. If a was just a pointer of type int * then the statement ptr=&a; would be valid, since void * is a generic pointer, so this is not the reason why this statement is invalid.
b.
int k=1, *ptr; ptr=&(k+3); /* a compile error */
(k+3) is an expression and as such does not have a space in memory (well not a permanent one anyway); thus we cannot get the address where the result of the expression is stored.
c.
A function for swapping two integers /* a logical error
*/
void swap(int a, int b)
{ int temp;
temp=a; a=b; b=temp; }
Since values are passed by value in the function and we only pass the values of the two variable, the changes that are made to them (swapping them) will not persist after the function exits. In order for the changes to persist we need to pass pointers to the variables instead of the variables themselves.
d.
int *ptr; ptr*=2; /* a compile error */
ptr*=2; means ptr=ptr*2; Multiplication however is not a valid operation when applied to pointers, meaning that we cannot multiply pointers.
e.
void foo(const char string[], int *a) /* a compile
error */
{ static int i;
for (i=0; string[i]; i++) string[i]++;
a=&i; }
Since string is declared as a constant (const) array its elements cannot and should not be altered. Trying to increase its elements by one with the statement string[i]++; will therefore cause an error. Note that the statement a=&i; is completely valid, since a static variable has a non-temporary address in memory and can thus be pointed to!
3.
What is the output of this program?
#include <stdio.h>
typedef struct node { int array[5]; char c; } node;
int foo() { static int i; printf("i=%d\n", i); return i++; }
void printNode(node *n) { int i; for (i=0; i<5; i++) printf("%d ", n->array[i]); printf("%c\n", n->c); }
void main() { node a, b, *p; int i, j; printf("%5.3lf %5.3lf %5.3lf\n", (double)(8/5), (double)(8.0/5.0), (double)(8/5.0)); printf("\"%c-%s\"\n", *("Cornell"+4), "Cornell University"+6); printf("%c,%d\n", 66, sizeof(a.array)/sizeof(int)); for (i=1, j=0; i<20; i*=2, j++) a.array[j]=i+foo(); b=a; p=&a; a.c='a'; b.c='b'; p->c='p'; a.array[2]=5; b.array[3]=9; p->array[4]=17; printNode(&a); printNode(&b); printNode(p); }
The output of this program is:
1.000 1.600 1.600
"e-l University"
B,5
i=0
i=1
i=2
i=3
i=4
1 3 5 11 17 p
1 3 6 9 20 b
1 3 5 11 17 p
Some things to note:
a static variable is automatically initialized to 0 and its value persists between every call of the function
b=a means that the contents of a are copied into b, but after that any changes made to either do not affect the other one
p=&a means that p points to the structure a; any changes made using p or a are made to the same structure (a)
4.
Find the errors in this code: (just underline them)
#include <stdio.h> #include <stdlib.h>
#define N 100;
struct node { char c, struct node *left, *right; } first;
/* returns true or false * / char *strfoo(int *_n) { if (n) return "TRUE"; else return "FALSE"; }
void main() { int i, a[N]; first node;
i=j+1=k*2; printf("%s, strfoo(1)); node.c="c"; node.left=node.right=NULL; }
Note: Some of the errors are duplicate, so if you mark them in one place you need not mark them in the other (e.g 100; and a[N] etc.)
5.
Assume we have the following structure:
struct
doublell {
int x;
struct doublell *prev, *next;
}
This
is a node of a double-linked list; it is called a double linked list, because
each node has a pointer to the next node in the list and to the previous one.
The
following function should take two pointers p and q to nodes in the list and
insert the node pointed to by q after the node pointed to by p. You are to
assume that p, q and p->next are not NULL.
void insertAfter(struct doublell *p, struct doublell *q) { /* insert your code here */ p->next->prev=q; q->prev=p; q->next=p->next; p->next=q; }