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

Similar documents
CS 0449 Sample Midterm

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

University of Calgary Department of Electrical and Computer Engineering ENCM 335 Instructor: Steve Norman

CS61, Fall 2012 Section 2 Notes

PRINCIPLES OF OPERATING SYSTEMS

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

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Arrays and Memory Management

SOFTWARE Ph.D. Qualifying Exam Fall 2017

ECE264 Fall 2013 Exam 3, November 20, 2013

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

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

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

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

CSE 333 Midterm Exam July 24, Name UW ID#

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

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

EE 312 Fall 2017 Midterm 1 October 12, 2017

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CS 61C: Great Ideas in Computer Architecture Introduction to C

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

Understanding Pointers

Final CSE 131B Spring 2004

CSC231 C Tutorial Fall 2018 Introduction to C

Student Number: Instructor: Reid Section: L0101 (10:10-11:00am)

CSE 333 Midterm Exam 5/10/13

Kurt Schmidt. October 30, 2018

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

Memory Allocation. General Questions

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

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

Contents. A Review of C language. Visual C Visual C++ 6.0

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

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

CSE 333 Midterm Exam Sample Solution 7/28/14

Dynamic memory allocation

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

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

Bristol Institute of Technology

Good Luck! Marking Guide. APRIL 2014 Final Exam CSC 209H5S

CSE 333 Lecture 6 - data structures

Common Misunderstandings from Exam 1 Material

Arrays and Pointers in C. Alan L. Cox

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

C0MP1911 Final Exam 1337 Computing 1

ECE264 Spring 2014 Exam 2, March 11, 2014

CSE 333 Autumn 2013 Midterm

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

CSE 303 Winter 2008 Midterm Key

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

CSE 374 Final Exam 3/15/17 Sample Solution. Question 1. (8 points) Suppose we have the following two statements in a C program:

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

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

Intermediate Programming, Spring 2017*

This document contains the questions and solutions to the CS107 midterm given in Winter 2018 by instructor Chris Gregg. This was a 120-minute exam.

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

Armide Documentation. Release Kyle Mayes

C Language Summary (Continued)

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

Intermediate Programming, Spring 2017*

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

mith College Computer Science CSC231 Bash Labs Week #10, 11, 12 Spring 2017 Introduction to C Dominique Thiébaut

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

Question 1. [15 marks]

C mini reference. 5 Binary numbers 12

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

25.2 Opening and Closing a File

Approximately a Test II CPSC 206

Student Number: Instructor: Reid Section: L5101 (6:10-7:00pm)

CS 2301 Exam 3 B-Term 2011

CS 61c: Great Ideas in Computer Architecture

Dynamic memory allocation (malloc)

CS16 Exam #1 7/17/ Minutes 100 Points total

Pointers, Arrays, Memory: AKA the cause of those Segfaults

Quick review pointer basics (KR ch )

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

Procedural programming with C

Signature: ECE 551 Midterm Exam

MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes

CSE 333 Midterm Exam Sample Solution 5/10/13

ECE551 Midterm Version 1

Creating a String Data Type in C

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

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

TI2725-C, C programming lab, course

ECE551 Midterm Version 2

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

Fundamentals of Programming. Lecture 10 Hamed Rasifard

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Intermediate Programming, Spring 2017*

EL2310 Scientific Programming

CSE 333 Midterm Exam July 24, 2017 Sample Solution

Final Exam. Fall Semester 2016 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

Transcription:

Midterm Exam Nov 8th, 2012 COMS W3157 Advanced Programming Columbia University Fall 2012 Instructor: Jae Woo Lee About this exam: - There are 4 problems totaling 100 points: problem 1: 30 points problem 2: 30 points problem 3: 30 points problem 4: 10 points - Your answers should be written directly to the exam booklet itself. Do not use any bluebook. The exam booklet has ample space for your answers. If you need more for some reason, blank sheets of papers will be provided. Make sure to put your name and UNI in all loose sheets you turn in. - Everything you write will be considered when we grade. This can be good or bad. If you give two different answers to a question, we will take the one that will result in a LOWER grade. So make sure to cross out clearly anything that you don t consider your final answer. - Assume the following programming environment: Language: C Compiler: gcc Platform: Ubuntu Linux 12.04, 64-bit version Primitive type sizes: sizeof(int) is 4 and sizeof(int *) is 8 Please write your name and UNI: Name: UNI: Good luck! 1

