MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

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

This examination has 11 pages. Check that you have a complete paper.

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Final Examination 8:30 AM, Wednesday, April 18, 2012

EXAMINATION INSTRUCTIONS

EXAMINATION INSTRUCTIONS

Question Marks 1 /11 2 /4 3 /13 4 /5 5 /11 6 /14 7 /32 8 /6 9 /11 10 /10 Total /117

Question Marks 1 /12 2 /6 3 /14 4 /8 5 /5 6 /16 7 /34 8 /25 Total /120

UNIVERSITY REGULATIONS

Question Marks 1 /20 2 /16 3 /7 4 /10 5 /16 6 /7 7 /24 Total /100

EXAMINATION INSTRUCTIONS

THE UNIVERSITY OF BRITISH COLUMBIA CPSC 110: MIDTERM 1 Part B May 26, Important notes about this examination

CPSC 121 Some Sample Questions for the Final Exam Tuesday, April 15, 2014, 8:30AM

THE UNIVERSITY OF BRITISH COLUMBIA CPSC 261: MIDTERM 1 February 14, 2017

UNIVERSITY REGULATIONS

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

CPSC 411, 2015W Term 2 Midterm Exam Date: February 25, 2016; Instructor: Ron Garcia

CPSC 320 Midterm 2. July 13, 2007

Question Marks 1 /16 2 /13 3 /12 4 /15 5 /8 6 /15 7 /8 8 /5 9 /8 Total /100

CPSC 311, 2010W1 Midterm Exam #2

CPSC 121 Midterm 1 Friday October 14th, Signature: Section (circle one): 11:00 15:30 17:00

CPSC 427: Object-Oriented Programming

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

CPSC 410 Advanced Software Engineering Mid-term Examination (Term I ): Solution Instructor: Gail Murphy

CPSC 410? Advanced Software Engineering Mid-term Examination (Term I ) SOLUTION Instructor: Gail Murphy

CS201 Some Important Definitions

CPSC 121 Sample Final Examination December 2013

EECS168 Exam 3 Review

CE221 Programming in C++ Part 1 Introduction

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

Chapter 4 Defining Classes I

Full Name: CS Account:

THE UNIVERSITY OF BRITISH COLUMBIA CPSC 121: MIDTERM 2 Group March 12, 2014

EXAMINATION INSTRUCTIONS

Chapter 6 Introduction to Defining Classes

CS 411 Midterm Feb 2008

Computer Science 304

CPSC 121 Midterm 1 Friday February 5th, Signature: Section (circle one): Morning Afternoon

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

04-19 Discussion Notes

CPSC 427: Object-Oriented Programming

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

An Introduction to C++

CS201 Latest Solved MCQs

And Even More and More C++ Fundamentals of Computer Science

CSE 303: Concepts and Tools for Software Development

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Problem Solving with C++

Interview Questions of C++

PIC 10A Objects/Classes

G52CPP C++ Programming Lecture 9

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

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

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

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

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.

Fundamental Concepts and Definitions

Common Misunderstandings from Exam 1 Material

Classes and Objects. Class scope: - private members are only accessible by the class methods.

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

Absolute C++ Walter Savitch

Object Oriented Programming in C#

CLASSES AND OBJECTS IN JAVA

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Computer Programming : C++

QUIZ Friends class Y;

Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review)

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

EXAMINATION INSTRUCTIONS

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

The University of British Columbia Computer Science 304 Practice Final Examination

Ch. 11: References & the Copy-Constructor. - continued -

READ AND OBSERVE THE FOLLOWING RULES:

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

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

Cpt S 122 Data Structures. Introduction to C++ Part II

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

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

Ch. 10: Name Control

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

UNIVERSITY OF SWAZILAND

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

A class is a user-defined type. It is composed of built-in types, other user-defined types and

CIS 190: C/C++ Programming. Classes in C++

2 ADT Programming User-defined abstract data types

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

MPATE-GE 2618: C Programming for Music Technology. Syllabus

PREPARING FOR PRELIM 1

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

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

Programming in C and C++

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

Copying Data. Contents. Steven J. Zeil. November 13, Destructors 2

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

CMSC 202 Midterm Exam 1 Fall 2015

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 2. Procedural Programming

CS 455 Final Exam Spring 2015 [Bono] May 13, 2015

Transcription:

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012 Instructor: K. S. Booth Time: 70 minutes (one hour ten minutes) Total marks: 70 First Last Student No Printed first name Printed last name Signature Lecture Section 201 Lab Section This examination has 11 pages. Check that you have a complete paper. This is a closed book exam. Notes, books or other materials are not allowed. Answer all the questions on this paper. The marks for each question are given in braces. Use this to manage your time. Good luck. READ AND OBSERVE THE FOLLOWING RULES: 1. Each candidate should be prepared to produce, upon request, his or her Library/AMS card. 2. No candidate shall be permitted to enter the examination room after the expiration of 10 minutes, or to leave during the first 10 minutes of the examination. 3. Candidates are not permitted to ask questions of the invigilators, except in cases of supposed errors in examination questions. 4. CAUTION Candidates guilty of any of the following, or similar dishonest practices shall be immediately dismissed from the examination and shall be liable to disciplinary action. a. Making use of any books, papers or memoranda, calculators or computers, audio or visual cassette players, or other memory aid devices, other than those authorized by the examiners. b. Speaking or communicating with other candidates. c. Purposely exposing written papers to the view of other candidates. The plea of accident or forgetfulness shall not be received. Page Mean Max 3 7.36 10 4 7.23 10 5 5.71 10 6 5.75 8 7 3.57 5 8 3.10 7 9 7.22 10 10 3.48 10 Total 43.42 70

