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

Similar documents
Dynamic Allocation of Memory

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

CS201- Introduction to Programming Current Quizzes

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Common Misunderstandings from Exam 1 Material

Assignment of Structs

CA31-1K DIS. Pointers. TA: You Lu

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Memory Leak. C++: Memory Problems. Memory Leak. Memory Leak. Pointer Ownership. Memory Leak

CSE 303: Concepts and Tools for Software Development

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

Come and join us at WebLyceum

04-19 Discussion Notes

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

Object-Oriented Principles and Practice / C++

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Dynamic Allocation of Memory

Ch. 12: Operator Overloading

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

CSE 333 Midterm Exam 7/27/15 Sample Solution

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

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

CS201 Some Important Definitions

Largest Online Community of VU Students

Pointers, Dynamic Data, and Reference Types

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

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

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

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

CSE 333 Midterm Exam July 24, Name UW ID#

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

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

Introduction Of Classes ( OOPS )

CSE 333 Midterm Exam Sample Solution 5/10/13

Documentation. Programming / Documentation Slide 42

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

Section - Computer Science. int main() {! int a=10,b=20;! printf("a:%d B:%d\n",a,b);! a=(a+b)-(b=a);! printf("a:%d B:%d\n",a,b);!

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

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

CSE 333 Midterm Exam 5/10/13

CS201 Latest Solved MCQs

CS105 C++ Lecture 7. More on Classes, Inheritance

Classes in C++98 and C++11

Pointers and References

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

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

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

An Introduction to C++

IS0020 Program Design and Software Tools Midterm, Fall, 2004

CS250 Final Review Questions

Intermediate Programming, Spring 2017*

Next week s homework. Classes: Member functions. Member functions: Methods. Objects : Reminder. Objects : Reminder 3/6/2017

Object-Oriented Programming, Iouliia Skliarova

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


CS 2604 Homework 1 C++ Review Summer I 2003

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Come and join us at WebLyceum

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

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

G52CPP C++ Programming Lecture 13

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Abstraction in Software Development

CS 161 Exam II Winter 2018 FORM 1

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Constants, References

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

CS304 Object Oriented Programming Final Term

C++ Programming: Polymorphism

CS 2604 Homework 1 Greatest Hits of C++ Summer 2000

Intermediate Programming, Spring 2017*

CS 101: Computer Programming and Utilization

C11: Garbage Collection and Constructors

C++ 8. Constructors and Destructors

UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class

CSCI 123 Introduction to Programming Concepts in C++

COMP 524 Spring 2018 Midterm Thursday, March 1

Midterm Exam 5 April 20, 2015

CS 2604 Homework 2 Solution for Greatest Hits of C++ Fall 2000

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

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

COMP 2355 Introduction to Systems Programming

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

CS24 Week 3 Lecture 1

Classes, Constructors, etc., in C++

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

REFERENCES, POINTERS AND STRUCTS

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

Programming Abstractions

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

C How to Program, 6/e, 7/e

COMPUTER APPLICATION

Outline. A C++ Linked Structure Class A C++ Linked List C++ Linked Dynamic Memory Errors In-class work. 1 Chapter 11: C++ Linked Structures

Collected By Anonymous

Transcription:

Multiple choice questions, 2 point each: 1. What output is produced by the following program? #include <stream.h> int f (int a, int &b) a = b + 1; b = 2 * b; return a + b; int main( ) int x=1, y=2, z=3; x = f(y, z); cout << x << " " << y << " " << z << endl; A. 10 2 2 B. 10 2 6 C. 10 4 6 D. The program won t compile because the function call should have been x = f(y, &z); E. None of above 2. 3. 4. Which of the following statements about function declarations and definitions are true? A. The definition gives the implementation of the function; the declaration only contains the function name, result type, and parameters. B. A declaration may only appear in a.h file; a definition only in a.cpp file. C. Declarations and definitions may both appear in.h files. D. Statements A and B are true. E. Statements A and C are true. If Blob is a class, its implementation file blob.cpp should contain an #include "blob.h" directive to include the blob interface because A. it is only good style; there is no technical reason for doing so. B. It allows the compiler to check whether the implementation in the.cpp file is consistent with the interface in the.h file. C. It may not be possible to compile the implementations in the.cpp file without doing so. D. Both B and C E. Actually, the.cpp should not #include the.h file. This could generate duplicate declarations or definitions of items in the class, and the file would not compile. Suppose we find the following function in a file: int Abc::xyz(int z) return 2 * z + 1; What do we know by looking at this function? A. This function appears in file abc.cpp B. xyz is a member function of class Abc. C. xyz is a friend function of class Abc. D. We could call this function in an assignment statement k = Abc.xyz(6). E. Tow or more of the above statements are true.

Page 2 5. A class definition allows some items to be specified as public and others to be specified as private. If a variable x is specified in the private section of the class definition, there are several places where one might want to change its value by directly assigning a new value to x. Of these possible places: 1: public member functions of the class 2: private member functions of the class 3: functions declared as friends of the class 4: other functions appearing in the implementation file 5: functions that appear in other.cpp files Which of these can directly change the value of x? A. 1 and 2 only B. 2 and 3 only C. 1, 2, and 3 only D. 1, 2, 3, and 4 only E. 1, 2, 3, 4, and 5 6. Given the followin line: friend Thing & operator<< (ostream &s, const Thing &t); Which of the following is true? (1) This line of code must appear in the Thing class declaration in order for it to be a friend of Thing. (2) The ostream should instead be passed by value. (3) This definition will not allow output stream chaining (ie. cout << x << y). A. 1 only B. 2 only C. 3 only D. 1 and 2, but not 3 E. 1 and 3, but not 2 7. A complete C++ class definition should contain most of the following if the class refers to dynamically allocated data. Which of these are not needed? A. Null constructor B. Copy constructor C. Initialization function D. Destructor E. Operator=

Page 3 8. Suppose Blob is a class containing all of the standard features needed in a C++ class. Suppose we have the following declarations in a client program that uses the Blob class: Blob b1; Blob b2 = b1; // **** What happens when the second variable declaration (marked ****) is processed? A. Blob::operator= (assignment) is executed to initialize b2 with value b1. B. The null constructor is executed to initialize b2, then Blob::operator= is executed to assign the value of b1 to b2. C. The copy constructor is executed to initialize b2 with the value b1. D. The null constructor is executed to initialize b2, then the copy constructor is executed to copy the value of b1 and store it in b2. Suppose we have a program that allocates and frees an array as follows: char* s; s = new char[100]; The character array is freed properly and returned to the heap. The program will not compile because you are using the wrong form of delete. The program may continue executing and might crash later. The program will not compile because you are deleting something in automatic memory. the program will delete the first item in the array, and the rest of the array will become a memory leak. Variable j will not be changed, but k is changed to 3. Variable j will change to value 3, k to 7. Variable j will change to value 63 (ASCII code for? ), k to 3. Neither j nor k will be changed. variables j and k will be changed to some undefined value. Modify readinnumbers to use assert macros. Modify readinnumbers to return a status flag. Modify readinnumbers to return a "safe" value if user input failed. B and C A and B and C

Page 4 12. 13. A function declaration contains A. The function name only. B. The function name and function body. C. The function name, types of parameters, and type of return value. D. The function name, types of parameters, type of the return value, and function body. E. A #include to access the associated header file What does the following program output? void snark(int* a, int& b, int c) *a = c; b = *a + 1; c = c + 2; void main() int x = 3; int *y = &x; snark(y, x, x); cout << x << " " << y << endl; A. 3 3 B. 4 4 C. 3 4 D. 4 3 E. None of above. Given the following code snippet: int* gobble(int size) int* a = new int[size]; a[0]=0; for (int k=1; k < size-1; k++) a[k] = a[k-1]+k; return a; 1 3 6 Unknown, because there s a dangling pointer. Unknown, because there s a memory leak.

Page 5 Short Problem (8 pts) The following problem assumes the following class and function: class Sample public: Sample(); Sample(const Sample& other); Sample& operator= (const Sample& other); ; ~Sample(); Sample mixsample ( Sample& blend1, Sample& blend2 ) Sample result = blend1; return result; Given the following code snippet, and assuming item1 and item2 are of type Sample: Sample blend3; blend3 = mixsample ( item1, item2); Write down all of the Sample methods (and the ojects they are invoked on) that are called during the execution of the code, in the order in which they are called. Short Problem (8 pts) For the following program snippet, draw a boxes and arrows diagram representing what is happening in memory, when explicitly stated in the code. Draw a vertical line representing the division between automatic and dynamic memory. All memory that is automatically allocated is placed on the right side of the vertical line. You must clearly show what the pointer is pointing to; if the pointer contains NULL, make Draw the diagram at this point

Page 6 Long Problem (21 pts) A knapsack holds a collection of items and their associated weights. A knapsack is limited by both the number of different items it can store as well as the overall weight of the items that it currently carries. Implement the methods of the Knapsack class: const int maxnitems = 80; const int maxknapsackweight = 400; const int maxitemnamesize = 80; // Max. # of different items in sack // Maximum weight that a sack can hold // Longest name len