CSE 333 Midterm Exam 7/22/12

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

CSE 333 Midterm Exam 2/14/14

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

CSE 333 Midterm Exam 5/10/13

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

CSE 333 Midterm Exam July 24, Name UW ID#

CSE 333 Midterm Exam 7/29/13

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

CSE 333 Final Exam 3/19/14

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

CSE 331 Midterm Exam 2/13/12

CSE 333 Midterm Exam Sample Solution 5/10/13

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

CSE au Midterm Exam Nov. 2, 2018 Sample Solution

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

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

CSE 333 Midterm Exam Sample Solution 7/28/14

QUIZ. What are 3 differences between C and C++ const variables?

CSE 333 Autumn 2013 Midterm

CSE 333 Lecture 6 - data structures

CSE 333 Midterm Exam 5/9/14 Sample Solution

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

CSE 333 Lecture 5 - data structures & modules

CSE 303, Autumn 2006, Final Examination 12 December 2006

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

CSE 333 Lecture 6 - data structures

Kurt Schmidt. October 30, 2018

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

CSE 333 Midterm Exam July 24, 2017 Sample Solution

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

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

CSC111 Computer Science II

Question 1. [15 marks]

CSE 331 Final Exam 12/9/13

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

QUIZ. Source:

Common Misunderstandings from Exam 1 Material

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

Object Oriented Design

CSE 303 Midterm Exam

CSE P 501 Exam 8/5/04

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

CSE 333 Lecture 7 - final C details

CE221 Programming in C++ Part 1 Introduction

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

CS 0449 Sample Midterm

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Pointers and References

ECE264 Spring 2013 Exam 1, February 14, 2013

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

The University of Nottingham

ECE 462 Exam 1. 6:30-7:30PM, September 22, 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Lab 2: ADT Design & Implementation

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

A brief introduction to C++

The University of Nottingham

CSE 374 Programming Concepts & Tools

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

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

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

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 303, Spring 2005, Midterm Examination 29 April Please do not turn the page until everyone is ready.

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

Fast Introduction to Object Oriented Programming and C++

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

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

CS 104 (Spring 2014) Final Exam 05/09/2014

ComS 228 Exam 1. September 27, 2004

CSE 333 Lecture C++ final details, networks

Lecture Notes on Generic Data Structures

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

PIC 10A Objects/Classes

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

CSE 331 Final Exam 3/12/12

CSE 374 Final Exam Sample Solution 6/10/10

Data Structures and Modules

377 Student Guide to C++

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

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

Tutorial 1 C Tutorial: Pointers, Strings, Exec

Arrays and Pointers in C. Alan L. Cox

Review Questions for Final Exam

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

CSE 333 Autumn 2014 Midterm Key

4. Structure of a C++ program

Final CSE 131B Spring 2004

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

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

CSE 333 Final Exam Sample Solution 3/19/14

CSE 303 Final Exam. March 16, 2009

CSE 333 Lecture 9 - intro to C++

There are, of course, many other possible solutions and, if done correctly, those received full credit.

QUIZ. What is wrong with this code that uses default arguments?

5) Attacker causes damage Different to gaining control. For example, the attacker might quit after gaining control.

Transcription:

Name There are 6 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, no signal flares, etc. If you don t remember the exact syntax of 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. / 20 2. / 17 3. / 20 4. / 20 5. / 22 6. / 1 Page 1 of 9

Question 1. (20 points) A little C programming. A palindrome is a string that reads the same forwards or backwards. For instance, madam, abba, and x are palindromes, while ab, and foo are not. You are to complete a function to determine if a string is a palindrome. For this question, a string must be exactly the same forward and backward to be a palindrome, including whitespace (so the string nurses run is not a palindrome here). We will also consider an empty string (length 0) to be a palindrome. Complete the definition of function IsPalindrome below so it returns 1 (true) if its string argument is a palindrome and returns 0 (false) if it is not. You may assume that the function argument is a properly \0-terminated C string. You may use any of the C string library functions in <string.h>. You may not copy or modify the string only examine it. #include <string.h> // Return 1 if s is a palindrome, otherwise return 0. // If the string has length 0, return 1 (true). int IsPalindrome(char *s) { Page 2 of 9

Question 2. (17 points) Preprocessor madness. Suppose we have the following code in file hdr.h: #ifndef _HDR_H_ #define _HDR_H_ #define BAZ 17 int func(int n); #endif // _HDR_H_ Then suppose we have the following code in file hdr.c: #include "hdr.h" #define BAR 42 #include "hdr.h" int main(int argc, char **argv) { int n = BAR; int f = func(baz+bar); return n+1; Below, write the exact output produced by the C preprocessor when it reads and processes the file hdr.c. In other words, what is the output that is generated by the preprocessor and sent as input to the compiler when we use gcc to compile file hdr.c? Page 3 of 9

