CSE 333 Midterm Exam 5/10/13

Similar documents
CSE 333 Midterm Exam Sample Solution 5/10/13

CSE 333 Midterm Exam 2/14/14

CSE 333 Midterm Exam July 24, Name UW ID#

CSE 333 Midterm Exam 7/25/16. Name UW ID#

CSE 333 Midterm Exam 7/29/13

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

CSE 333 Midterm Exam 7/22/12

CSE 333 Midterm Exam 2/12/16. Name UW ID#

CSE 333 Midterm Exam 5/9/14 Sample Solution

CSE 333 Midterm Exam 7/27/15 Sample Solution

CSE au Midterm Exam Nov. 2, 2018 Sample Solution

CSE 333 Final Exam 3/19/14

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

CSE 333 Midterm Exam 7/25/16 Sample Solution. Question 1. (10 points) Preprocessor. Suppose we have the following two files:

CSE 374 Final Exam 3/15/17. Name UW ID#

CSE 374 Midterm Exam 11/2/15. Name Id #

CSE 303 Midterm Exam

CSE 333 Midterm Exam July 24, 2017 Sample Solution

Common Misunderstandings from Exam 1 Material

Creating a String Data Type in C

CSE 333 Midterm Exam Sample Solution 7/28/14

CSE 333 Midterm Exam Nov. 3, Name UW ID#

CSE 303, Winter 2006, Final Examination 16 March Please do not turn the page until everyone is ready.

CSE 331 Midterm Exam 2/13/12

CSE351 Winter 2016, Final Examination March 16, 2016

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

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

CSE 333 Midterm Exam Sample Solution 7/29/13

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

C Introduction. Comparison w/ Java, Memory Model, and Pointers

ComS 228 Exam 1. September 27, 2004

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

Final CSE 131B Spring 2004

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

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

CSCI-1200 Data Structures Spring 2013 Lecture 7 Templated Classes & Vector Implementation

// Initially NULL, points to the dynamically allocated array of bytes. uint8_t *data;

CSE 303 Final Exam. March 16, 2009

ECE 15B COMPUTER ORGANIZATION

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

C0MP1911 Final Exam 1337 Computing 1

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

CSE wi Midterm Exam 2/8/18. Name UW ID #

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

ECE264 Spring 2013 Final Exam, April 30, 2013

CSE 333 Autumn 2013 Midterm

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Pointers, Dynamic Data, and Reference Types

CSE 331 Final Exam 12/9/13

CSE 374 Midterm Exam Sample Solution 2/11/13. Question 1. (8 points) What does each of the following bash commands do?

EE 355 Lab 4 - Party Like A Char Star

Question 1. [15 marks]

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

ECE264 Spring 2014 Exam 2, March 11, 2014

Signature: ECE 551 Midterm Exam

CSE P 501 Exam 12/1/11

Computer Science Foundation Exam

Final Exam 1 /12 2 /12 3 /10 4 /7 5 /4 6 /10 7 /8 8 /9 9 /8 10 /11 11 /8 12 /10 13 /9 14 /13 15 /10 16 /10 17 /12. Faculty of Computer Science

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

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

QUIZ. Can you find 5 errors in this code?

Exercise Session 2 Simon Gerber

CSE 374 Final Exam Sample Solution 3/15/12

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

DAY 3. CS3600, Northeastern University. Alan Mislove

CS 103 Lab 6 - Party Like A Char Star

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

Midterm II CS164, Spring 2006

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS 61c: Great Ideas in Computer Architecture

CS61C : Machine Structures

Ch. 11: References & the Copy-Constructor. - continued -

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

CSE wi Final Exam 3/12/18. Name UW ID#

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

CSE 333 Final Exam June 6, 2017 Sample Solution

REFERENCES, POINTERS AND STRUCTS

CS 103 Lab - Party Like A Char Star

CSE P 501 Exam 8/5/04

CSE 303, Spring 2006, Final Examination 6 June Please do not turn the page until everyone is ready.

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Name: I. 20 II. 20 III. 20 IV. 40. CMSC 341 Section 01 Fall 2016 Data Structures Exam 1. Instructions: 1. This is a closed-book, closed-notes exam.

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

CSE 401 Final Exam. December 16, 2010

CSE 331 Final Exam 3/12/12

PRINCIPLES OF OPERATING SYSTEMS

PRACTICE MIDTERM EXAM #2

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Lecture 2: C Programm

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

The University of Nottingham

Personal SE. Functions, Arrays, Strings & Command Line Arguments

CS 223: Data Structures and Programming Techniques. Exam 2

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

Memory Corruption 101 From Primitives to Exploit

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

CSE 374 Midterm Exam Sample Solution 2/6/12

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

ECE551 Midterm Version 1

Transcription:

Name There are 5 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes, closed electronics, closed telepathy, open mind. If you don t remember the exact syntax for something, make the best attempt you can. We will make allowances when grading. Don t be alarmed if there seems to be more space than is needed for your answers we tried to include more than enough blank space. Relax, you are here to learn. Please wait to turn the page until everyone is told to begin. Score / 100 1. / 18 2. / 20 3. / 22 4. / 22 5. / 18 Page 1 of 8

