ComS 228 Exam 1. September 27, 2004

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

Introduction to Computer Science Midterm 3 Fall, Points

Midterm Exam. Sample Solutions

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

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001

CSE 333 Midterm Exam 5/10/13

CSE 333 Midterm Exam July 24, Name UW ID#

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

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

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

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

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.

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

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

Review for Test 1 (Chapter 1-5)

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

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

University of Maryland Baltimore County. CMSC 202 Computer Science II. Fall Mid-Term Exam. Sections

CSCS 261 Programming Concepts Exam 2 Fall EXAM 2 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

Common Misunderstandings from Exam 1 Material

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

Concepts (this lecture) CSC 143. Classes with Dynamically Allocated Data. List of ints w/dups (cont.) Example: List of ints with dups.

CS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren

Review Questions for Final Exam

CS201- Introduction to Programming Current Quizzes

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

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

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

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

CS Fall 2018 Homework Assignment 3 Due: 23:59, Dec 26, 2018

CS201 Some Important Definitions

CS32 - Week 1. Umut Oztok. June 24, Umut Oztok CS32 - Week 1

BEng (Hons) Telecommunications. Examinations for 2016 / Semester 2

C:\Temp\Templates. Download This PDF From The Web Site

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Dynamic Data Structures

Advanced Systems Programming

Exam 3 Chapters 7 & 9

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

CSE143 Exam with answers MIDTERM #1, 1/26/2001 Problem numbering may differ from the test as given.

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

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

CSCS 261 Programming Concepts Exam 2 Fall EXAM 2 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

FORM 2 (Please put your name and form # on the scantron!!!!)

CSCE 110 PROGRAMMING FUNDAMENTALS

6. Pointers, Structs, and Arrays. 1. Juli 2011

CSC 210, Exam Two Section February 1999

CSCI 123 Introduction to Programming Concepts in C++

CS24 Week 4 Lecture 1

Question: How can we compare two objects of a given class to see if they are the same? Is it legal to do: Rectangle r(0,0,3,3);,, Rectangle s(0,0,4,4)

Data Structures and Algorithms

University of Utah School of Computing

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

CSE au Midterm Exam Nov. 2, 2018 Sample Solution

Object oriented programming

CSE 333 Midterm Exam Sample Solution 5/10/13

CSE030 Fall 2012 Final Exam Friday, December 14, PM

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

CSCE 206: Structured Programming in C++

การทดลองท 8_2 Editor Buffer Array Implementation

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

Object Oriented Design

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

CSE 333 Midterm Exam July 24, 2017 Sample Solution

Linked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil

Topics: Material through example 19 (types, operators, expressions, functions, selection, loops, arrays)

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

Variables, Memory and Pointers

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

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Classes in C++98 and C++11

Intermediate Programming & Design (C++) Classes in C++

Assignment 4: SkipList

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

CS 162, Lecture 25: Exam II Review. 30 May 2018

CSE 333 Midterm Exam 2/14/14

CS Spring 2018 Homework Assignment 3 Due: 23:59, May 7, 2018

More class design with C++ Starting Savitch Chap. 11

Homework #3 CS2255 Fall 2012

Come and join us at WebLyceum

Suppose that the following is from a correct C++ program:

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

C++ - Lesson 2 This is a function prototype. a' is a function that takes an integer array argument and returns an integer pointer.

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

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Midterm Examination. Instructor: Gary Chan Date: Saturday, 23 October 2010 Time: 2:30pm 4:00pm Venue: LTC

CS201 Latest Solved MCQs

CMPT 135: Midterm Answer Key

CS 2604 Homework 1 C++ Review Summer I 2003

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

PIC 10A. Lecture 17: Classes III, overloading

EECE.3220: Data Structures Spring 2017

Programming, numerics and optimization

CS 61B, Spring 1996 Midterm #1 Professor M. Clancy

Hong Kong University of Science and Technology COMP151: Object Oriented Programming

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Question: Total Points: Score:

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

Transcription:

ComS 228 Exam 1 September 27, 2004 Name: University ID: Section: (10 percent penalty if incorrect) This is a one-hour, closed-book, closed-notes, closed-calculator exam. The exam consists of 9 pages (including this one) and 6 questions; make sure your exam has all the pages and questions before you begin. Read each question carefully and provide clear and concise answers. Use correct C++ syntax when asked to write code. Please write neatly; illegible answers will get an automatic zero. If you need more room, you may write on the backs of the pages, but clearly indicate you have done so. To ensure fairness, everyone will be given the same amount of time, so you must stop writing when time is called. If you do not stop writing when instructed, you will receive a 25% deduction in your score. Possible Points Your Score Question 1 12 Question 2 12 Question 3 25 Question 4 12 Question 5 9 Question 6 30 Total 100 1

1. (12 points) On the lines below, give the output of the following code: void foo(double* d); double* bar(double*& d); int main() double* d1, d2; *d1 = 4.2; d2 = 2.4; double* d3; d3 = &d2; cout << Main Start: << *d1 <<, << d2 <<, << *d3 << endl; foo(d1); cout << After Foo: << *d1 <<, << d2 <<, << d3 << endl; d1 = bar(d3); cout << After Bar: << *d1 <<, << d2 <<, << d3 << endl; void foo(double* d) *d = 1.1; d = new double; *d = 2.2; cout << Inside Foo: << *d << endl; double* bar(double*& d) *d = 3.3; d = new double; *d = 5.5; cout << Inside Bar: << *d << endl; return d; output line 1: output line 2: output line 3: output line 4: output line 5: 2

2. (12 points) Consider the following example class and answer the questions below. class MyExample public: void func1(myexample& other); void func2(myexample& other) const; void func3(const MyExample& other) const; void func4(const MyExample& other); private: int value; ; Will the following implementations compile? If not, give a short explanation why. a) void MyExample::func1(MyExample& other) value = 2; other.func3(*this); b) void MyExample::func2(MyExample& other) const other.func1(other); func3(*this); c) void MyExample::func3(const MyExample& other) const other.func3(*this); value = 13; d) void MyExample:: func4(const MyExample& other) func3(other); other.func1(*this); 3