Problem [1]: 30 points (15 expressions, 2 points each) ------------------------------------------------------- Consider a C program s main() function that starts as follows: struct MdbRec { char name[16]; char msg[24]; }; int main(int argc, char **argv) { int a[10] = { 100, 101, 102, 103, 104, 105, 106, 107, 108, 109 }; int *p = a + 2; struct MdbRec m[1]; strcpy(m[0].name, "dude"); strcpy(m[0].msg, "0"); int x = 0xffffffff; int y = 0xffffffff; unsigned char *c = (unsigned char *)&x; Evaluate the 15 given expressions in the context of the main() function. Here is what I mean by evaluating an expression: - For integer expressions (i.e., the expressions whose types are char, short, int, size_t, long, or long long--either signed or unsigned), write the actual number value in decimal notation. - For non-integer expressions, write the type name, in the format that you use to declare a variable of that type. Some example type names include (but not limited to): int * double double ** int (*)(int) - Write "invalid" if a given expression is not a valid C expression. Please note that: - Boolean expressions are integer expressions in C. Please do not write "true" or "false". - You can assume that the program is run with no command line argument. - None of the given expressions have const types, i.e., you don t need to worry about putting const in front of any of the types. Please be careful. Some of these are VERY tricky. 2

Problem [1] continued ---------------------- (1.1) p[2] (1.2) sizeof(p) (1.3) m->msg + 1 (1.4) "0"[1]==0 && "0"[1]== \0 (1.5) *argv++ (1.6) m->msg - (char *)m (1.7) &m[0] (1.8) sizeof(m[0].msg) 3

Problem [1] continued ---------------------- (1.9) &p (1.10) x + y (1.11) c[2] (1.12) &c (1.13) ( x) & y (1.14) sizeof(a) / sizeof(a[0]) (1.15) *&*&a[5] 4

Problem [2]: 30 points ----------------------- Consider the following program, arg.c, shown with line numbers: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 int main(int argc, char **argv) 6 { 7 if (argc!= 3) { 8 printf("usage: %s <arg1> <arg2>\n", argv[0]); 9 return 1; 10 } 11 12 char *a = *++argv; 13 char *b = *++argv; 14 char c[2] = { 0, 0 }; 15 char *p; 16 char *q; 17 18 for (p = a; *p; p++) { 19 if ( a <= *p && *p <= z ) 20 *c = *p + A - a ; 21 else 22 *c = *p; 23 printf("%s", c); 24 } 25 26 p = (char *)malloc(strlen(a) + strlen(b) + 1); 27 strcpy(p, a); 28 q = p + strlen(a); 29 while ((*q++ = *b++)!= 0) 30 ; 31 printf("%s\n", p); 32 p = NULL; 33 return 0; 34 } It is compiled, linked and run with two command line arguments as follows: $ gcc -g -Wall arg.c $./a.out abc 123 Answer the following questions: 5

Problem [2] continued ---------------------- (a) 15 points What is the output? Assume that the malloc() call was successful. If you think that the program contains bugs (other than memory leaks) that can make the program crash (ex. segmentation fault), write "CAN CRASH" and then write the most likely output if the program happens to run without crashing. (b) 5 points If the program is run using valgrind, how many bytes will valgrind report as "definitely lost"? You can write 0 if you think there is no memory leak that valgrind will consider "definitely lost". Please make sure to write the number of BYTES. 6

Problem [2] continued ---------------------- (c) 5 points Identify the line numbers that contains memory errors other than memory leaks (invalid access, for example.) If you think that there is no memory error other than possible memory leaks, write NONE. You don t have to identify the nature of the memory errors. Just line numbers. (d) 5 points Modify the program to fix ALL memory errors, including memory leaks. Write only the lines that need to be fixed. Write the line number and the line of code that will replace that line. If you think there is nothing to fix, write "NO CHANGE". The program can be memory error free by modifying 0 to 3 lines of code. Please do not modify more than 3 lines of code. 7

Problem [3]: 30 points ----------------------- Consider a mdb database, my-mdb, containing 2 records. We ran mdb-lookup program on it to display the records: $./mdb-lookup my-mdb lookup: 1: {Obama} said {likable enough Hillary} 2: {Romney} said {binders full of women} lookup: Consider the following program, mdb-print.c, which is supposed to display all the records in a given database without taking any user input. It is supposed to do the same thing that the mdb-lookup does when you just press ENTER at the lookup prompt (except that mdb-print will not display the line numbers.) 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include "mylist.h" 4 5 struct MdbRec { 6 char name[16]; 7 char msg[24]; 8 }; 9 10 int main(int argc, char **argv) 11 { 12 FILE *fp = fopen(argv[1], "rb"); 13 14 struct List list; 15 initlist(&list); 16 17 struct Node *node = NULL; 18 struct MdbRec *p; 19 20 p = (struct MdbRec *)malloc(sizeof(struct MdbRec)); 21 while (fread(p, sizeof(struct MdbRec), 1, fp) == 1) { 22 node = addafter(&list, node, p); 23 } 24 25 while ((p = (struct MdbRec *)popfront(&list))!= NULL) { 26 printf("{%s} said {%s}\n", p->name, p->msg); 27 } 28 29 fclose(fp); 30 return 0; 31 } Suppose that you compiled, linked, and ran the mdb-print program with my-mdb argument. Assume that the program ran without any I/O error, that all malloc() calls succeed, and that there is no memory error other than memory leaks. The program ran without crashing. (I removed all error checking code to make the program easier to read. Also, a note on using fread() and an excerpt from mylist.h is included at the end of this problem.) 8

Problem [3]: continued ----------------------- The program, however, may or may not contain memory leaks, and also, may or may not produce the desired output. (a) 10 points What is the output? If you think it will produce the desired output (i.e. same as mdb-lookup when you press ENTER, except the line numbers), then simply write "SAME AS MDB-LOOKUP". (b) 5 points If the program is run using valgrind, how many bytes will valgrind report as "definitely lost"? You can write 0 if you think there is no memory leak that valgrind will consider "definitely lost". Please make sure to write the number of BYTES. 9

Problem [3]: continued ----------------------- (c) 15 points The program, as I said, may or may not contain memory leaks, and also, may or may not produce the desired output. Modify the program so that it produces the desired output and it contains no memory leaks. Write "NO CHANGE" if you think no modification is needed. There is another requirement for making modifications. You can only add new lines of code. You cannot modify or remove any existing lines of code. For each line you add, clearly identify the two existing line numbers that your new line will go in between. The program can be corrected by adding 0 to 5 lines of code. Please do not add more than 5 lines of code. 10

// How to use fread(): /* size_t fread(void *p, size_t size, size_t n, FILE *file) * * - reads n objects, each size bytes long, from file * into the memory location pointed to by p. * * - returns the number of objects successfully read, which may be * less than the requested number n if the end of file has been * reached or an I/O error has occurred. */ // An excerpt from mylist.h: struct Node { void *data; struct Node *next; }; struct List { struct Node *head; }; static inline void initlist(struct List *list) { list->head = 0; } /* Create a node that holds the given data pointer, * and add the node to the front of the list. * * Note that this function does not manage the lifetime of the object * pointed to by data. * * It returns the newly created node on success. * It returns NULL if malloc fails. */ struct Node *addfront(struct List *list, void *data); /* Remove the first node from the list, deallocate the memory for the * ndoe, and return the data pointer that was stored in the node. * Returns NULL is the list is empty. */ void *popfront(struct List *list); /* Create a node that holds the given data pointer, * and add the node right after the node passed in as the prevnode * parameter. If prevnode is NULL, this function is equivalent to * addfront(). * * Note that this function does not manage the lifetime of the object * pointed to by data. * * It returns the newly created node on success. * It returns NULL if malloc fails. */ struct Node *addafter(struct List *list, struct Node *prevnode, void *data); 11

Problem [4]: 10 points ----------------------- Consider the following program, sum55.c: #include <stdio.h> int f() { // SOME MAGICAL CODE GOES HERE... } int main() { int i; int sum = 0; for (i = 0; i < 10; i++) sum += f(); } printf("%d\n", sum); return 0; Implement the body of the function f() so that when this program is compiled, linked, and run, it will output "55" as follows: $ gcc -g -Wall sum55.c $./a.out 55 You can only implement the body of f. You CANNOT change the prototype of f. You CANNOT add any other function. You CANNOT call any library function from the body of f. 12

[blank page] 13

[blank page] 14

[blank page] 15