Question 3. (20 points) A touch of class. The following simple C++ class Int has an integer instance variable and various constructors, methods (functions) and a destructor. The various functions write output that includes the value of the instance variable ival when they are executed. #include <iostream> using namespace std; class Int { public: Int() { ival = 17; cout<<"aaa("<<ival<<")"<<endl; Int(int n) { ival = n; cout<<"bbb("<<ival<<")"<<endl; Int(const Int &n) { ival = n.ival; cout<<"ccc("<<ival<<")"<<endl; ~Int() { cout<<"ddd("<<ival<<")"<<endl; int get() const { cout<<"eee("<<ival<<")"<<endl; return ival; void set(int n) { ival = n; cout<<"fff("<<ival<<")"<<endl; private: int ival; ; What output is produced when we run the following program that uses this class? If there is more than one possible sequence in which the output appears, write down any one of the possible output sequences. int main(int argc, char **argv) { Int p; Int q(p); Int r(5); q.set(p.get()+1); Output: Page 4 of 9

Question 4. (20 points) Bugs, bugs, bugs. The following C program compiles and links without errors, but it produces segfaults when we run it. #include <stdio.h> #include <stdlib.h> #include <string.h> // linked list node for a list of c-strings typedef struct node { char * data; // string data in this node struct node * next; // next node or NULL if none Node; // print strings in list that starts at head void prlist(node * head) { Node * p = head; while (p!= NULL) { printf("%s\n", p->data); p = p->next; // add x to front of strlist and return pointer to new list head Node * push_node(node x, Node * strlist) { x.next = strlist; return &x; // link two nodes together as a list and then print the list int main(int argc, char ** argv) { Node n1; Node n2; Node * list = NULL; // head of linked list or NULL if empty // copy "world" to first node and push onto front of list strcpy(n1.data, "world"); list = push_node(n1, list); // copy "hello" to second node and push onto front of list strcpy (n2.data, "hello"); list = push_node(n2,list); // print list prlist(list); return EXIT_SUCCESS; (continued on next page) Page 5 of 9

Question 4. (cont.) Describe the error(s) in the code on the previous page, and describe minimal changes that would allow the program to execute successfully. Your changes should not alter the general structure of the program, i.e., it should still link the two nodes in the main program into a linked list and print it. Just describe the changes needed to allow the program to execute successfully. You can either annotate the code on the previous page to show the problems and corrections, or describe them below. Page 6 of 9

Question 5. (22 points) The question with an alarming amount of reading. This question concerns that hash table and linked list data structures from HW1 and HW2. You should have a separate set of pages with listings of the header files ll.h, hashtable.h, and hashtable_priv.h, which you should reference as needed when writing the code for this question. We would like to add a function AvgBucketLength to hashtable.c that computes the average number of elements in the non-empty buckets in the hash table. This is not the same as the load factor (recall that the load factor is a simple average of the total number of elements in the table divided by the number of buckets that s not what we re after here). For example, suppose we have a table with 5 buckets where three buckets are empty, one of the others contains 3 elements, and the last one contains 6 elements. Here we have 2 buckets that are not empty and 9 elements in the non-empty buckets, so the average number of elements in the non-empty buckets is 4.5 (9/2). Complete the definition of function AvgBucketLength on the next page. Besides being correct, your code should be written in reasonably good style (although we will make allowances for the fact that this is an exam). As much as possible, use good variable names and write neatly to make it easier to understand your code. You should use appropriate interfaces defined in ll.h to examine the linked lists in the hash table buckets, and be sure to declare appropriate variables, use casts as needed, and so forth. Page 7 of 9

Question 5. (cont.) Complete the definition of function AvgBucketLength below. Assume that this function is being added to hashtable.c, and that all of the necessary #includes are already present. You probably won t need all of the available space for your answer. // Return the average number of items stored in non-empty buckets // in the HashTable table. Return 0.0 if the hash table is empty. double AvgBucketLength(HashTable table) { Page 8 of 9

Question 6. (512>>9 points) The One True Editor for programming is a) ed b) vi/vim c) emacs d) pico e) nano f) genie g) teco h) notepad i) notepad++ j) Word k) cat l) butterfly m) None of the above. The correct answer is. n) I really don t care. Just give me my free point, please. o) I really do care, but just give me my free point, please. Page 9 of 9