Final Exam Content: Final Intro to C Review - Pass by reference Functions - General Syntax - Structures - Recursion(maybe?) - Programming by nature is cumulative so any past material is up for grabs as well 40 Multiple Choice in 50 Minutes
1. How long of a string can you have in a character array of length 10? 2. printf( < >, g[4]); What percent code would you use for this line of code? Assume g has already been declared and initialized as a 2D char array. a. %d b. %c c. %f d. %s 3. Choose the correct function signature. a. int a addnums (int x, int y) b. void x printstuff(void b) c. float dividenums (int, int) d. char change(int z, int b) 4. What is the output of this program? int x = 357, y = 109; swap(&y,&x); printf("%d %d", x, y); void swap (int *x, int *y) int temp; temp = *y; *x = *y; *y = temp;
5. True or false. This is a valid struct definition. typedef struct int age; char name[20]; float height; ; 6. What will happen if you try to write to a file that does not exist using the w mode? a. The compiler will create one for you. b. The function will return NULL. c. The program will not compile. d. None of the above. 7. Expected output? int i, array[5], a = 9, b = 1, c = 1; array[0] = 12; for(i = 1; i < 5; i++) array[i] = array[i - b] + a; a += a; b = c; printf("%d\n", array[4]);
8. Expected output? int main() int nums[2][4], i, j, t=15; for(i=0; i<2; i++) nums[i][3] = t; t+=2; for(j=0; j<3; j++) nums[i][j] = t; t-=3; printf("%d\n", nums[0][0]); printf("%d\n", nums[0][3]); printf("%d\n", nums[1][3]); 9. Which of these is a valid way to insert a string into a char array? (assume word is a character array of length 20) a. strcat( Bee, word); b. word[0] = B ; word[1] = e ; word[2] = e ; word[3] = \0 ; c. strcpy( Bee, word); d. None of the above 10. What is the expected output? int z = 27, *k = &z, b = *k; k = &b; z = *k * 3; z /= *k; *k /= z; k = &z; printf("z = %d\t*k = %d\tb = %d", z, *k, b);
11. Write a program that places the string anxiety into a char array just large enough for it, then prints out the string in reverse order. #include <stdio.h> #include <string.h> 12. Fill in the code so that you put the string Stress into a file called finals.txt. FILE *ofp; char str[10]; strcpy(str, "Stress"); ofp = fopen( ); fprintf( ); fclose( );
13. Expected Output? int r = 3, m = 7; m = z(r, m-r); printf("m = %d\n", m); int z(int m, int r) int z = 0, i; for(i = 0; i < r; i++) z+=m+r; return z; 14. What number will strlen return for this string? +---+ +---+ +---+ +---+ +---+ +-----+ C r u m b \0 +---+ +---+ +---+ +---+ +---+ +-----+ 15. Which of the following initializes all the elements of the array to 0? a. int nums[23] == 0; b. int nums[23] = 0; c. for(i = 0; i < 23; i++) nums[i] = i; d. b & c e. None of the above
16. What is the expected output? void w(int *x, int *g); void v(int y, int h); int t(int h, int y); int a = 2, b = 3; w(&a,&b); printf("a = %d, b = %d\n", a, b); v(b,a); printf("a = %d, b = %d\n", a, b); b = t(b,a); printf("a = %d, b = %d\n", a, b); void w(int *x, int *g) int temp = *g; *g = *x**g; *x += *g; return; void v(int y, int h) y *= h; h *= y; return; int t(int h, int y) h += y; y *= h; return y;
17. Based off of the following lines of code, fill in the struct. Any array lengths will be of length 10; #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct hat ; shoe ; x ; flo ; o; o p; float y; strcpy(p.shoe[0], "Coolio"); scanf("%c", &p.hat[0]); printf("%d\n", p.x); p.flo = &y; 18. Using the function signature below, write a function that compares two integers and sets the smaller one = to 0 and returns. If the numbers are equal, return right away. void deathmatch (int *x, int *y)
19. Expected output? #include <stdio.h> #include <stdlib.h> int y(int a, int *b, int *c); void g(int *a, int *b, int *c); int main() int a = 8, b = 13, c = 7; g(&b, &c, &a); printf("%d %d %d \n", b, a, c); b = y(b, &a, &a); printf("%d %d %d \n", b, a, c); int y(int a, int *b, int *c) if(*c > *b) *c -= *b; return *c + *b / 2; else if(*c < *b) *b -= *c; return *b + *c / 2; else return a + *c * 2; void g(int *a, int *b, int *c) int y = *a, z = *c, j = *b; *a = z; *c = y; *b = j;
20. True or false. This function takes in a person struct pointer and successfully prints out its contents. void printperson(person *user) printf("%s\n", user.name); printf("%d\n", user.age); printf("%f\n", user.height); 21. What is this recursive function doing? int y(int x) if(x <= 1) return 1; return x * y(x-1); 22. How many times will bananas be printed? for(i = 0; i < 6; i++) for(i = 0; i < 3; i++) printf("bananas\n"); 23. Which of these successfully allocates an integer array of size 10? a. int *x = calloc(sizeof(int) * 10); b. int *x = malloc(10); c. int *x = calloc(10); d. int *x = malloc(sizeof(int) * 10); 24. What is this recursive function doing? int foo(x, y) if(y <= 1) return x; return x + foo(x, y - 1); 25. What is the output? int main() double i[20]; printf("%d", sizeof(i));
26. True or false. This program will allocate memory for a double array of size 5 and print out its 5 trash values. int main() int i; double *ar = calloc(5, sizeof(double)); for(i = 0; i < 5; i++) printf("array[%d] = %lf\n", i, ar[i]); free(ar);