Question 1. void func( int * ptr ) { ptr++; ptr += 1; } int main () { int arr[] = {0,0}; int * p = arr; func(p); p++; *p += 1; printf("%d %d\n",arr[1],arr[0]); } The main above will print: ______________ Question 2. Consider the following makefile: data.o: data.c data.h gcc -c data.c driver.o: driver.c data.h gcc -c driver.c io.o: io.c gcc -c io.c driver: io.o data.o driver.o gcc -o driver io.o driver.o data.o Running ls -ls we get: -rw-r--r-- 1 plab courses 673 Feb 4 18:10 data.c -rw-r--r-- 1 plab courses 673 Feb 4 16:59 data.h -rw-r--r-- 1 plab courses 673 Feb 4 17:00 data.o -rwxr-xr-x 1 plab courses 673 Feb 4 18:01 driver -rw-r--r-- 1 plab courses 673 Feb 4 17:30 driver.c -rw-r--r-- 1 plab courses 673 Feb 4 17:31 driver.o -rw-r--r-- 1 plab courses 673 Feb 4 17:40 io.c -rw-r--r-- 1 plab courses 673 Feb 4 17:41 io.o Running the command 'make driver' will re-create the following files (mark all true answers, you can mark between 0 and 5 answers): 1. driver 2. driver.o 3. data.h 4. data.o 5. io.o Question 3. #define MAX(a,b) ( ( (a) > (b) ) ? (a) : (b) ) int i=2; int j=3; int x = MAX(i++,j); printf("%d %d\n",i,x); the output of this code is: ______________ Question 4. Consider the command: g++ ex0.o stack.c -o ex0 1. The command performs compilation and linkage, but doesn't perofrm preprocessing. 2. The command only performs compilation. 3. The command only performs linkage. 4. The command performs both linkage and compilation Question 5. #include void f(int* pa[]) { for (int i=0; i<5; i++) { pa[i] = (int*)malloc(7*sizeof(int)); } } int main() { int* pa[5]; for( int i=0; i<5; i++ ) pa[i] = (int*)malloc(7*sizeof(int)); f(pa); for (int i=0; i<5; i++) free(pa[i]); } 1. The program causes runtime error. 2. The program has a memory leak. 3. The program will not compile. 4. All the above are incorrect Question 6. Assume that sizeof(char)=1, sizeof(int)=4, and sizeof(void*)=4 struct Str1 { int x; char y[8]; }; struct Str2 { int x[2]; char *y; }; sizeof(Str1) - sizeof(Str2) = _____________ Question 7. struct Flight { char source[20]; char * destination; }; Flight ticket1; Flight ticket2; ticket1.destination = (char*)malloc( 20*sizeof(char) ); strcpy(ticket1.source, "florence"); strcpy(ticket1.destination,"london"); ticket2=ticket1; *(ticket2.destination + 2) = 'k'; ticket2.source[1] = 'm'; printf("%s %s",ticket1.source,ticket2.destination); the output of the code will be: _______________________ Question 8. #define BAR(x) x ) #define FOO(x) ( x++ int a = 2; int b = 3; int c = FOO(a) + BAR(b); 1. This code will generate a compilation error. 2. This code will generate a run-time error. 3. The code will compile without problems. 4. This code will generate a pre-processing error. Question 9. #include #include void f(int* pa) { static int* pb = NULL; if (pb != NULL) { (*pb)++; } pb = pa++; } int main() { int a[5]; for (int i=0; i<5; i++) a[i] = i; for (int i=0; i<2; i++) f(a + i); for (int i=0; i<5; i++) printf("%d ", a[i]); printf("\n"); } 1. The program does not compile 2. The output is: 0 1 2 3 4 3. The output is: 1 1 2 3 4 4. The output is: 1 2 3 4 4 Question 10. Suppose that sizeof(char) = 1, sizeof(int) = 4, sizeof(double)=8. union ID { char x; double y; }; sizeof( ID ) is ________________ Question 11. file1.h --------------- #if !defined(FILE1_H) #define FILE1_H int x=7; #endif //end file1.h file2.h -------------- #if !defined(FILE2_H) #define FILE2_H #include "file1.h" int x=8; #endif //end file2.h file3.c -------------- #include "file1.h" #include "file2.h" int main() { printf("%d",x); return 0; } //end file3.c The command "gcc file3.c -o prog" will result: 1. no error/warning will be generated 2. error/warning in the pre-processing stage 3. error/warning in the compilation stage 4. error/warning in the linkage stage Question 12. void main() { (1) int *y = (int*)malloc( 3*sizeof(int) ); (2) int x[] = {1,2,3}; (3) int ** pp = NULL; (4) int *z = y; } A space on the memory heap was allocated in the following lines (mark all correct answers, between 0-4): a. 1 b. 2 c. 3 d. 4 Question 13. void foo( int const* r ) { (1) (*r)++; } void bar( int const *p ) { (2) printf("%d\n", *p ); } void apple( int const* q ) { (3) q++; } int main() { int x = 3; int y = 3; (4) foo(&x); (5) x++; (6) bar(&y); (7) apple(&x); } Compilation errors were generated on the following lines (mark all correct answers, between 0-7): a. 1 b. 2 c. 3 d. 4 e. 5 f. 6 g. 7 Question 14. char s[] = {'p','l','a','b'}; What will be returned by calling strcmp(s,"plab")? 1. a negative number 2. 0 3. a positive number 4. We cannot know for sure Question 15. struct IntList { int value; IntList* next; }; int Last( IntList *L ) { for( ; L->next != NULL; L = L->next ) ; return L->value; } main() { IntList *p = (IntList*)malloc(sizeof(IntList)); int x = Last(p); } 1. This code will link errors 2. This code will generate compilation errors 3. The code will compile, but we canont predict what happens at run-time. 4. The code will generate run-time error. Question 16. void bar(int * x) { x[0] += 2; } void foo( int * x ) { x[1] += 2; } typedef void (*myfunc)(int*); main() { myfunc funcs[2] = { foo, bar }; int arr[] = {3,4,5}; (funcs[0])(arr); (funcs[1])(arr+1); printf("%d %d %d\n",arr[2],arr[1],arr[0]); } The output of the code is: __________________ Question 17. int cmp( void const* p, void const *q ) { return *(int const*)p - *(int const*)q; } int arr[] = { 1, 4, 5, 2 }; qsort( arr, 4, sizeof(int), cmp ); 1. After running this code arr = { 5, 4, 2, 1 }; 2. After running this code arr = { 1, 2, 4, 5 }; 3. The code will not compile 4. The code will compile, but we cannot be sure about the content of arr after running it Question 18. (1) int i; (2) int * const px = &i; (3) int j = 5; (4) px = &j; (5) px[1] = j; 1. Line 2 will generate compilation errors/warnings 2. Line 4 will generate compilation errors/warnings 3. Line 5 will generate compilation errors/warnings 4. The code will not generate compilation errors/warnings Question 19. (1) char s[] = "plab"; (2) (*s)++; (3) *(s+2) = '\0'; 1. line 2 will generate a compilation error 2. line 3 will generate a compilation error 3. after runnung this code the string s is "ql" 4. after runnung this code the string s is "pl" 5. after running this code the string s is "plab" Question 20. In a directory we have the following 2 files: -rw-r--r-- 1 plab ntp 2306 Oct 30 07:54 list.c -rw-r--r-- 1 plab ntp 2387 Oct 28 10:51 main.c In main.c we use functions implemented in list.c. The first line of main.c is: #include "list.c" The result of running g++ main.c is: 1. the preprocessor will report an error 2. the program will not compile 3. no problems in the compilation phase, but linkage will not work 4. a.out will be created Question 21. int y = 7; int x = 18; printf("--%2d--%4d--\n", x, y ); The output of this code is: 1. -- 7-- 18-- 2. --18-- 7-- 3. --18--7 -- 4. --7-- 18-- Question 22. int *ip = (int*)malloc(100*sizeof(int)); (*ip) += 30; free(ip); 1. This code will generate a compilation error. 2. This code can generate unpredictable run-time behavior. 3. No error would occur if we only check if ip equals NULL before calling the "free" function 4. The code contains no error.