CS 251 Practice Final Exam R. Brown May 1, 2015 SHOW YOUR WORK No work may mean no credit Point totals will be adjusted to a 120 point scale later

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

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):

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

Name Section: M/W or T/TH. True or False (14 Points)

True or False (15 Points)

Midterm Exam 5 April 20, 2015

True or False (12 Points)

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

Computer Programming

Name SECTION: 12:45 2:20. True or False (12 Points)

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

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

2. It is possible for a structure variable to be a member of another structure variable.

CS250 Final Review Questions

Object Reference and Memory Allocation. Questions:

CS250 Final Review Questions

CS250 Final Review Questions

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

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

CSE 303, Autumn 2006, Final Examination 12 December 2006

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

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

CSE 303: Concepts and Tools for Software Development

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

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Classes in C++98 and C++11

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

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

Multiple Choice Questions (20 questions * 5 points per question = 100 points)

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

CS201- Introduction to Programming Current Quizzes

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Multiple Choice Questions (20 questions * 6 points per question = 120 points)

Constructor - example

CS 376b Computer Vision

Week 3: Pointers (Part 2)

Common Misunderstandings from Exam 1 Material

True or False (15 Points)

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Linked List using a Sentinel

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

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Name. CPTR246 Spring '17 (100 total points) Exam 2

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Part I: Short Answer (12 questions, 65 points total)

C++_ MARKS 40 MIN

Review Questions for Final Exam

1. Which of the following best describes the situation after Line 1 has been executed?

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

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

CS201 Some Important Definitions

Introduction Of Classes ( OOPS )

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15


CS 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

CS24 Week 3 Lecture 1

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

Short Notes of CS201

Programming in C++: Assignment Week 8

The American University in Cairo Department of Computer Science & Engineeringt CSCI &09 Dr. KHALIL Exam-I Fall 2009

CS201 - Introduction to Programming Glossary By

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Integer Data Types. Data Type. Data Types. int, short int, long int

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Pointers. Variable Declaration. Chapter 10

! 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)

Exercise 1.1 Hello world

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

Definition Matching (10 Points)

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

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

CPSC 427: Object-Oriented Programming

University of Michigan EECS 183: Elem. Programming Concepts Fall 2011 Exam 1: Part 1: Form 1. Professors: ML Dorf, Elliot Soloway

CS 247: Software Engineering Principles. ADT Design

CPSC 427: Object-Oriented Programming

/************************************************ * CSC 309/404 Review for Final -- Solutions * ************************************************/

! 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)

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

Homework #3 CS2255 Fall 2012

Lab 1: First Steps in C++ - Eclipse

CS101 PLEDGED SPRING 2001

CS 161 Exam II Winter 2018 FORM 1

University of Toronto

CSE341 Spring 2017, Final Examination June 8, 2017

a. a * c - 10 = b. a % b + (a * d) + 7 =

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

CMPS 221 Sample Final

True or False (14 Points)

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

Midterm Exam #2 Spring (1:00-3:00pm, Friday, March 15)

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

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

Transcription:

Name CS 251 Practice Final Exam R. Brown May 1, 2015 SHOW YOUR WORK No work may mean no credit Point totals will be adjusted to a 120 point scale later I pledge my honor that I have neither given nor received assistance during this exam, and that I have seen no dishonest work. Signed I have intentionally not signed the pledge (check only if appropriate) (6 pts) 1. Write a definition of a C++ function doublestring that satisfies the following specification. doublestring 1 Argument: A string (null-terminated array of characters). State change: If arg1 is not an empty string, a string with twice the length of arg1 is allocated dynamically, and assigned to hold two complete copies of the characters in arg1. Return: If arg1 is not an empty string, return the doubled string that was allocated and assigned. Otherwise, return 0. For example, the call doublestring("hello") should return a newly allocated string "hellohello", and doublestring("") should return 0.

CS 251 Practice Final Exam R. Brown May 1, 2015 Page 2 (9 pts) 2. Draw a memory diagram for the following complete C++ program, using the conventions presented in class, including output. Note: No definition is provided for the method makestring. Assume that makestring satisfies the spec on the following page, and show any calls to makestring, butomit the details of those calls that could only be known by having an implementation. #include <iostream> using namespace std; class Name { protected: char *name; public: Name(const char *n) { name = makestring(n); } Name() { name = makestring(""); } char *getname() { return name; } char modify(int num) { name[0] += num; return name[0]; } char *makestring(const char *str); }; struct Item : public Name { double price; double func(double x, double &y) { double saved = y; y = y + x; return saved; } int main() { double x = 2.5; char *y = new char[5]; y[1] = A ; Item it("cat", 4.1); y[2] = it.modify(2); Name nm(it.getname()); y[3] = nm.modify(3); cout << it.price << it.getname() << nm.getname() << endl; } public: Item(const char *n, double p) { name = makestring(n); price = p; } char modify(int num) { price = price * num; name[0] += num; return name[0]; } };

CS 251 Practice Final Exam R. Brown May 1, 2015 Page 3 (Additional answer space for memory diagram problem) makestring (method) 1 Argument: A C-style string (null-terminated array of characters). State change: A newly allocated copy of arg1 is created. Return: C-style string, that newly allocated copy of str.

