Circle all of the following which would make sense as the function prototype.

Similar documents
Abstract Data Types. Development and Implementation. Well-defined representations that allow objects to be created and used in an intuitive manner

CS 215 Fundamentals of Programming II Spring 2011 Project 2

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

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

CS 247: Software Engineering Principles. ADT Design

Due Date: See Blackboard

istream& operator >> (istream &sin, Widget &object); ostream& operator << (ostream &sout, Widget &object);

Overloading Operators in C++

A <Basic> C++ Course

A <Basic> C++ Course

Lab 2: ADT Design & Implementation

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

Lecture 7. Log into Linux New documents posted to course webpage

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

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

Programming Language. Functions. Eng. Anis Nazer First Semester

EE 152 Advanced Programming LAB 7

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Computer Programming : C++

Due Date: See Blackboard

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

Object-Oriented Design (OOD) and C++

Ch 8. Operator Overloading, Friends, and References

Review. What is const member data? By what mechanism is const enforced? How do we initialize it? How do we initialize it?

Lecture 3 ADT and C++ Classes (II)

CSCE 110 PROGRAMMING FUNDAMENTALS

CSci 1113 Final. Name: Student ID:

Programming. C++ Basics

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan

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

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Use the dot operator to access a member of a specific object.

Fundamentals of Programming CS-110. Lecture 2

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

Set Implementation Version 1

Introduction to Programming using C++

Function Overloading

(3) Some memory that holds a value of a given type. (8) The basic unit of addressing in most computers.

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

CSI33 Data Structures

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Object Oriented Programming COP3330 / CGS5409

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

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

CSCE 206: Structured Programming in C++

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

Getting started with C++ (Part 2)

IS0020 Program Design and Software Tools Midterm, Fall, 2004

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

pointers & references

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

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

CSci 1113 Final. Name: Student ID:

Introduction to the C++ Programming Language

EECE.3220: Data Structures Spring 2017

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.

PIC 10A. Lecture 17: Classes III, overloading

III. Classes (Chap. 3)

Lecture 2. Binary Trees & Implementations. Yusuf Pisan

Note 11/13/2014. They are like those i s, j s, and temp s that appear and disappear when the function starts and finishes...

Object-Oriented Programming in C++

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 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Introduction to C++ (Extensions to C)


More Advanced Class Concepts

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

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

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

REVIEW. The C++ Programming Language. CS 151 Review #2

CSE 333 Midterm Exam July 24, Name UW ID#

Introduction Of Classes ( OOPS )

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

CS 247: Software Engineering Principles. Modules

Operator overloading

Polymorphism Part 1 1

Shahram Rahatlou. Computing Methods in Physics. Overloading Operators friend functions static data and methods

Review Questions for Final Exam

CSCE 110 PROGRAMMING FUNDAMENTALS

Instantiation of Template class

CE221 Programming in C++ Part 1 Introduction

CSC 126 FINAL EXAMINATION FINAL Spring 2012 B. Name (last, First) Instructor. Total Possible. Received

Introduction to Programming EC-105. Lecture 2

Module Operator Overloading and Type Conversion. Table of Contents

The following is a typical execution run of this program:

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science. Instructor: Final Exam Fall 2011

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

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

C++ STREAMS; INHERITANCE AS

Ch 2 ADTs and C++ Classes

CS Spring 05 - MidTerm

ADTs & Classes. An introduction

Introduction to C++ Systems Programming

Module 7 b. -Namespaces -Exceptions handling

pointers + memory double x; string a; int x; main overhead int y; main overhead

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

Transcription:

Student ID: Lab Section: This test is closed book, closed notes. Points for each question are shown inside [ ] brackets at the beginning of each question. You should assume that, for all quoted program segments, the appropriate header files are included and the standard namespace is used. Remember to write out and sign the pledge at the end of the test. 1. [1] The parameters in a function invocation are called the parameters. The parameters are represented in the invoked function by the parameters. 2. [1] The values of the formal parameters and other objects defined in a function are kept in its. 3. [1] A description of a function s interface, which is a valid C++ statement, is called a 4. [1] A object is an object defined outside of any function interface, or any other block structure. 5. [1] C++ supports two forms of parameter passing: and. 6. [1] A modifier applied to a parameter declaration indicates that the function may not change the object. 7. [1] parameters can be specified as trailing parameters only. 8. [1] When a function name or an operator symbol can cause diferent actions, depending on its parameters or operands, it is said to be. 9. [1] A constructor is a constructor that requires no parameters. 10. [5] Suppose we have a function that is supposed to double the value of an integer variable, and the resulting value is to be used in the invoking expression. The body of the function consists only of the following: value = value * 2; Circle all of the following which would make sense as the function prototype. void double(int &value); int twice(int value); void int by_two(int &value); int value(); void double_val(int &value); Spring 1999 Page 1 of 11 Pledged

11. [6] What is the output of the following program segment? int test(int x) if (x > 3) return 1; else int y = 6; while (test(y + 1) == 1) cout << y << " "; y = y - 1; cout << endl; 12. [5] Consider the following function curse: int curse(int n) if (n <= 0) return 1; else return n * curse(n-2); What is the output of the following program fragment? cout << curse(5) << endl; Spring 1999 Page 2 of 11 Pledged

