Computer Science and Engineering 2031.03 Midterm Test
June 25 2007
Answer all questions in the space provided Make sure that you have 8 pages
Student Last Name: ___________________________ Student Given Name: __________________________ Student Id. No: ________________________________
Question
Value
Score
A
50
B
50
1
Question 1. [50 points]
1. [5 points] Which component of the C compipler is run before the actual compi-
lation.
2. [5 points] How do we specify a newline in a C string.
3. [5 points] Which dimensions can we omit when declaring a multidimensional array.
4. [5 points] What is the type of ́\0 ́
5. [5 points] What is a cast
2
6. [5 points] How do we define a variable to avoid a name conflict with a variable with a different name in another file.
7. [5 points] Write 4 of the asiignment operators available in C.
8. [5 points] Which of the two conventions do we use to define a jagged 2-D array (an array with unequal row sizes)
9. [5 points] What does the following function do?
xxx(char *a, char *b)
{
while (*a++=*b++);
}
10. [5 points] The following piece of code
int *x;
x=(int *)malloc(sizeof(int)); (x==NULL)||(x[0]=3);
Is there something wrong with this code?
3
Question 2.
[50 points]
1. [10 points]
#include
int *ptr;
int arr1[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10};
int arr2[10] = {11,12,13,14,15,16,17,18,19,20};
int main() {
ptr = arr1;
printf(“%d\n”,*(ptr+1));
ptr = arr2;
printf(“%d\n”,*(ptr+2));
}
What will the above program print?
4
2. [10 points]
#include
#define DYNAMIC 01
#define DEBUG 02
#define PROFILE 04
#define THREAD 010
void fun(int options)
{
if (options & DYNAMIC) printf(“DYNAMIC\n”);
if (options & DEBUG ) printf(“DEBUG\n”);
if (options & PROFILE) printf(“PROFILE\n”);
if (options & THREAD) printf(“THREAD\n”);
}
int main() {
int options = 0;
options |= DYNAMIC;
options &= ̃DEBUG;
options |= PROFILE;
options &= ̃THREAD;
fun(options);
return 0; }
What does the above program print?
5
3. [10 points] Describe the type of the object x in the following declarations in English (for example: int **x: x is a pointer to a pointer to integer)
int *x[10];
int (*x)();
int (*x[])();
4. [10 points] What is wrong with this piece of code? Is it stylistic or substantial?
char s[MAXARR];
n=0;
while (c=getchar()!=-1)
s[n++]=c;
6
5. [10 points] What is wrong with this piece of code? Is it stylistic or substantial?
int *ip, i;
for (i=1; i<10; i++)
ip[i] = i;
7