CS 251 Practice Final Exam R. Brown May 1, 2015 Page 4 (6 pts) 3. Answer one of the following questions. Summarize your answer in words; provide an example if desired. Clearly indicate which part you are skipping. a) The key rule of dynamic memory management is that every new should have exactly one delete. What would go wrong if some call of new had no corresponding call of delete? What if a single call of new had more than one delete call? or b) What are some advantages of programming with subclasses? Include an example where a subclass of a class C would be more appropriate than having a state variable of type C. (12 pts) 4. On the next pages, define and implement the class Taxi described by the accompanying spec. Define and implement the entire class Taxi. ImplementtheTaxi default constructor within the Taxi class definition (inline), and implement the display() method and the helper function makestring outside of that class definition. Other required members may be implemented within or outside that class definition. Do not define the superclass Car nor any of its members. Use C-style strings (char * type, null-terminated) with dynamic allocation and correct memory management. Use the helper function makestring() appropriately in the code you write. Call the Car assignment operator within your definition of the Taxi assignment operator, and call the Car display() method within your definition of the Taxi display() method. Use const appropriately. arguments. Use reference types whenever passing objects as

CS 251 Practice Final Exam R. Brown May 1, 2015 Page 5 (Solution of implementation problem)

CS 251 Practice Final Exam R. Brown May 1, 2015 Page 6 (Additional answer space for implementation problem)

Taxi, An example class representing a taxicab R. Brown Class Taxi 12/5/14 (R Represents a taxicab. Superclass: Car State Variables for Class Taxi: driver C-style string (null-terminated array of character) (e.g., "Jim Doe"), the driver for this taxicab. rate Float (e.g., 2.20), the rate per mile of this taxicab. Constructors for Class Taxi: Taxi Taxi Taxi Constructor cr Car object. dr C-style string, driver for this instance of Taxi. rt Float, rate per mile for this instance of Taxi. State Change: This instance of Taxi is initialized by copying values from cr to the corresponding Car state variables in this Taxi object, with new allocation where appropriate, assigning a newly allocated copy of dr to the state variable driver, and assigning rt to the state variable rate. Default constructor None. State Change: This instance of Taxi is initialized by applying the default constructor for Car to assign values to the Car state variables in this Taxi, assigning a newly allocated empty string to the state variable driver, and assigning 0.00 to the state variable rate. Copy constructor tx An instance of Taxi. State Change: This instance of Taxi is initialized by copying the Car state variables in this Taxi from the corresponding Car state variables in tx, and assigning the state variables driver and rate from the corresponding Taxi state variables in tx, with new allocation where appropriate. Brown): In tial entry. Over... 1

Destructor for Class Taxi: ~Taxi Destructor None. State Change: Memory is deallocated that was dynamically allocated for this instance of Taxi but not for the superclass Car. Methods for Class Taxi: = Assignment operator tx Another instance of Taxi. State Change: The state variables for this instance of Taxi are assigned copies of the values of the corresponding state variables in the argument tx, with new allocation where appropriate. Return Value: This instance of Taxi. setrate rt Float. State Change: The value rt is assigned to the state variable rate. setdriver dr C-style string. State Change: A newly allocated copy of dr is assigned to the state variable driver. display None State Change: The state variables of this instance of Taxi are printed on standard output in the format year make (driver, $rate) Example: 2014 Ford (Jim Doe, $2.20) makestring Helper method str A C-style string. State Change: A newly allocated copy of str is created. Return Value: C-style string, that newly allocated copy of str. 2

Car, An example class representing an automobile R. Brown Class Car 12/5/14 (R Represents an automobile. Superclass: None State Variables for Class Car: Brown): In tial entry. make year C-style string (null-terminated array of character) (e.g., "Ford"), the manufacturer for this automobile. Integer (e.g., 2014), the model year of this automobile. Constructors for Class Car: Car Car Car Constructor mk C-style string, manufacturer for this instance of Car. yr Integer, model year for this instance of Car. State Change: This instance of Car is initialized by assigning a newly allocated copy of mk to the state variable make, and assigning yr to the state variable year. Default constructor None. State Change: This instance of Car is initialized by assigning a newly allocated empty string to the state variable make, and assigning 1 to the state variable year. Copy constructor cr An instance of Car. State Change: This instance of Car is initialized by assigning the state variables make and year from the corresponding Car state variables in cr, with new allocation where appropriate. Destructor for Class Car: ~Car Destructor None. State Change: Memory is deallocated that was dynamically allocated for this instance of Car. Over... 1

Methods for Class Car: = Assignment operator cr Another instance of Car. State Change: The state variables for this instance of Car are assigned copies of the values of the corresponding state variables in the argument cr, with new allocation where appropriate. Return Value: This instance of Car. getmake None. Return Value: C-style string, the value of the state variable make. getyear None. Return Value: Integer value of the state variable year. setmake mk C-style string. State Change: A newly allocated copy of mk is assigned to the state variable make. setyear yr Integer. State Change: The value yr is assigned to the state variable year. display None State Change: The state variables of this instance of Car are printed on standard output in the format year make Example: 2014 Ford 2