CSc 328, Spring 2004 Final Examination May 12, 2004

Similar documents
CSc 328, Spring 2004 Final Examination Solutions

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

CE221 Programming in C++ Part 1 Introduction

CSc 372, Fall 2001 Final Examination December 14, 2001 READ THIS FIRST

CSc 372, Fall 1996 Mid-Term Examination Monday, October 21, 1996 READ THIS FIRST

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Absolute C++ Walter Savitch

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Interview Questions of C++

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

CS201 Latest Solved MCQs

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

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

CSc 451, Spring 2003 Examination #1 February 25, 2003 READ THIS FIRST

CSE 303: Concepts and Tools for Software Development

III. Classes (Chap. 3)

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

Introduction to Classes

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Operator overloading

AN OVERVIEW OF C++ 1

CSE 333 Midterm Exam July 24, Name UW ID#

Introduction to C++ Systems Programming

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

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Variables. Data Types.

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

CS Final Exam Review Suggestions - Spring 2014

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

CS Exam 2 Study Suggestions

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

7.1 Optional Parameters

Module 10 Inheritance, Virtual Functions, and Polymorphism

Computer Programming : C++

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

Chapter 2 Basic Elements of C++

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

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

C++ (Non for C Programmer) (BT307) 40 Hours

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Problem Solving with C++

Vectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects

Come and join us at WebLyceum

CSC 210, Exam Two Section February 1999

Chapter 2. Procedural Programming

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

CS

Instantiation of Template class

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

An Object Oriented Programming with C

Object Oriented Software Design II

Lecture 14: more class, C++ streams

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

Chapter 15 - C++ As A "Better C"

VALLIAMMAI ENGINEERING COLLEGE

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

CS 376b Computer Vision

COMP322 - Introduction to C++ Lecture 01 - Introduction

Object-Oriented Programming for Scientific Computing

CS304 Object Oriented Programming Final Term

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

For Teacher's Use Only Q No Total Q No Q No

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

2 nd Week Lecture Notes

Full file at

CS201 Some Important Definitions

Ch. 12: Operator Overloading

Function Overloading

CPSC 427: Object-Oriented Programming

Introduction to Programming

1 Shyam sir JAVA Notes

2 ADT Programming User-defined abstract data types

CS24 Week 3 Lecture 1

C++ Quick Guide. Advertisements

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Object Oriented Programming

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

1 Lexical Considerations

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

PIC 10A Objects/Classes

VIRTUAL FUNCTIONS Chapter 10

CSE 333 Midterm Exam Sample Solution 5/10/13

Outline. 1 About the course

CS250 Final Review Questions

Part X. Advanced C ++

Object-Oriented Design (OOD) and C++

Functions and Recursion

COMP 401 Midterm. Tuesday, Oct 18, pm-3:15pm. Instructions

KLiC C++ Programming. (KLiC Certificate in C++ Programming)

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

COSC 2P95 Lab 5 Object Orientation

Inheritance, and Polymorphism.

Lecture #1. Introduction to Classes and Objects

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

CSC 372 Ruby Mid-term Exam April 11, 2014

Time : 3 hours. Full Marks : 75. Own words as far as practicable. The questions are of equal value. Answer any five questions.

Object-Oriented Principles and Practice / C++

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Transcription:

Name: CSc 328, Spring 2004 Final Examination May 12, 2004 READ THIS FIRST Fill in your name above. Do not turn this page until you are told to begin. Books, and photocopies of pages from books MAY NOT be used during the exam. Electronic devices may not be used during the exam. Everything else, such as the full set of handouts, is permitted. If you run out of room, write on the back of a page. DO NOT use sheets of your own paper. If you have a question you wish to ask, raise your hand and the instructor will come to you. DO NOT leave your seat. If you are unsure about the form or operation of a language construct that is central to a problem's solution, you are strongly encouraged to ask about it. If you have a question that can be safely resolved by making a minor assumption, simply write down that assumption and proceed. Example: "Assuming setw(n) causes the next value to appear in an n-wide field." If a possible assumption seems to make a problem trivial, ask it about it! You need not include any explanation in an answer if you are confident it is correct. However, if an answer is incorrect, any accompanying explanation may help you earn credit, even on multiple choice and true/false questions. If you're completely puzzled on a problem, please ask for a hint. Try to avoid leaving a problem completely blank that will certainly earn no credit. Your C++ code need not have any #includes or using directives save time by leaving them out. Trivial errors in C++ code, such as an omitted semicolon, will not result in any deduction. This is a one hour and forty-five minute (105 minutes) exam with a total of 100 points of regular questions and 15 possible points of extra credit. When told to begin, double-check that your name is at the top of this page, and then put your initials in the lower right hand corner of each page, being sure to check that you have all 17 pages. When you have completed the exam, enter your name on the exam sign-out log and then hand your exam to the instructor. Page 1 of 17

Problem 1: (1 point each, 10 points total) For the following multiple choice questions, circle the best answer. (1) In the statement cout << "hello!" << endl;", endl is a/an: a) extractor b) inserter c) manipulator d) terminator (2) In the definition int Rectangle::getArea() {... }', the '::' is called the a) class binder b) scope resolution operator c) member resolver d) method specifier (3) Consider the following fragment of a Java class definition: class Lamp {... private Bulb itsbulb; } Which of the following is the closest equivalent for itsbulb in C++? (assume all are in a private section): a) Bulb itsbulb; b) Bulb *itsbulb; c) Bulb& itsbulb; d) Bulb& *itsbulb; (4) Consider this fragment of C++ code: char c; while (cin >> c) {... } What is that causes the while loop to be exited at end-of-file on standard input? a) At end-of-file the character c will have the value 0. b) The extraction fails and the failure propagates to the while. c) At end-of-file the expression cin >> c evaluates to 0. d) A break statement is executed in the body of the loop (not shown). (5) Given no other knowledge of a class X, which of the following is the best indicator that X requires a copy constructor? a) The class defines this assignment operator: X& operator=(const X&). b) Memory allocation is performed in the constructor. c) The class has a destructor. d) There is a data member that is a pointer. Page 2 of 17

(6) Default arguments can be used to a) Specify missing arguments for overloaded operators. b) Reduce the number of overloaded functions. c) Control the order of evaluation in a member initialization list. d) All of the above (7) For overloading, the prefix and postfix forms of the increment (++) and decrement operators (--) are distinguished by: a) The postfix keyword. b) Differing numbers of parameters. c) Use of operator+++ and operator--- for the prefix forms. d) There's no need to distinguish the prefix and postfix forms when overloading. (8) Which of the following best describes the ordering of destructor calls for stack-resident objects in a routine: a) The first object created is the first object destroyed; last created is last destroyed. b) The first object destroyed is the last object created; last created is first destroyed. c) Objects are destroyed in the order they appear in memory the object with the lowest memory address is destroyed first. d) The order is undefined and may vary from compiler to compiler. (9) The statement char *p = new char(100); does which of the following? a) Allocates sufficient memory in the heap to hold 100 char values. b) Directs the memory allocator to allocate memory starting at address 100. c) Allocates one byte of memory in the heap and initializes with the character 'd'. d) The statement won't compile. (10) If a, b, and c are of class type V, V + V is overloaded with a free-standing function, and V * V is overloaded with a member function, both returning a value of type V, then the expression a + b * c is equivalent to which of the following? a) c.operator*(operator+(a,b)) b) a.operator+(b.operator*(c)) c) operator*(a.operator+(b),c) d) operator+(a, b.operator*(c)) Page 3 of 17

Problem 2: (1 point each, 5 points total) Indicate whether each of the following statements is true (T) or false (F) In C++ there is no direct analog to this Java class definition: abstract class A { } The string class in the C++ standard library is essentially equivalent to java.lang.string. The semantics associated with the bool type in C++ are essentially the same as the boolean type in Java. A good first step for solving performance problems is to make more extensive use of inline functions. Multiple inheritance provides a good way to define a class that consists of a single instance of several other classes. Problem 3: (2 points each, 10 points total) a) The routine copyints(const int *p1, int *p2, int N) copies N ints starting at one address to another address. In which direction is the data copied: from p1 to p2 or p2 to p1? Justify your answer. b) If a data member is declared as mutable, what does that mean? c) Define a class X that makes the following free standing function compilable: X f() { return 1;} d) What does the const in void X::f() const {... } indicate? e) The instructor often said that in C++, "you only pay for what you use". What did he mean by that? Page 4 of 17

