Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Exam 2 Answer and Grading Key - Introduction to Low Level Programming | CMSC 212, Exams of Computer Science

Material Type: Exam; Professor: Hollingsworth; Class: INTRO TO LOW-LEVEL PROG; Subject: Computer Science; University: University of Maryland; Term: Fall 2005;

Typology: Exams

Pre 2010

Uploaded on 02/13/2009

koofers-user-anx
koofers-user-anx 🇺🇸

5

(1)

10 documents

1 / 10

Related documents


Partial preview of the text

Download Exam 2 Answer and Grading Key - Introduction to Low Level Programming | CMSC 212 and more Exams Computer Science in PDF only on Docsity! CMSC 212 – standard version Midterm #2 - Fall 2005 - Keleher/Plane - p. 1 of 10 CMSC 212 Midterm #2 (Fall 2005) ANSWERS AND GRADING KEY Discussion Section Time (circle one): 12:00 1:00 2:00 3:00 4:00 5:00 Elena Sorelle Morgan (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 for Thursday, December 15(4:00-6:00pm) Location: H.J. Patterson 0226. Page Possible Score 2 10 3 9 4 6 5 23 6 15 7 21 8 16 Total 100 CMSC 212 – standard version Midterm #2 - Fall 2005 - Keleher/Plane - p. 2 of 10 1.) [25 points] Define and explain the following as requested: a) Assuming that you want to dynamically link a library and use the functions described in that library into the function you are writing, describe the code that would need to appear within the current function so that the functions of that library could be called. Make sure you include all steps that would be used if only this function needs to access those library functions.(note: describe the steps since you don’t have enough specifics to write the code) [5 points possible] 1 point for mention of the dlopen 1 point for mention of testing the return value of dlopen 1 point for each of the following (up to 2 points) for the mention of the dlsym function pointers to the functions defined in the library for mentioning checking the return value of the dlsym 1 point for the dlclose b) Write all of the pieces of code that would be necessary so that the one line z = callit(x,y) could be used to call the add function if the numbers are both positive or the multiply function if either (or both) of the integers are non-positive. int add(int a, int b){ return a+b; } int multiply(int a, int b){ return a*b; } /*make sure you write both type definitions and code as needed*/ [ 5 points possible] typedef int (*fpntr)(int, int); [not necessary if they do it inside] int funct(int x, int y){ int z; /* write the code that would go here so that z gets the correct value as described above when the one line shown below the blank space is executed */ fpntr callit; [2 points for the declaration of the type of the callit function] if (x > 0 && y > 0) callit = add; else callit = multiply; z = callit(x,y); Comment [JP1]: the 5 points are split 2 for the setup of the library 2 points for the setup or use of the function pointers 1 point for the close of the library Comment [JP2]: lose 4 out of 5 if it calls the correct function but does not use the last line that was given (no function pointer at all) lose 2 points if they have the code but no definition of the type of the callit item the code about which to call and assigning the function pointer a value is worth 3 points if they use a & or a * or () with add and/or multiply they lose 2 of those 3 if they have it backwards they lose 1 of the 3 CMSC 212 – standard version Midterm #2 - Fall 2005 - Keleher/Plane - p. 5 of 10 2.) [23 points] Write the complete main that would exit setting status to -1 and with a printed error message if there are not at least two arguments (in addition to the application which is in argv[0]). If there are at least two arguments, it should count how many would be positive integers (containing only numeric digits) and how many must only be considered as strings. It should then print out both counts. It then also prints the sum of the positive integers. You may use the functions: int isdigit(char); int atoi(const char *); For example: call from shell prompt output printed to the screen countthem a b 12 4 c Numeric: 2 non-Numeric: 3 Sum: 16 countthem There are not at least two arguments countthem 12 23 13 1324a Numeric: 3 non-Numeric: 1 Sum: 48 [23 points possible] int main(int argc, char * argv[]){ int total = 0, curr=0; int picount = 0, strcount=0; if (argc < 3){ printf(“There are not at least two arguments\n”); return -1; } for (curr = 1; curr < argc; curr++){ if (ispos(argv[curr]){ picount++; total += atoi(argv[curr]); } else{ strcount++; } } printf(“Numeric: %d non-Numeric: %d Sum: %d\n”, picount, strcount, total); return 0; } --------------------------------------------- int ispos(const char str[]){ char *c = str; int allzero; while (*c != ‘\0’) if (!isdigit(*c)) return 0; c = str; allzero = 1; while (*c != ‘\0’){ if (*c != ‘0’) allzero = 0; c++; } if (allzero == 1) return 0; else return 1; } Comment [JP8]: This is broken among the concepts noted below Comment [JP9]: the test to make sure there are at least 2 other values after name of the executable is worth 4 of the 23 points Comment [JP10]: Correctly tracking the argument vector is worth 7 of the points of the 23 If they included the executable in the list -2 If they did not correctly convert and sum the value into the total up to -3 If they did not correctly increment the string counter it is up to -2 If they did not correctly increment the positive integer counter it is up to -2 Comment [JP11]: printing the result is worth 3 of the 23 points (one for each of the three values to be taken care of Comment [JP12]: They need to have some way to determine if that argument is indeed all numeric This portion is worth 9 of the 23 points it does not have to be done in a helper function if they did write a helper function they don’t need to have a prototype or any certain order between the two functions if they correctly allowed negative integers to be summed in -2 if they accepted anything with a – (anywhere in the number accepting that as numeric) -4 if they did not take care of the zero case (it should not be counted as a positive integer) -1 if they used isdigit incorrectly (to assume it did the whole string -5 CMSC 212 – standard version Midterm #2 - Fall 2005 - Keleher/Plane - p. 6 of 10 3.) [15 points] Answer the following questions about multiple processes from within a single C source file. a) When the fork function is called, what value does the child process have as the return value of that function? [3 points possible] 0 b) When the fork function is called, what values could the parent process have as the return value of that function? [6 points possible] 3 for saying the process ID of the child or saying a positive value 3 for saying the value -1 in the case that the forking of a child was not possible c) If the value of a variable named parentvar is set to 5 before the fork command is called and the following line appears in the child portion of the code, circle the letter of the statement that best describes what will happen. printf(“%d\n”, parentvar); a) The child will declare it’s own variable named parentvar with a random initial value. b) The child will declare it’s own variable named parentvar with a 0 as the initial value. c) The child will declare it’s own variable named parentvar with a 5 as the initial value. [3 points] d) There will be a compilation error because that variable is in the parent’s space. e) There will be a runtime error as the child accesses the parent’s space. f) None of the above. d) If the child changes the value of parentvar to 7, after the fork but before the child finishes, select the line that best describes what will happen if the following line appears after the wait in the parent process (so this line is running after the child has completed). Assume the parent did nothing to modify the value of this variable since it was originally set before the fork in the previous question. printf(“%d\n”, parentvar); a) The parent will print the value 7 since they share the same variable space. b) The parent will print the value 5 since the child changed its own copy of the variable. [3 points] c) The parent will print a random value since the space got corrupted -by the child’s attempt to change the value. d) There will be a compilation error because they can not both have access to the same variable. e) There will be a runtime error as the child accesses the parent’s space so this line won’t be executed at all. f) None of the above. Comment [JP13]: They must say 0 here – basically all or nothiing Comment [JP14]: 3 points for each -- what happens when the fork is successful - at leastthey must say a positive integer (they don’t have to say that it is the PID of the child) -- what happens when the fork is not successful Comment [JP15]: all or nothing Comment [JP16]: all or nothing CMSC 212 – standard version Midterm #2 - Fall 2005 - Keleher/Plane - p. 7 of 10 4.) [21 points] Project 4 as defined used the following types. Write function called “distorttree” which will make a new tree that has all of the arithmetic operators changed to their opposite (plus becomes minus, minus becomes plus, mult becomes div and div becomes mult). The function returns the distorted tree through the return value of the function. You may write helper functions as needed. Names have been reduced to save some writing time. typedef enum { operatorNode, variableNode, constantNode } nodeType; typedef enum { plusOp, minusOp, multOp, divOp, equalOp } 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); /* assume these */ ASTnode *createConstantNode(int constant); /* functions have been written*/ ASTnode *createVariableNode(char *name); /* and tested already for you */ ASTnode *copyTree(ASTnode *node); /* you do not need to write them*/ --------------------------------------------------------------------------- ASTnode *distortTree(ASTnode *node); /*This is the prototype for the function you must implement*/ [21 points possible] ASTnode * distortTree(ASTnode * root){ ASTnode* newtree = copyTree(root); modtree(newtree); return newtree; } void modtree(ASTnode *root){ if (node == NULL) return; if (root -> type == operatorNode){ if (root -> operator == plusOp) root->operator = minusOp; else if (root -> operator == minusOp) root->operator = plusOp; else if (root -> operator == multOp) root->operator = divOp; else if (root -> operator == divOp) root ->operator = multOp; } modtree(root->left); modtree(root->right); } ------------- or ------------- Comment [JP17]: two basic ways – more people did the harder way they could use the copyTree function and then distort the copied tree they could make a new form of copy that id all of the copying and distorting Comment [JP18]: 9 points for correctly copying the structure of the tree Comment [JP19]: 9 points for correctly distorting the tree Comment [JP20]: 3 points for returning the pointer to the root of their new tree as the return value of the function
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved