CMSC 212 Midterm #2 (Fall 2005) ANSWERS AND GRADING KEY

Similar documents
Programming in C. Pointers and Arrays

Second Examination Solution

Computer Architecture I Midterm I

Introduction to Computer Science Midterm 3 Fall, Points

C0MP1911 Final Exam 1337 Computing 1

This is an open book, open notes exam. But no online or in-class chatting.

Creating a String Data Type in C

ECE264 Spring 2013 Final Exam, April 30, 2013

ECE 15B COMPUTER ORGANIZATION

CSE 303 Midterm Exam

CS16 Midterm Exam 2 E02, 10W, Phill Conrad, UC Santa Barbara Tuesday, 03/02/2010

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME

Intermediate Programming, Spring 2017*

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

Computer Science Foundation Exam

CSE 333 Midterm Exam 5/10/13

CSE 333 Midterm Exam 2/14/14

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017)

ECE264 Fall 2013 Exam 3, November 20, 2013

COMP 524 Spring 2018 Midterm Thursday, March 1

CSE 333 Midterm Exam July 24, Name UW ID#

15-213/18-243, Spring 2011 Exam 2

Final CSE 131B Spring 2005

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

Introduction. This project will focus primarily on processes.

PRINCIPLES OF OPERATING SYSTEMS

In Java we have the keyword null, which is the value of an uninitialized reference type

CSE 333 Midterm Exam 7/29/13

Computer Science Foundation Exam

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

CSE351 Winter 2016, Final Examination March 16, 2016

CSE351 Spring 2018, Final Exam June 6, 2018

ch = argv[i][++j]; /* why does ++j but j++ does not? */

CS 322 Operating Systems Practice Midterm Questions

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Portland State University. CS201 Section 5. Midterm Exam. Fall 2018

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

CENG 447/547 Embedded and Real-Time Systems. Review of C coding and Soft Eng Concepts

THE UNIVERSITY OF WESTERN ONTARIO. COMPUTER SCIENCE 211a FINAL EXAMINATION 17 DECEMBER HOURS

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CMSC 313 Fall2009 Midterm Exam 1 Section 01 October 12, 2009

CSE 351, Spring 2010 Lab 7: Writing a Dynamic Storage Allocator Due: Thursday May 27, 11:59PM

***PRACTICE EXAM 02 for*** CS16 Midterm Exam 2 E02, 09F, Phill Conrad, UC Santa Barbara Actual exam scheduled for: 11/18/2009

Wawrzynek & Weaver CS 61C. Sp 2018 Great Ideas in Computer Architecture MT 1. Print your name:,

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

CS 237 Meeting 19 10/24/12

ECE264 Fall 2013 Exam 1, September 24, 2013

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

CSE351 Spring 2018, Midterm Exam April 27, 2018

HW1 due Monday by 9:30am Assignment online, submission details to come

Largest Online Community of VU Students

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Computer Architecture I Mid-Term I

CS , Spring 2009 Exam 2

(In columns, of course.)

a) Write the signed (two s complement) binary number in decimal: b) Write the unsigned binary number in hexadecimal:

Slide Set 4. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

EE 312 Fall 2017 Midterm 1 October 12, 2017

Midterm Exam 2 Solutions, C programming

Elementary Data Structures: Part 1: Arrays, Lists. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

CPSC 213, Summer 2017, Term 2 Midterm Exam Solution Date: July 27, 2017; Instructor: Anthony Estey

EE 312 Fall 2018 Midterm 1 Version A October 10, 2018

Common Misunderstandings from Exam 1 Material

Slide Set 4. for ENCM 335 in Fall Steve Norman, PhD, PEng

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

My malloc: mylloc and mhysa. Johan Montelius HT2016

Here's how you declare a function that returns a pointer to a character:

Department of Electrical Engineering and Computer Science Spring 2004 Instructor: Dan Garcia CS61C Midterm

15-213, Fall 2007 Midterm Exam

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

ECE 2035 Programming HW/SW Systems Spring problems, 5 pages Exam Three 8 April Your Name (please print clearly)

CS61, Fall 2012 Section 2 Notes

University of California, Berkeley College of Engineering

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Computer Systems Assignment 2: Fork and Threads Package

Chapter 11 Introduction to Programming in C

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

CS61c Summer 2014 Midterm Exam

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

CS61C : Machine Structures

Memory Corruption 101 From Primitives to Exploit

There are 10 total numbered pages, 5 Questions. You have 60 minutes. Budget your time carefully!

Do not start the test until instructed to do so!

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Summer 2016 Programming Assignment 1 Introduction The purpose of this

Department of Electrical Engineering and Computer Sciences Spring 2013 Instructor: Dr. Dan Garcia CS61C Midterm

POINTER AND ARRAY SUNU WIBIRAMA

HOT-Compilation: Garbage Collection

CS16 Midterm Exam 1 E01, 10S, Phill Conrad, UC Santa Barbara Wednesday, 04/21/2010, 1pm-1:50pm

Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review)

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

Fundamental of Programming (C)

Transcription:

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. 1 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 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 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 */ 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 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); CMSC 212 standard version Midterm #2 - Fall 2005 - Keleher/Plane - p. 2 of 10

c) Given that the following line is used to define the library flags in your make file: LIBFLAGS = -nostdlib -shared -fpic -Wl,-soname,$@.1 Describe the purpose of the $@ that appears near the end of the line. Tell when this would be useful. [3 points possible] The $@ will be replaced by the name of the target. It is useful if there are several libraries that need to be compiled within the same makefile; it allows the same LIBFLAGS to be used for all of them because it makes the macro independent of the name of the target even though that name needs to appear within one of the switches. Comment [JP3]: they must state that the $@ gets replaced (thisis worth 3) they must say that it makes it possible to use the same LIBFLAGS (this is worth 2 points) d) Answer the following questions about using lcov. 1. What needs to appear in the Makefile so that lcov can be accessed? [3 points possible] All of the files which are supposed to be analyzed by lcov should be compiled with flags that say they are supposed to have added arcs or connections and added coverage. or -fprofile-arcs fprofile-coverage or The file profile needs to be set appropriately when compiling all files that need to be viewed by lcov. Comment [JP4]: any method of wording an explanation --they don t need to specify the exact options -- they should use either the word profile or the arcs/coverage for full credit 2. What steps do you need to take at the shell prompt in order to use lcov? [3 points possible] [1 point for each of the following to a max of 3 points] 1. compile so that lcov options are included 2. tap lcov 3. run the application (or create the extra data file used by lcov) 4. type lcov c d. o cov.out (or type command telling it where to put the lcov output) 5. genhtml cov.out 6. view the html file created Comment [JP5]: any 3 of these 6 (or their meaning) for full credit they don t need to have any certain sequence or order 1 point per to max of three if they have 4 but 1 is wrong cross it out but they still get full credit CMSC 212 standard version Midterm #2 - Fall 2005 - Keleher/Plane - p. 3 of 10

e) Compare and contrast Whitebox testing and Blackbox testing. Compare and contrast means to specify both similarities and differences. [4 points possible] compare[1 point] : both of these testing methods try to test all aspects of the program contrast[3 points]: In whitebox testing, you can see the implementation and base your test cases on what code you know is there an needs to be tested, but in blackbox testing, you only know the definition of the API and you have to test the functionality of that API without knowing what the code implementing it actually looks like. or The whitebox testing can consider issues such path coverage/branch coverage because the tester can see the code and consider all of the paths available in that code, while in blackbox testing the tester has to try to test all kinds of input for conditions such as covering the extremes and exceptional cases because the tester can t see the actual code. f) Write, in hex, the two s complement representation of the 32-bit value -9. [2 points possible] FFFFFFF7 Comment [JP6]: 4 points total 1 point for the compare anything about them both testing the run of the program 3 points for the contrast any one thing that is different between the two is fine Comment [JP7]: several things I saw done incorrectly -1 if written in binary, but otherwise correct -1 if you can tell there was a conversion error to hex but otherwise correct -1 if off by 1 (one s complement rather than 2 s complement) -2 if presented as a positive value CMSC 212 standard version Midterm #2 - Fall 2005 - Keleher/Plane - p. 4 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. 5 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 Comment [JP13]: They must say 0 here basically all or nothiing 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. 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 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 [JP16]: all or nothing CMSC 212 standard version Midterm #2 - Fall 2005 - Keleher/Plane - p. 6 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); 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 ------------- or ------------- CMSC 212 standard version Midterm #2 - Fall 2005 - Keleher/Plane - p. 7 of 10