Problem 4: (8 points) In Java, a method f() that requires a parameter of type X and returns void is usually declared like this: void f(x value) In C++, there are several choices for that same situation. Here are four of them: void f(x& value) void f(const X& value) void f(x* p) void f(x value) Briefly describe the meaning of each alternative AND a situation where that alternative would be the best choice. Page 5 of 17

Problem 5: (5 points) The following paragraph is written using terms that a Java programmer might use. Strike out those Java-centric terms and write in the terms that a C++ programmer would most likely use. A common example for teaching inheritance is a hierarchy of geometric shapes. A class named Shape has subclasses like Rectangle and Circle. Methods such as getarea() and getperimeter() provide good examples of abstract methods. To see if students are thinking, an instructor will sometimes propose fields in Shape such as length, width, and radius. Some examples include a class Square, with Rectangle as its superclass, but by the Liskov Substitution Principle that is clearly a bad idea. Problem 6: (5 points) Which element of C++ did you find the most difficult to understand? What did you ultimately find to be the key to understanding that element? Page 6 of 17

Problem 7: (4 points) What is meant by the term "type extensibility"? Name two elements of C++ that support type extensibility. Problem 8: (5 points) In the style of an e-mail message to a friend who knows C and Java, briefly describe one of the following features of C++: (1) destructors, and the guarantee of destruction, (2) operator overloading, (3) IO Streams, (4) templates, (5) multiple inheritance. IMPORTANT: Your "message" should include some examples of code. Page 7 of 17

Problem 9: (3 points) Write a class named Circle such that this program int main() { Circle c(7); } cout << c.radius() << endl; c.radius() = 11; cout << c.radius() << endl; produces this output: 7 11 Problem 10: (5 points) Write a program that prints the integers from 1 through 1000, one per line. RESTRICTION: Your solution may not use any control structures (if, while, for, etc.), goto statements, local variables, or employ recursion. Page 8 of 17

Problem 11: (10 points) In this problem you are to create two overloaded operators that operate on C++ Standard Library strings: (1) Overload string & string to cause the value of the right-hand operand to be appended to the left-hand operand, changing the left-hand operand. (This operation is imperative, not applicative.) string s("520"), dash("-"); s & dash; s & "577" & "-" & "6431"; // s is now '520-', dash is unchanged // s is now '520-577-6431' The left hand operand is returned, providing the ability to "chain" several & operations as shown in the second case. Another example: string s2; for (char *p = "testing"; *p; p++) { s2 & "-" & string(1,*p); // string(1,*p) creates a one-character string from *p } s2 & "-"; cout << s2 << endl; // Output: -t-e-s-t-i-n-g- (2) Overload ~string to produce a copy of the string wrapped in angle-brackets. Example: string s("testing"); cout << ~s << endl; cout << ~~(s + " this") << endl; // Output: <testing>; s is unchanged // Output: <<testing this>> Note: There's no problem overloading tilde, even though that symbol is also used to denote a destructor. The string class may not be changed, of course your solution must use string "as is". Recall that the string class overloads + to represent concatenation and has a string(const char *) constructor, just like the String class developed in the slides. (And that's just about all you need to recall about string to do this problem.) Page 9 of 17

[Space for answer for problem 11] Page 10 of 17

Problem 12: (10 points) Create a templated class named Pair whose instances are an aggregate of two values of possibly different types. For example, Pair<int, string> p1(10, "ten"); creates a Pair named p1 that holds the int 10 and the string "ten". Provide methods geta() and getb() that return the first and second values, respectively. Note that the return types of geta() and getb() depend on the types the template is instantiated with. In this example, the return type of geta() is int and the return type of getb() is string. Be sure to provide an inserter for Pairs. Example: cout << p1 << endl; // Output: <10,ten> Pair should be able to accommodate any two types, call them A and B, as long as both A and B support copy construction and assignment. Do not assume the presence of a default constructor for A or B. Another example (assuming p1 from above): Pair<double, char> p2(3.1, '4'); Pair< Pair<int,string>, Pair<double,char> > p3(p1, p2); cout << p3 << endl; // Output: <<10,ten>,<3.1,4>> There is no interaction between A and B. One might declare Pair<Cat,Dog> pets<c,d>. Page 11 of 17