1. Multiple choice questions 30 marks 1 mark per question On the next page is a series of short fill-in-the-blanks questions. All of your answers are to be selected from the list below. You may find it convenient to remove this page from the answer booklet so you can look at it while you answer the questions that follow. 1) accessor 17) executable 33) lifetime 49) scope 2) automatic 18) extent 34) linker 50) selection 3) binding 19) field 35) member 51) shallow 4) block 20) frame 36) memory 52) signature 5) boolean 21) function 37) module 53) source 6) compiler 22) global 38) mutator 54) stack 7) conditional 23) header 39) object 55) static 8) constructor 24) heap 40) operator 56) string 9) contiguous 25) heterogeneous 41) overloading 57) struct 10) dangling 26) homogeneous 42) pointer 58) synthesize 11) deep 27) implicit 43) private 59) this 12) depth 28) initialization 44) public 60) type 13) destructor 29) instance 45) record 61) value 14) driver 30) invariant 46) recursion 62) visibility 15) dynamic 31) leak 47) reference 63) void 16) encapsulation 32) level 48) relational 64) while Each statement will have one within it, which is where the missing term or phrase would appear. Choose the best answer from among those above and write its number in the space provided in the first column. Do not write the term or phrase. It may be a good idea to read over the list of terms and phrases before you start answering. Some of the terms listed may not appear in any of the statements, some may appear in more than one statement, and some many appear in exactly one statement. Continue on to the next page You may remove this page from the exam booklet. CPSC 260 Midterm #1 February 14, 2012 Page 2 of 11

Read the instructions on the previous page. Enter the number for your answer in the first column. Do not write words! (a) 33 (b) 49 (c) 2 (d) 15 (e) 22 (f) 55 (g) 47 (h) 47 (i) 43 (j) 44 A synonym for the term extent is. It refers to the duration during which a variable exists in memory. A synonym for the term visiblity is. It refers to the portion of a program in which a particular identifier is bound to a particular variable. variables are always stored in the stack memory segment. variables are always stored in the heap memory segment. variables are visible to the linker and are always stored in the static memory segment. variables are not visible to the linker and are always stored in the static memory segment. The call-by- parameter passing mechanism does not exist in the C programming language. The call-by- parameter passing mechanism is always used in a copy constructor in the C++ programming language. Member variables in a class are usually declared to be. lifetime scope automatic dynamic global static reference reference private A struct is the same as a class except that member variables are by default. public (This question is continued on the next page.) Put a slash through every wrong answer, and an X through every blank answer. Total the number of correct answers and write at top of page AND on first page. CPSC 260 Midterm #1 February 14, 2012 Page 3 of 11

(This question is continued from the previous page.) (k) 58 (l) 20 (m) 27 (n) 25 (o) 63 (p) 47 (q) 52 (r) 28 (s) 11 (t) 51 A compiler will a destructor if one is not declared for a class. synthesize The memory in the stack segment that is allocated at run time when a function or method is invoked is called a stack. The pointer variable this that is built in to every method is the parameter for the method. Unlike arrays, structs are data types. The keyword is used to indicate that a function does not return a value. int i; int &k = i; frame implicit heterogeneous void The variable k is a because of the &. reference The number and the types of the parameters passed to a function are part of the for the function. signature The process of uses syntax that is similar to an assignment, but does not perform an assignment. initialization A copy usually allocates new dynamic memory for one or more of the member variables in an object. deep A copy usually does not allocate new dynamic memory for any of the member variables in an object. shallow (This question is continued on the next page.) Put a slash through every wrong answer, and an X through every blank answer. Total the number of correct answers. CPSC 260 Midterm #1 February 14, 2012 Page 4 of 11

(This question is continued from the previous page.) (u) 5 (v) 48 (w) 7 (x) 64 (y) 4 (z) 23 (aa) 53 (bb) 34 (cc) 6 (dd) 56 There are two binary logical or operators in C and C++, && and. The binary operator == is a operator in C and C++. The three-part operator value1? value2 : value3 is the operator in C and C++. There are two variants of the control flow, one that tests before each iteration of a loop and one that tests after each iteration. A sequence of statements enclosed in curly braces and is a. The file for a class contains the class declaration. The file for a class contains the class definitions. boolean relational conditional while block header source The will report an error if a program defines the same function more than once. linker The will report an error if a program fails to declare a variable that is used in an assignment statement. compiler The type does not exist in C but is available in C++ as one of the standard classes that extend the basic language. It is often a better choice than the array of char type in C. string CPSC 260 Midterm #1 February 14, 2012 Page 5 of 11