ASTnode * distorttree(astnode * root){ ASTnode* temp = NULL; if (root!= NULL) { temp = (ASTnode *) calloc(sizeof(astnode),1); if (!temp) exit(-1); copycontent(temp, root); temp->left = distorttree(root->left); temp->right=distorttree(root->right); return temp; void copycontent(astnode *dest, ASTnode *source){ operatortype swaparr[] = {minusop, plusop, divop,multop,equalop; dest -> type = source -> type; dest ->value = source -> value; if (source ->name) dest -> name = strdup(source ->name); dest->operator = swaparr[source->operator]; return; CMSC 212 standard version Midterm #2 - Fall 2005 - Keleher/Plane - p. 8 of 10

5.) [16 Points] Recall the heap manager discussed in class, which allocates space using 8-byte chunks. Assume a total heap space of 64 bytes, and that the interface to the heap manager consists of malloc() and free() with the usual semantics. The shown heap does not contain the header discussed in class to manage the blocks just the blocks allocated directly from the request are shaded (assume the management information is stored elsewhere). Assuming each square represents an 8-byte chunk, we can pictorially represent the state of the heap at some point as follows: This shows the heap being broken into three blocks. The center of those three blocks is allocated and the two on the ends are free. a) [5 points] Assuming a first-fit allocation strategy, that the memory is completely unallocated before starting, and the sequence of malloc() and free() operations below, annotate the following diagram by shading allocated regions to indicate the state of the heap immediately after each line of the program. p1 = malloc(15); p2 = malloc(20); p3 = malloc(16); free(p2); p4 = malloc(4); free(p3); b) [5 points] Repeat the above assuming best-fit. p1 = malloc(15); Comment [JP21]: 5 points for the first fit trace -2 for showing partial 8-byte chunks as allocated but the end of the 8-byte chunk as non allocated -3 for showing partial 8 byte chunks as allocated and starting the next allocation inside of that partial 8-byte chunk -1 for any allocation/deallocatoin that did not all or remove the right squares but be careful of double jeopardy once points are removed on one line for an error, as it continues down they should not lose points for maintaining the space allocated/deallocated by that error (Pete how do you want to handle if an error is mysteriously corrected this can either be viewed as another error, or it can be accepted assuming we think they realize they did something wrong) Comment [JP22]: same as for the first fit trace -- yes they lose the 2 or 3 points again if they did it both places even though they are making the same conceptual error p2 = malloc(20); p3 = malloc(16); free(p2); p4 = malloc(4); free(p3); c) Define internal and external fragmentation. [3 points for definition of internal fragmentation] due to extra space allocated beyond what was requested in order to keep the allocated blocks at some specific granularity example in the first line 2 whole sections were allocated (16 bytes) when only 15 were requested. [3 points for definition of external fragmentation] due to the pattern of allocation and deallocation when there are several small non-contiguous blocks of unallocated space Comment [JP23]: 3 points for the definition of internal fragmentation it must include something that indicates granularity of the chucks allocated or size of the chunk actually allocates as compared to the size of the space requested Comment [JP24]: 3 points for the definition of external fragmentation it must include that it refers to spaces left available after a free takes place between two allocated portions in some way CMSC 212 standard version Midterm #2 - Fall 2005 - Keleher/Plane - p. 9 of 10

CMSC 212 standard version Midterm #2 - Fall 2005 - Keleher/Plane - p. 10 of 10