For the next three questions, consider the class declaration: Member function implementations put inline to save space.



Similar documents
Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

C++FA 5.1 PRACTICE MID-TERM EXAM

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Computer Programming C++ Classes and Objects 15 th Lecture

Passing 1D arrays to functions.

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

J a v a Quiz (Unit 3, Test 0 Practice)

Comp151. Definitions & Declarations

Formatting Numbers with C++ Output Streams

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

N3458: Simple Database Integration in C++11

PIC 10A. Lecture 7: Graphics II and intro to the if statement

Ch 7-1. Object-Oriented Programming and Classes

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

Compiler Construction

Sample CSE8A midterm Multiple Choice (circle one)

C++ Input/Output: Streams

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

! " # $ %& %' ( ) ) *%%+, -..*/ *%%+ - 0 ) 1 2 1

Moving from CS 61A Scheme to CS 61B Java

C++ Programming Language

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

Chapter One Introduction to Programming

sqlpp11 - An SQL Library Worthy of Modern C++

Appendix K Introduction to Microsoft Visual C++ 6.0

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Member Functions of the istream Class

Software Engineering Concepts: Testing. Pointers & Dynamic Allocation. CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009

Common Beginner C++ Programming Mistakes

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

The Payroll Program. Payroll

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

The if Statement and Practice Problems

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

Help on the Embedded Software Block

CSI33 Data Structures

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

7.7 Case Study: Calculating Depreciation

CISC 181 Project 3 Designing Classes for Bank Accounts

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Chapter 5 Functions. Introducing Functions

Sources: On the Web: Slides will be available on:

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

CS107L Handout 02 Autumn 2007 October 5, 2007 Copy Constructor and operator=

CORBA Programming with TAOX11. The C++11 CORBA Implementation

While Loop. 6. Iteration

Note: Syntactically, a ; is needed at the end of a struct definition.

C++ INTERVIEW QUESTIONS

Chapter 8 Selection 8-1

Moving from C++ to VBA

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Appendix M: Introduction to Microsoft Visual C Express Edition

Binary storage of graphs and related data

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

vector vec double # in # cl in ude <s ude tdexcept> tdexcept> // std::ou std t_of ::ou _range t_of class class V Vector { ector {

Chapter 5. Selection 5-1

How to Write a Checker in 24 Hours

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

13 Classes & Objects with Constructors/Destructors

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

CS107L Handout 04 Autumn 2007 October 19, 2007 Custom STL-Like Containers and Iterators

Conditions & Boolean Expressions

Class 16: Function Parameters and Polymorphism

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

MPLAB Harmony System Service Libraries Help

Why you shouldn't use set (and what you should use instead) Matt Austern

Part I. Multiple Choice Questions (2 points each):

Reading and Writing PCD Files The PCD File Format The Grabber Interface Writing a Custom Grabber PCL :: I/O. Suat Gedikli, Nico Blodow

Coding conventions and C++-style

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Semantic Analysis: Types and Type Checking

Introduction to Python

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

Answers to Review Questions Chapter 7

COMPUTER SCIENCE 1999 (Delhi Board)

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Lecture 2 Notes: Flow of Control

F ahrenheit = 9 Celsius + 32

5 CLASSES CHAPTER. 5.1 Object-Oriented and Procedural Programming. 5.2 Classes and Objects 5.3 Sample Application: A Clock Class

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

(Eng. Hayam Reda Seireg) Sheet Java

/* File: blkcopy.c. size_t n


C++ program structure Variable Declarations

C++ Language Tutorial

6. Control Structures

Transcription:

Instructions: This homework assignment focuses on basic facts regarding classes in C++. Submit your answers via the Curator System as OQ4. For the next three questions, consider the class declaration: class CreditCard { CreditCard(double Amount) { Bal = Amount; void Pay( double Amount ) { Bal -= Amount; void Charge( double Amount ) { Bal += Amount; double Balance() const { return Bal; Member function implementations put inline to save space. double Bal; ; 1. Which member function provides an observer/reporter operation? 1) Pay 2) Charge 3) Balance 4) 1 and 2 only 5) 1, 2, and 3 2. Which member function provides a mutator operation? 1) Pay 2) Charge 3) Balance 4) 1 and 2 only 5) 1, 2, and 3 3. Consider the following statements, assuming the class declaration for CreditCard is in scope: CreditCard WesternUnion(1000.0); CreditCard BankAmerica(0.0); WesternUnion.Pay(400.0); // line 1 cout << WesternUnion.Bal; // 2 BankAmerica = WesternUnion; // 3 CreditCard.Charge(120.0); // 4 Which statements would not cause compilation errors? 1) 1 only 2) 2 only 3) 3 only 4) 4 only 5) 1 and 3 only 6) 1 and 4 only 8) 2 and 4 only 9) 2, 3 and 4 only 10) None of these 4. Which of the following statements about C++ classes is false? 1) Classes can have private member functions. 2) Classes can have both public and private data members. 3) By default, members of classes are private. 4) A class may have no data members. 5) Aggregate assignment (shallow) is automatically supported for classes., (all are true). 1

5. Which of the following C++ built-in operations are not automatically defined for class objects? 1) = 2) == 3) << 4) None are auto-defined. 5) All are auto-defined. 6) 1 and 2 only 7) 1 and 3 only 8) 2 and 3 only 6. A class A has a public member function G that takes one int parameter for input, returns a bool value, and does not modify any of the class data members. Which of the following would be the best correct function prototype for G in the class declaration? 1) bool G(int& x); 2) bool G(int x) const; 3) const bool G(int x); 4) bool G(int& x) const; 5) const bool G(int& x); 7. Suppose that the declaration of the class A includes the following function prototype. bool LessThan( const A& RHS ); Which of the following tests in the client code correctly compares two A objects named Alpha and Beta? 1) if (Alpha < Beta) 2) if (LessThan(Alpha, Beta)) 3) if (Alpha.LessThan(Beta)) 4) if (Alpha.LessThan.Beta) 5) if (LessThan(Alpha).Beta) 8. If the designer of a C++ class wishes to allow clients to inspect and modify a data member, what is the best approach? 1) Make the data member private and provide a public observer function as a class member. 2) Make the data member private and provide a public mutator function as a class member. 3) Declare the data to be public, not private. 4) Provide a public constructor that takes a value for that data member as a parameter. 5) Make the data member private and provide a public mutator function and a public observer function as class members. 6) Do nothing because it is not acceptable to let clients modify data members. 7) None of these Consider the following class: class B { int S; B() { B(int inits) { S = inits; ; 9. Assuming everything necessary is in scope, consider the declaration: B Foo; What is the value of Foo.S? 1) 0 2) Unknown 3) The declaration isn't allowed. 4) None of these 2

10. Assuming everything necessary is in scope, what is the value of Bar.S after executing the code fragment: B Foo(17), Bar(32); Bar = Foo; 1) 17 2) 32 3) Unknown 4) The assignment isn't allowed. 5) None of these For the next six questions, consider the class declaration: class Farey { int Top, Bottom; Farey(); Farey(int T, int B); Farey operator+(const Farey& RHS) const; Farey operator-(const Farey& RHS) const; bool operator==(const Farey& RHS) const; void Display(ostream& Out) const; ; Farey::Farey() { Top = Bottom = 0; Farey::Farey(int T, int B) { Top = T; Bottom = B; Farey Farey::operator+(const Farey& RHS) const { return Farey(Top + RHS.Top, Bottom + RHS.Bottom); Farey Farey::operator-(const Farey& RHS) const { return Farey(Top - RHS.Top, Bottom - RHS.Bottom); bool Farey::operator==(const Farey& RHS) const { The addition operation here is sometimes called "Football player addition". It does have a legitimate application. return ( (Top == RHS.Top) && (Bottom == RHS.Bottom) ); void Farey::Display(ostream& Out) const { Out << Top << '/' << Bottom; 3

Again, assuming everything necessary is in scope, consider the following code fragment: Farey A(3, 5), B(1, 4), C(2, 4), D(0, 5), E; E = A + B; // line 1 A.Display(cout); // 2 E = A + B C; // 3 E = 2*A; // 4 11. After the execution of line 1, what are the values of E.Top and E.Bottom, respectively? 1) 0 and 0 2) 3 and 5 3) 4 and 9 4) 9 and 4 5) Unknown 12. What is written to the stream cout when line 2 is executed? 1) "3/5" 2) Nothing 3) None of these 13. After the execution of line 3, what are the values of E.Top and E.Bottom, respectively? 1) 0 and 0 2) 2 and 5 3) 4 and 9 4) The statement isn't allowed. 5) Unknown 14. After the execution of line 4, what are the values of E.Top and E.Bottom, respectively? 1) 6 and 10 2) 6 and 5 3) 3 and 10 4) The statement isn't allowed. 5) Unknown Consider the following code fragment: Farey X(1, 2), Y(2, 4); if ( X == Y ) // line 5 cout << "X == Y" << endl; else cout << "X!= Y" << endl; if ( X + X == Y ) // line 6 cout << "X + X == Y" << endl; else cout << "X + X!= Y" << endl; 15. When the if statement beginning in line 5 is executed, what is written to cout? 1) "X == Y" 2) "X!= Y" 16. When the if statement beginning in line 6 is executed, what is written to cout? 1) "X + X == Y" 2) "X + X!= Y" 4

For the next five questions, consider the following class: class Quadratic { double Coefficient[3]; Quadratic(double a = 0.0, double b = 0.0, double c = 0.0); double Evaluate(double x) const; ; Quadratic::Quadratic(double a, double b, double c) { Coefficient[0] = a; Coefficient[1] = b; Coefficient[2] = c; double Quadratic::Evaluate(double x) const { return ( (Coefficient[2]*x + Coefficient[1])*x + Coefficient[0] ); 17. Given the declaration: Quadratic F(3, 2, 1); What value is output by the statement: cout << F.Evaluate(2); 1) 6 2) 11 3) 17 4) None of these 18. Given the declaration: Quadratic G(1); What value is output by the statement: cout << G.Evaluate(2); 1) 1 2) 4 3) Not allowed. 4) None of these A designer wants to add an addition operation to the class Quadratic. Consider the partial implementation: Quadratic::operator+(const Quadratic& RHS) const { // line 1 double a = Coefficient[0] + RHS.Coefficient[0]; double b = Coefficient[1] + RHS.Coefficient[1]; double c = Coefficient[2] + RHS.Coefficient[2]; return ; // line 2 19. How should the blank in line 1 be filled? 1) void 2) Quadratic 3) Quadratic& 4) 2 or 3 only 5) It should be left blank. 20. How should the blank in line 2 be filled? 1) Quadratic(c, b, a) 2) RHS 3) *this 4) Quadratic(a, b, c) 5) It should be left blank. 5

21. The algebraic expression in the member function Evaluate() could have been written as: Coefficient[2]*x*x + Coefficient[1]*x + Coefficient[0] Why was the approach shown above not used? 1) It isn't the right algebraic expression. 2) The original version required typing fewer characters. 3) The original version requires fewer operations to evaluate. 8) None of these For the next six questions, consider the following class declaration: enum Response {PRODUCT, CHECKPRICE, OUTOFSTOCK; class Vendor { unsigned int Stock; // number of units available unsigned int Balance; // total payments received unsigned int Price; // unit price for product std::string passcode; // security passcode unsigned int Capacity; // max units vendor can hold Vendor(); Vendor(unsigned int Stk, unsigned int P, string Code); Response MakePurchase(unsigned int Payment); void addproduct(unsigned int Quantity, string Code); unsigned int removemoney(string Code); bool changeprice(unsigned int P, string Code); ~Vendor(); ; For the next three questions, consider implementing the member function MakePurchase(). Response Vendor::MakePurchase(unsigned int Payment) { if ( ) return CHECKPRICE; // correct amount? if ( ) return OUTOFSTOCK; // stock available? Stock--; // update stock Balance += Payment; // record payment return ; // return product 22. The purpose of the first if statement is to make sure that the correct amount of money has been offered. A Vendor does not support making change. How should the blank in that line be filled? 1) Payment < Price 2) Payment!= Price 3) Payment!= Vendor.Price 4) There is no correct way to do this. 5) None of these 6

23. The purpose of the second if statement is to make sure that there is an item in stock to dispense in return for payment. How should the blank in that line be filled? 1) Stock!= 0 2) Stock <= Capacity 3) Vendor.Stock == 0 4) There is no correct way to do this. 5) None of these 24. What value should be returned in the final statement? 1) CHECKPRICE 2) OUTOFSTOCK 3) PRODUCT 4) There is no correct way to do this. 5) None of these For the next question, consider an implementation the member function changeprice(): bool Vendor::changePrice(unsigned int P, string Code) { if ( Code == passcode ) { // authenticate caller Price = P; // reset price return true; // confirm reset return false; // deny reset 25. What design features of Vendor support restricting who can change the price of an item? 1) Making passcode private. 2) Not providing a mutator for passcode. 3) Providing changeprice() as public. 8) None of these 26. Which of the following would be logically and syntactically valid declarations of Vendor objects? Vendor M0000; // 1 Vendor M0001(100, 50, "wowonfie"); // 2 Vendor M0002(300, 100, "", 300); // 3 1) 1 only 2) 2 only 3) 3 only 4) All of them 8) None of these 27. Which of the following are possible consequences of the choice that the implementation of Vendor uses unsigned int instead of int for some data members? 1) It reduces the amount of memory a Vendor object requires. 2) It eliminates the possibility of logical errors if parameters with negative values were passed in calls to member functions. 3) It reduces the amount of code needed for member functions. 4) All of them are true. 5) All of them are false. 6) 1 and 2 only 7) 1 and 3 only 8) 2 and 3 only 9) None of these 7

For the next three questions, consider the following class that represents a todo list. Tasks are represented by string objects, and stored in an array of dimension 100. Tasks are always added at the tail of the list, and removed from the front. To make the use of the array efficient, it is used in a circular manner. const unsigned int SIZE = 100; class todolist { todolist(); bool addtask(const string& Task); string getnexttask() const; bool deltask(); void Display(ostream& Out) const; void Clear(); ~todolist(); // set up empty task list // add a task to the list // see what the next task is // remove the next task // see the whole list // remove all the tasks unsigned int putnext; // index where next task will go unsigned int donext; // index of next task to do unsigned int ntasks; // number of tasks in the list string List[SIZE]; // list of tasks ; 28. What, if anything, is wrong with the following implementation of the member function to add a new task to the list? bool todolist::addtask(const string& Task) { List[putNext] = Task; putnext = (putnext + 1) % SIZE; ntasks++; return true; 1) If the list is full, it will replace the first uncompleted task with a new one. 2) It updates the index putnext incorrectly. 3) It doesn't need a return value, since it always returns the same thing. 8) Something not listed 9) Nothing is wrong 29. What, if anything, is wrong with the following implementation of the member function to delete a task from the list? bool todolist::deltask() { if ( ntasks == 0 ) return false; donext = ( donext + SIZE 1 ) % SIZE; ntasks--; return true; 1) If the list is empty, it will cause a runtime error. 2) It updates the index donext incorrectly. 3) It should be declared as a const member function. 8) Something not listed 9) Nothing is wrong 8

30. What, if anything, is wrong with the following implementation of the member function to display the tasks in the list? void todolist::display(ostream& Out) const { if ( ntasks == 0 ) Out << "Todo list is empty." << endl; else { for (unsigned int Pos = 0; Pos < ntasks; Pos++) { Out << setw(3) << Pos << ": "; Out << List[Pos] << endl; Pos++; 1) It doesn't necessarily start with the next task to do. 2) It doesn't necessarily stop with the last task to do. 3) It doesn't necessarily print all of the tasks waiting to be done. 8) None of these 9