Question 1. (18 points) Consider these two C files: a.c void f(int p); int main() { f(17); return 0; b.c void f(char *p) { *p = 'x'; (a) Why is the program made from a.c and b.c incorrect? What would you expect to happen if it is executed? (b) Will gcc -Wall -c a.c and gcc -Wall -c b.c give an error or will they successfully produce a.o and b.o without complaint? (c) Will gcc -Wall a.c b.c give an error or will it successfully produce a.out without complaint? (d) How would you use standard C coding practices (using an extra file) to avoid the problems with this program (or at least detect them properly)? Give the contents of that extra file below and explain what modifications should be made to a.c and/or b.c, if any. Page 2 of 8

Question 2. (20 points) Consider the following rather twisted C program, which does compile and execute with no warnings or errors: #include <stdio.h> int *conundrum(int *a, int b, int **c) { **c = b + *a; *c = a; **c = *a * b; // HERE return a; int main() { int p = 2, q = 3, r = 7; int *d = &r; d = conundrum(&p, q, &d); printf("%d %d %d %d\n", p, q, r, *d); return 0; (a) Draw a boxes n arrows diagram showing state of memory when control reaches the comment containing HERE, right before executing the return statement in function conundrum. Your diagram should have two boxes showing the stack frames for functions main and conundrum. The stack frames should include values of integer variables and an arrow from each pointer to the location that it references. Then answer part (b) at the bottom of the page. (b) What output does this program produce when it is executed? Page 3 of 8

Question 3. (22 points) The nodes in a linked list of C strings can be defined as follows: typedef struct snode { char * str; // this node s heap-allocated string struct snode * next; // next node in the list or NULL Snode; Complete the definition of function Clone below so that it returns (a pointer to) an exact duplicate of the list that is its argument, including duplicates of all the nodes and strings in the original list. You may use strcpy instead of strncpy, and may assume that all strings in the original list are properly \0-terminated. You may assume that malloc will always successfully allocate data when it is called. Also assume that all necessary library header files have already been #included. Hint: strcpy(dst,src) copies the string src to the string dst. // return a clone of the linked list with first node lst // (which may be NULL) Snode * Clone(Snode * lst) { (additional room on the next page if needed) Page 4 of 8

Question 3. (cont.) Additional room for your answer if you need it. Page 5 of 8

Question 4. (22 points) For this problem consider the C++ class Vec on the next page. This class is supposed to implement a simple vector of integers. All of the code is written in the class declaration instead of in separate.h and.cc files to get it to fit on one page for the exam. The class has a constructor, copy constructor, assignment, methods to set and get individual elements from the vector, and a destructor. The assignment and copy constructor operations are supposed to make a complete copy (clone) of their argument. The class compiles without any errors or warnings, but when it is used in a program it generally segfaults, and, even if it doesn t crash, valgrind reports all sorts of memory management problems. Mark the code on the following page to identify the problems, and write in corrections so the class will work as intended. Keep your notes brief and to the point. You should ignore possible problems with invalid index values or length arguments i.e., assume the length provided to the constructor is positive and that the index arguments to get and set are within bounds (i.e., 0 <= index < len_). You can also assume that heap allocation (new) always succeeds. (There are enough other problems in the code without worrying about these possibilities. ) You can use the space below if you need extra room to write explanations or corrections, but please help the graders by making it easy to read your changes and figure out where they fit. Page 6 of 8

Question 4. (cont.) Find the bugs in the C++ code below, give a very brief description of the problems, and correct the code so it works properly. class Vec { // a vector of integers public: // initialize new Vec with n elements all set to 0. Assume n>0. Vec(int n) { v_ = new int[n]; len_ = n; for (int i=0; i<len_; i++) v_[i] = 0; // copy constructor - initialize *this to be a clone of other Vec(const Vec &other) { v_ = other.v_; len_ = other.len_; // destructor free resources ~Vec() { delete v_; // replace the contents of *this with a clone of rhs Vec &operator=(const Vec &rhs) { delete v_; v_ = rhs.v_; len_ = rhs.len_; return *this; // get/set functions. Assume that 0<=index<len_ (i.e., for this // question don't worry about index out of bounds problems) int get(int index) const { return v_[index]; void set(int index, int n) { v_[index] = n; private: int* v_; int len_; ; // array of int values allocated on the heap // number of ints in v_ Page 7 of 8

Question 5. (18 points) A few short questions to wrap up. For each question circle the correct choice. You do not need to justify your answers. (a) If a is declared as char a[5], then a[3]==*(a+3) is always true. (circle) (b) If a is declared as int32_t a[5], then a[3]==*(a+12) is always true. (Recall that a int32_t value occupies 4 bytes.) (circle) (c) If we execute program xyzzy using the command line./xyzzy one two, then the value of argv[0] in the main program will be the string one. (circle) (d) If on our Linux system a program contains a pointer variable declared as int *p and in the debugger we see that the value of p is 0x7fffffffe318, we can conclude that: (circle the best choice) (i) p refers to a variable allocated on the heap (ii) p refers to a local variable in some function s stack frame (iii) p refers to constant data or to a location in the program s x86 machine code (iv) p is NULL (v) we cannot conclude anything about the data referenced by p (e) The system-level read function normally returns the number of bytes read. If it returns the value -1 (error), then the program should not attempt any further I/O operations on that stream because it would be a fatal error to do so. (circle) (f) The stat system function returns information about a file. That information includes a field st_mode that, among other things, describes the type of the file. All of these files are classified as regular files as opposed to some other type: ascii text, Word.docx files, jpeg picture files, compiler produced.o files, and executable files like a.out. (circle) Page 8 of 8