Unformatted text preview:

CMSC 212 - Standard Midterm #1 - Spring 2007 - Hollingsworth/Plane - p. 1 of 12 CMSC 212 Midterm #1 (Spring 2007) Name ________Key___________________________ (LDAP) Directory ID _____________________ Signature ________________________________ Discussion Section Time (circle one): 10:00 11:00 12:00 1:00 Jose Cole (1) This exam is closed book, closed notes, and closed neighbor. No calculators are permitted. Violation of any of these rules will be considered academic dishonesty. (2) You have 70 minutes to complete this exam. If you finish early, you may turn in your exam at the front of the room and leave. However if you finish during the last ten minutes of the exam please remain seated until the end of the exam so you don't disturb others. Failure to follow this direction will result in points being deducted from your exam. (3) Write all answers on the exam. If you need additional paper, we will provide it. Make sure your name is on any additional sheets and make sure they are submitted with your exam paper. (4) Partial credit will be given for most questions assuming we can figure out what you were doing. (5) Please write neatly. Print your answers, if that will make your handwriting easier to read. If you write something, and wish to cross it out, simply put an X through it. Please clearly indicate if your answer continues onto another page. (6) Do not use scanf or fscanf for any of the code writing questions in this exam. Page Question Possible Score 2 (1) a,b 3 (1) c,d 19 4 (2) 15 5 (3) a,b 12 6 (4) a-f 18 7 (5) a,b,c 15 8 (6) a 9 (6) b 10 (6) c 21 Total 100CMSC 212 - Standard Midterm #1 - Spring 2007 - Hollingsworth/Plane - p. 2 of 12 1.) [19 points] Answer each of the following in a short paragraph or a short piece of code: a) Compare and contrast the struct and the union as it is defined in C A struct and a union are the same in that both are aggregate types that allow heterogeneous fields – this means that you can create a single variable that is a struct or a union and it has many fields and the fields do not all need to be of the same type. They are different in that the space allocated for a struct is the sum of all of the individual members because all members exist in that space in the same time, but in the case of a union, there is only enough space allocated for the largest member and it holds only one of its members at a time. In other words a struct stores its fields as if there is an “and” between them, but a union stores its members as if there is an “or” between them. b) Describe how you would use the term “extern” as a specifier for a variable in a C program. Include what it would mean, where it would likely appear, what else has to appear, and how your program would be different because it is there. The extern specifier when used on a variable will allow that variable to extend beyond the file scope so that that same variable is available in other files. When the extern specifier is used on a variable that has file scope, it tells the compiler that the actual definition of this variable will come from another file. The actual variable (without the extern keyword) must appear in another file that will be linked in. It allows the object code to be created with the assumption that the actual definition of the variable will be provided at link time instead. If there are two variables of the same name in two different files with neither having the extern keyword, they are independent (unrelated) variables.CMSC 212 - Standard Midterm #1 - Spring 2007 - Hollingsworth/Plane - p. 3 of 12 c) Explain the difference between the p and the q as they appear here. int j= 5; const int *p = &j; (this line is equivalent to int const *p = &j;) int* const q = &j; The first line makes the integer constant. It will not allow a change in the value of the integer through the pointer named p, but p can change in value. In other words *p = 78; will cause a compilation error but p = (int*) calloc(sizeof(int),1); will not cause an error. The second line makes the pointer constant. It will not allow a change in the value of q itself, but it will allow a change in the value of the integer pointed at. In other words *q = 78; will not cause a compilation error but q = (int*) calloc(sizeof(int),1); will cause a compilation error. d) Write the complete program (main only) that will look at the command line arguments and do the following: i. If there aren’t exactly two arguments, it should give an appropriate error message and exit with status -1. ii. If there are exactly two arguments and they are exactly the same length (same number of characters), it should give an appropriate message and exit with status 0. iii. If there are exactly two arguments but they are not exactly the same length, it should give an appropriate message and exit with status 1. #include <stdio.h> #include <string.h> int main(int argc, char *argv[]){ if (argc != 3){ printf(“%s needs exactly two arguments\n”,argv[0]); return -1; } else{ printf(“%s and %s: ”,argv[1],argv[2]); if (strlen(argv[1]) == strlen(argv[2])){ printf(“same length\n”); return 0; } else{ printf(“different length\n); return 1; } } }CMSC 212 - Standard Midterm #1 - Spring 2007 - Hollingsworth/Plane - p. 4 of 12 2.) [15 points] Give the exact output that would be produced by the following code. You do not need to worry about the exact location of any whitespace characters since none of the field width specifiers are given. You only need to worry about exactly what values/text appears on which lines. #include <stdio.h> #include <stdlib.h> #include <string.h> #define ARRSIZE 12 typedef struct{ char *name; int age; }SType; int main(void){ int nums[ARRSIZE] = {3,5,7,9,2,4,6,8,1}; int *ipntr1,*ipntr2; char *cpntr1,*cpntr2; SType *spntr; spntr = (SType*) malloc(sizeof(SType)); spntr->name = (char*)calloc(4,sizeof(char)); strncpy(spntr->name,"Tim",4); spntr->age = 37; printf("%d, %d-%d-%d\n",spntr->age, *nums, nums[3],*(nums+4)); cpntr1 = spntr->name; cpntr2 = spntr->name + 1; ipntr1 = nums; ipntr2 = nums+5; *cpntr1 = 'J'; *(cpntr2+1) = 'g'; printf("%s %s %s\n",spntr->name,cpntr1,cpntr2); printf("%s %d %d\n",spntr->name+1,*ipntr1,*ipntr2); printf("%d %d\n",ipntr1[2],ipntr2[1]); return 0; } 37, 3-9-2 Jig Jig ig ig 3 4


View Full Document

UMD CMSC 212 - Midterm #1

Download Midterm #1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Midterm #1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Midterm #1 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?