2. Class declarations and method definitions 13 marks Refer to the following UML (Universal Modeling Language) for both parts of this question. It specifies a simplified variation of the Date class that was used in Labs 2 and 3. - year_: int - month_: string - day_: int + Date() Date + setdate(year: int, month: string, day: int): void + tostring() const: string - inttostring(n: int): string (a) 8 marks Write a complete class header file for the Date class that matches the UML diagram. #ifndef _DATE_H_ #define _DATE_H_ #include <string> using namespace std; class Date private: int year_; string month_; int day_; static string inttostring( int n); public: Date(); ; #endif ½ mark off for each part that is incorrect #ifndef, #define, #endif #include, using class, class name private, public year_, month_, day_ static, string return type, name intostring, parameter constructor void, method name, parameters string return type, method name, const ½ mark off for extra stuff ( const, etc.) void setdate( int year, string month, int day ); string tostring() const; Continue on to the next page CPSC 260 Midterm #1 February 14, 2012 Page 6 of 11

(b) 5 marks Write a complete definition for the tostring() method in the Date class. The format for the text representation of a Date should be Month dd, yyyy where the Month is the name of the month spelled out (not a number), dd is the day in the month (a one- or two-digit number), and yyyy is the year (a number). You may assume that all of the other methods have been implemented and that all of the member variables have appropriate values. string Date::toString() const return month_ + " " + inttostring( day_ ) + ", " + inttostring( year_ ) ½ mark off for each part that is incorrect return type, class name, method name, no parameters, const month_ does not need conversion blank, convert day, comma, convert year ½ mark off for extra stuff ( const, etc.) CPSC 260 Midterm #1 February 14, 2012 Page 7 of 11

3. Class declarations and method definitions with dynamic memory 27 marks Refer to the following class declaration for all parts of this question. class IntSet private: static const int CAPACITY = 5; int capacity_; int *data_; int count_; ;... public: IntSet( int capacity=capacity ) : capacity_(capacity), data_(new int[capacity]), count_(0) ~IntSet(); IntSet( const IntSet &other ); IntSet& operator=( const IntSet &other ); void insert( int entry ); void remove( int entry ); bool find( int entry ); int get( int index ); The return type for insert() was int on the exam for the declaration, but should have been void ( which is what it was for the definition) so it has been changed here. It made no difference for the question. (a) 2 marks How many distinct signatures are declared for constructors in the class declaration? 3 All or nothing. There are TWO constructors, default and parameterized in the first method plus the copy constructor (total of THREE). (b) 5 marks Using the conventions we adopted in lecture, write the declarations for the two helper methods that should be where the... appear in the class declaration above for the IntSet class. void copy( const IntSet &other ); void cleanup(); 1/2 mark off for each part that is incorrect return type, method name, const, type, reference, parameter name return type, method name, no parameters Continue on to the next page ½ mark off for extra stuff ( const, etc.) CPSC 260 Midterm #1 February 14, 2012 Page 8 of 11

Consider the following code example that uses the IntSet class declared on the previous page. The code is a definition for the insert() method as it might appear in the IntSet.cpp file. void IntSet::insert( int entry ) if ( find( entry ) ) return; if ( count_ < capacity_ ) data_[ count_ ] = entry; count_++; (c) 10 marks Write a complete definition for the remove() method for the IntSet class based on the class declaration on page 8 and the implementation described in lecture, which is very similar to the discussion in the textbook in Chapter 6 about how to remove items from a vector. The key idea is to overwrite the entry that is being removed with the last entry and then decrease the size by one. void IntSet::remove( int entry ) for ( int i=0; i<count_; i++ ) if ( data_[i] == entry ) count_--; data_[i] = data_[count_]; return; return; 1 mark off for each part that is incorrect return type, class name, method name, parameter type, parameter name iteration (for or while), only look at count number of entries test to find entry decrement count move last entry to vacant entry 1 mark off for extra stuff ( const, etc.) Both of the returns are optional. The first return could be a break instead. Continue on to the next page CPSC 260 Midterm #1 February 14, 2012 Page 9 of 11

The next piece of code is a definition for main()as it might appear in the IntSet-driver.cpp file. int main() IntSet A; for ( int k=1; k<5; k++ ) A.insert( k ); A.remove( 1 ); A.remove( 2 ); A.insert( 2 ); (d) 10 marks Draw a diagram of the memory associated with the IntSet object A after the above code has executed. You should use the implementation we described in lecture for the remove() method. The code you wrote in part (c) does this, but you should be able to answer this part of the question even if you did not complete part (c). Use the symbol? to indicate an undefined or unspecified value. Label each of the member variables. capacity_ 5 data_ 4 count_ 3 3 2 4? 1 mark off for each entry that is incorrect (including the arrow) 1 mark off for each extra entry in either the object or the array 1 mark off for each missing or incorrect label TOTAL cannot be negative CPSC 260 Midterm #1 February 14, 2012 Page 10 of 11

Continue your answers here make sure to identify the questions whose answers are here! Additional marking instructions [Instructions will be provided prior to the marking session.] CPSC 260 Midterm #1 February 14, 2012 Page 11 of 11