13. [9] What is the output of the following program? void nasty(int &x, int y, int &z); int a = 10; int b = 20; int c = 30; nasty(a, b, c); cout << a << " " << b << " " << c << endl; void nasty(int &x, int y, int &z) cout << x << " " << y << " " << z << endl; x = 1; y = 2; z = 3; cout << x << " " << y << " " << z << endl; 14. [2] What output will be produced by the following code, assuming it is embedded in a complete program? int first_choice = 1; switch(first_choice + 1) case 1: cout << "Roast Beef\n"; break; case 2: cout << "Roast Worms\n"; break; case 3: cout << "Roast Ice Cream\n"; break; default: cout << "Eat at Wendy s\n"; 15. [2] What output will be produced by the code above if the first line were replaced by: int first_choice = 3; Spring 1999 Page 3 of 11 Pledged

16. [9] What does the following program segment print? void func(); int i = 2; int j = 3; int k = 4; int m = 5; cout << i + j << " " << k << " " << m << endl; func(); cout << i + j << " " << k << " " << m << endl; void func() int m = i + j; j = i + m; k = m * m; 17. [4] What is the output of the following? void one(int &x) x = x + 1; void two(int x) x = x + 2; int x = 3; one(x); cout << x << " "; two(x); cout << x << endl; Spring 1999 Page 4 of 11 Pledged

The following code will be used in the next 3 questions. In each of these questions show the output produced when the code is executed. If the code segment will not compile, briefly state the reason for this. Here is the definition of a class of rectangular objects - the class Rect (don t confuse this class with class RectangleShape from the textbook): class Rect public: Rect(); Rect( int j, int k); int get_l(); int get_w(); int area(); private: int length; int width; ; // now the definitions of the member functions Rect::Rect() length = -234; width = 0; Rect::Rect(int j, int k) length = j; width = k; int Rect::get_l() return length; int Rect::get_w() return width; int Rect::area() return (length * width); Spring 1999 Page 5 of 11 Pledged

18. [3] Consider the following main function: Rect a(4, 7); cout << a.length << endl; 19. [4] Here is another main function: Rect box(6, 9); int x = box.area(); cout << x << " " << box.get_l() << endl; 20. [4] And here is a third: Rect alpha; Rect beta(1, 2); int one = alpha.get_l() + beta.get_l(); int two = alpha.get_w() + beta.get_w(); cout << one << " " << two << endl; Spring 1999 Page 6 of 11 Pledged

The definitions in the following header file, named rational.h,are in effect for the remaining questions #ifndef RATIONAL_H #define RATIONAL_H class Rational public: // member functions and operator Rational(); Rational(const int numer, const int denom); protected: int GetNumerator() const; int GetDenominator() const; void SetNumerator(const int numer); void SetDenominator(const int denom); private: int Numerator; int Denominator; ; Rational operator+(const Rational &r, const Rational &s); Rational operator*(const Rational &r, const Rational &s); bool operator!=(const Rational &r, const Rational &s); ostream& operator<<(ostream &sout, const Rational &r); istream& operator>>(istream &sin, Rational &r); #endif 21. [3] In no more than two short sentences, explain why the #ifndef, #define, and #endif directives appear in the include file. Don t explain how they work 22. [2] The const in the prototypes of functions GetNumerator() and GetDenominator() in class Rational indicates that: (a) (b) (c) (d) The functions will not modify their actual parameters. The functions will return a constant value. The functions will not modify the invoking object. None of the above. Spring 1999 Page 7 of 11 Pledged

23. [2] The const in the prototypes of functions SetNumerator() and SetDenominator() in class Rational indicates that: (a) (b) (c) (d) The functions will not modify their actual parameters. The functions will return a constant value. The functions will not modify the invoking object. None of the above. 24. [5] The code below defines the overloading of operator!= so that it returns true if and only if the rational numbers represented by its two operands are not equivalent. Otherwise it is to return the value false. bool operator!=(const Rational &r, const Rational &s) int a = r.getnumerator(); int b = r.getdenominator(); int c = s.getnumerator(); int d = s.getdenominator(); return X; Write here the code to replace X which will make this function perform correctly: (Note that 3/6 is equivalent to 1/2) 25. [4] The overloading of!= in the question above defines that its its parameters are to be const Rational &. In one sentence each, explain: (a) the parameter is a reference (&) parameter. Why do we pass non-fundamental objects by reference instead of by value? Spring 1999 Page 8 of 11 Pledged

(b) why, in addition, is it made a const parameter? 26. [10] Using the functions whose interfaces are provided in the include file rational.h, write a complete member function Reciprocal that returns the Rational reciprocal of the invoking object. Recall that for a Rational object, the reciprocal of a/b is b/a.do not compute the reciprocal using the division operation! Function Reciprocal is to be a member function. Write your function below. Spring 1999 Page 9 of 11 Pledged

27. [12] Using the class Rational as defined, together with the auxilliary functions, write a complete program that computes the sum of the rational numbers 1/2 to 1/n where n is an integer that is prompted for and read by the program. Your program should use an assert statement to ensure that the input typed by the user is greater than 1 and less than 51. Assume that the << operator, defined as an auxilliary function, works as described in the text. The final output of the program should be the line: The sum of 1/2 through 1/n is X where n is the value inputted from the keyboard and X is the computed sum. Your program will be graded based on its correctness and style. Do not include comments in your program. Write your program on the next page. We have included all necessary header files. Also, remember to write out and sign the pledge at the bottom of the next page. Spring 1999 Page 10 of 11 Pledged

Write your code here: #include <iostream> #include <assert.h> #include "rational.h" using namespace std; int main( ) Write out and sign the Pledge here: Spring 1999 Page 11 of 11 Pledged