3. (25 points) In this problem you will design a C++ class to represent an electronic stopwatch. Today s stopwatches do a lot more than just measuring the time from start to finish, this stopwatch has a number of different modes. Mode 1 shows the current wall-clock time for the stopwatch to double as a simple watch. The time is stored as an integer counting 1/100 th s of a second from 8 pm, Sep 27 th 2004. Mode 2 is the stopwatch mode, where it measures the time from the start to the end of a race. In addition to the main running time, it can keep times for up to 10 laps. For each lap it stores the time for the lap as well as the cumulative time from the start of the race to the end of the lap. For training purposes it is possible to store the single lap times and the whole race time in secondary buffer members. These buffers are used in mode 3, which is a countdown mode. In countdown mode it counts down the race time from the stored time, and it also counts down the times of the individual laps starting at the first one, to support a simple comparison whether the run is better than the stored one. Times are stored as integers, counting in 1/100 th second steps. The system has a hardware timer that calls a parameterless member function of the StopWatch class every 1/100 th of a second, to update all the class s times. The stopwatch has 3 buttons (left, middle, right), which control all the functions. Pressing/releasing these buttons causes one of three Button-functions to be caleed. Each of these three functions gets a bool parameter indicating whether the button is pressed (true) or released (false). To drive the actual display, which has one line with exactly 40 characters, the class needs to be able to produce a simple character array with the numbers to show. It is returned as a char * to an internal buffer that is big enough for a single line, and that is kept in the class. On the next page, write the class header file for a class called StopWatch. Don t forget to write the needed preprocessor directives. In the class definition, include member variables to store all the state information and member function prototypes to perform all the actions described above. In addition to the default constructor, write a constructor that takes the current wall-clock time as its argument, to initialize the clock when the stopwatch is turned on. Note: you are writing the header file only in this problem. You do not need to write any function implementations. You also do not need to include comments or pre/post conditions - unless any of your variable, arguments, function names, or function return types would be unclear to the graders. 4