Problem 13: (15 points) In this problem you are to define a three-class inheritance hierarchy, with the following requirements: There are two types of Containers: Buckets and Boxes. Every Container has an integer serial number. To create a Bucket, its height and radius (both integers), and serial number must be specified. To create a Box, its length, width, and height (integers), serial number, and material type ('m' for metal, 'c' for cardboard) must be specified. A container may be queried for its serial number, volume, and whether it can hold a fluid. Metal boxes and all buckets can hold fluids; cardboard boxes cannot hold fluids. Additionally, Container is to have a static member function totalfluidcapacity() that accepts a collection of Containers and calculates the total fluid capacity of all the containers that are capable of holding fluids. Think of these requirements as coming from your manager; all the other decisions, such as data representation, are up to you. Note that one key problem is how to pass the collection of containers to totalfluidcapacity(). Hint: Here's a very bad way: int totalfluidcapacity(container [ ]) // Don't do this! 2 The volume of a bucket is *radius *height. Use M_PI for. Page 12 of 17

[Additional space for answer for problem 13] Page 13 of 17

Problem 14: (5 points) This problem explores your understanding of when objects are created and destroyed, and where they reside. At each of the points of execution that are marked with a series of dashes, enter the number of instances of X that exist at that moment. Note that separate totals are maintained for stack-resident and heap-resident objects. The first pair of entries has been completed as an example. int main() { X x1; - - - - - - - - - - Stack: 1 Heap: 0 X *p = new X[3]; f(p); NOTE: The following totals are AFTER f(p) returns! } void f(x *arg) { X p(*arg); delete arg; X *xp = 0; delete xp; X a[3]; a[1] = a[2]; X **a2 = new X*[5]; Page 14 of 17

(Totals from previous page:) Stack: Heap: for (int i = 1; i <= 3; i++) { X x1; a2[i] = new X; } delete a2[1]; delete a2[2]; delete [] a2; } (Now go back and do the last one in main().) Extra Credit Section (1 point each unless otherwise noted) (1) What is "cfront"? (2) Name a C++ reserved word that really isn't needed, and describe how a C++ programmer could work around its absence. (Ignore the problem of existing code that might use it.) (3) A person telling a C++ programmer about Java says this: "Think of it this way: In Java, they are always explicit." What is the "they" that's being referred to? (4) Offer a GOOD definition of the term "memory leak". Page 15 of 17

(5) What do the # and ## preprocessor operators do? (6) Why did Stroustrup choose the terms "base class" and "derived class" instead of "superclass" and "subclass"? (7) How many lectures were there? (8) Answer any one of these three: At what university did Stan Lippman earn a graduate degree in English? Who had the first Original Thought of the semester? Who asked for coffee and doughnuts for the final exam? (9) (3 points) For problem 14, which asks you to calculate the number of instances existing at various points in time, the instructor wrote a program, docounts, that takes a C++ program as an input file and produces a listing essentially identical to that shown in problem 14 but with the counts filled in. The C++ code did contain an indication of each of the points where the counts were to be calculated. Here is the task for you: Sketch out the design for a program like docounts. Assume that the reader is a skilled developer. Page 16 of 17

(10) (4 points) Consider the following header file, Rectangle.h: #ifndef _Rectangle_h_ #define _Rectangle_h_ class Rectangle { public: Rectangle(double width, double height); double getarea(); //... private: double itswidth, itsheight; }; inline double Rectangle::getArea() { return itswidth * itsheight; } #endif Imagining a large system in which the Rectangle class is widely used, what problem will result if the reserved word inline (underlined) on Rectangle::getArea() is removed? Page 17 of 17