Unformatted text preview:

CMSC 212 - Standard Midterm #2 - Spring 2005 - Hollingsworth/Plane - p. 1 of 8 CMSC 212 Midterm #2 (Spring 2005) Name _________________________ Signature ________________________ Discussion Section Time (circle one): 12:00 1:00 2:00 3:00 Asad Konstantin (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 75 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. (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) The CMSC 212 Final Exam is scheduled: 5/16/05 Monday 4 - 6pm in J.M. Patterson 3201 (note: J.M. Patterson building not the H.J. Patterson building) Question Possible Score 1 a & b 8 1 c & d 8 2 21 3 18 4a 10 4b 15 5 20 Total 100CMSC 212 - Standard Midterm #2 - Spring 2005 - Hollingsworth/Plane - p. 2 of 8 1.) [16 points] Define and explain the following terms: a) Describe the purpose of #ifndef explain how it can be used to manage the inclusion of header files b) Explain the similarities and differences between the parameters to these two functions: int func1(int* const a); int func2(const int* a);CMSC 212 - Standard Midterm #2 - Spring 2005 - Hollingsworth/Plane - p. 3 of 8 c) Compare and contrast (both differences and similarities) between malloc, calloc and alloca. d) Briefly explain how floating point values are stored in binary.CMSC 212 - Standard Midterm #2 - Spring 2005 - Hollingsworth/Plane - p. 4 of 8 2.) [21 points] Write the complete program (using any standard libraries you like) that will determine if any of the command line arguments are also the keyword (name) identifying an environment variable passed to the program. If it does match a variable's keyword, it should print as described below. If it does not match, an environment variable should be added to the list of environment variables with the keyword specified on the command line as its name and "prog" as its value and print as defined below. You can assume it matches if and only if the environment variable is exactly the same case as what was typed, but you also must assume you don't know the number of arguments or the length of any argument typed. If the same argument is passed more than one time on the same command line it should process once for each time it appears. The user may have accidentally typed a dash immediately before the argument; if the dash was typed it should be ignored in the comparison as if the dash had not been typed. Output: If it was added, you must display exactly what was added (i.e. if Jan was typed as an argument and Jan is not an environment variable "Jan=prog added" should be printed). If the argument was found in the list of environment variables, you should print it as it was found in the list (i.e. if Jan=me is in the list of environment variables and Jan is given as an argument, Jan=me should be printed).CMSC 212 - Standard Midterm #2 - Spring 2005 - Hollingsworth/Plane - p. 5 of 8 3.) [18 points] Write the output from the following program. Assume the program does run all of the way through without a segmentation fault. If there are places that you do not know the actual value being printed, you should write that value in the output as "???". This uncertainty could occur because of un-initialized values being used or because of pointers into unallocated space. #include <stdio.h> #include <malloc.h> static int stint; void funct(int a){ /* you don't know what happens in this function*/ /* but no ouput takes place */ } int main(void){ int a = 5, *b = &a, c[] ={10,21,12}; int *d, *e, f; printf("%d %d %d \n",*b, *c+1, stint); e = b; b = malloc(sizeof(int)); d = malloc(sizeof(int)); *d = 7; *b = 9; *e = 11; printf("%d %d %d\n",*e, *d, a); funct(a); e = b; free(b); printf("%d %d %d\n",*e, f, a); return 0; } ______________________________________________________________________________ ______________________________________________________________________________ ______________________________________________________________________________ ______________________________________________________________________________ ______________________________________________________________________________ ______________________________________________________________________________CMSC 212 - Standard Midterm #2 - Spring 2005 - Hollingsworth/Plane - p. 6 of 8 4.) [25 points] In Project #3, Recall ast.h contained: typedef enum { operatorNode, variableNode, constantNode } nodeType; typedef enum { plusOperator, minusOperator, multOperator, divOperator } operatorType; typedef struct _node { nodeType type; struct _node *left; struct _node *right; int value; operatorType operator; char *name; } ASTnode; ASTnode *createOperatorNode(operatorType op, ASTnode *left, ASTnode *right); ASTnode *createConstantNode(int constant); ASTnode *createVariableNode(char *name); ASTnode *copyTree(ASTnode *node); a) You suspect a bug in the copyTree code is due to left and right sub-tree getting switched during the copy, write a test to verify if this is the case. Your test should return 0 if copyTree is working correct, and –1 if there is a problem. You may use the functions listed above (and you may assume you have already tested to know that the create functions listed above work correctly).CMSC 212 - Standard Midterm #2 - Spring 2005 - Hollingsworth/Plane - p. 7 of 8 b) Write a function that given two AST trees returns 0 if the trees are structurally equal and –1 if they are not. Structurally


View Full Document

UMD CMSC 212 - Midterm #2

Download Midterm #2
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 #2 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 #2 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?