(Problem 3 continued) 5

4. (12 points) Please write down the computational complexity (big O) of the following functions: a) int afunc(int n) int m = 16; int p = n / 2; int result = 0; for(int i = 0; i < n; ++i) m *= 2; for(int a = 0; a < n; ++a) for (int b = 0; b < p; ++b) result += m * n * b * a; return result; b) int bfunc(int n) int m = n * log(n); int p = n - 2; int result = 0; for(int a = 0; a < 15; ++a) for (int b = p; b < n; ++b) result += n b + a; return result; c) int cfunc(int n) int m = 1; int p = n - 2; int result = 0; for(int i = 0; i < n; ++i) m *= 2; for(int a = 0; a < m; ++a) for (int b = 0; b < 12; ++b) result += n + b - a; return result; 6

5. (9 points) Consider the following class: class Widget public: Widget(); //Sets the two values to zero Widget(int x, int y); //Sets the first value to x and the second to y int value(); //Returns the sum of the first and second values void print() const; //Prints out the two values to cout private: int firstval, secondval; ; In this problem, you will write an overloaded < operator as a member function of class Widget. If the client programmer has created two Widget objects w1 and w2, then he wants to be able to compare them using an expression like w1 < w2. Widgets are compared by adding their two values together and comparing the result; i.e., w1 would be less than w2 if the sum of firstval and secondval from w1 is less than the sum of firstval and secondval from w2. a) In the space below, write the prototype for the < operator. Also, write down where in the code you would put the prototype. b) In the space below, write the implementation for the < operator. Also, write down where in the code you would put the implementation. 7

6. (30points) A set is a data structure that holds a list of data items that does not contain duplicates. The order in which the items are listed doesn t matter, so typically, the implementation of a set will keep them sorted in ascending order. Sets can grow and shrink in size, so one way to implement them is to store the items in a dynamic array. In this problem, you will be writing member functions for a class that represents a set of integers. You can find the class definition of IntSet on the last page of this exam, which you may tear off to refer to as you work on these problems. a) In the space below, implement the destructor for IntSet. Don t forget to include the function header. b) In the space below, implement the assignment operator for IntSet. Don t forget to include the function header. 8

c) The unionwith() function for IntSet takes another IntSet and adds all the items that appear in the other IntSet to the items in this IntSet. However, items will not be duplicated; if the item was already in this IntSet, then it will not be added again. For example, suppose IntSet s contains 2,4,6,8 and IntSet t contains 2,5,7,8,12. Then suppose the function s.unionwith(t); is called. Afterwards, s will contain 2,4,5,6,7,8,12. In the space below, implement the unionwith() member function for IntSet. Don t forget to include the function header. You may not use the addint() function to help you write this one; instead, work with the dynamic arrays directly. 9

class IntSet public: IntSet(); //Post: this IntSet is an empty set of integers IntSet(const IntSet& other); //Post: if the other IntSet is empty, then so is this one. Otherwise, //this IntSet is a deep copy of the other IntSet ~IntSet(); //Post: if this IntSet is not empty, then the memory allocated to this //IntSet is reclaimed IntSet& operator=(const IntSet& other); //Post: if the other IntSet is an empty IntSet, then this IntSet becomes //an empty IntSet too. Otherwise, if this IntSet and the other IntSet are //not the same object, then this IntSet is a deep copy of the other //IntSet. bool isin(int num) const; //Post: returns true if num is in this IntSet void addint(int newnum); //Post: If newnum was not already in this IntSet, it has been added to //this IntSet void removeint(int num); //Post: If num is the only integer in this IntSet, then this IntSet //becomes empty. Otherwise, num is removed from this IntSet. void unionwith(const IntSet& other); //Post: If this IntSet is empty, then this IntSet is a deep copy of //other. Otherwise, the integers in the other IntSet but which are not //already in this IntSet have all been added to this IntSet. private: int* theints; //The pointer to the dynamic array containing the list of //integers that are in this IntSet. The integers are //listed in increasing order. If this IntSet is empty, //the pointer is NULL. int size; //The number of integers in the dynamic array. ; 10