AP Computer Science AB 2002 Free-Response Questions

Similar documents
2003 AP COMPUTER SCIENCE AB FREE-RESPONSE QUESTIONS

AP Computer Science A 2004 Scoring Commentary

AP Computer Science AB 2007 Free-Response Questions

AP Computer Science A 2003 Scoring Guidelines

Java 1996 AP Computer Science Question 3

AP Computer Science AB 2005 Scoring Guidelines

AP Computer Science A 2008 Scoring Guidelines

AP Computer Science A 2013 Free-Response Questions

AP Calculus BC 2009 Free-Response Questions Form B

Chapter 20: Binary Trees

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Suppose that the following is from a correct C++ program:

void insert( Type const & ) void push_front( Type const & )

Out: April 19, 2017 Due: April 26, 2017 (Wednesday, Reading/Study Day, no late work accepted after Friday)

AP Computer Science. Marine Biology Teacher s Manual. Supplement to the Marine Biology Case Study. Advanced Placement Program

Unit 9 Practice Test (AB27-30)

Largest Online Community of VU Students

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

CSE100. Advanced Data Structures. Lecture 12. (Based on Paul Kube course materials)

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Chapter 9. Priority Queue

Computer Science 136 Spring 2004 Professor Bruce. Final Examination May 19, 2004

MIDTERM EXAMINATION Douglas Wilhelm Harder EIT 4018 x T09:30:00P1H20M Rooms: RCH-103 and RCH-302

CS 215 Fundamentals of Programming II Fall 2017 Project 7. Morse Code. 30 points. Out: November 20, 2017 Due: December 4, 2017 (Monday) a n m

Name CPTR246 Spring '17 (100 total points) Exam 3

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

COMPUTER SCIENCE A SECTION II

PRACTICE MIDTERM EXAM #2

Technical University of Denmark

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.

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

Heaps & Priority Queues. (Walls & Mirrors - Remainder of Chapter 11)

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

York University AK/ITEC INTRODUCTION TO DATA STRUCTURES. Final Sample II. Examiner: S. Chen Duration: Three hours

Priority Queues and Huffman Trees

STUDENT LESSON AB30 Binary Search Trees

PRACTICE FINAL EXAM 3

Review Questions for Final Exam

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113)

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Garbage Collection: recycling unused memory

Linked List using a Sentinel

CS 223: Data Structures and Programming Techniques. Exam 2. April 19th, 2012

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Data Structures Lab II. Binary Search Tree implementation

CS 104 (Spring 2014) Final Exam 05/09/2014

CMSC 341 Lecture 10 Binary Search Trees

Exam Informatik D-MATH/D-PHYS :00 17:00

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

Compsci 100. Owen Astrachan April 14, 2010

Algorithms and Data Structures CS-CO-412

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

Good Luck! CSC207, Fall 2012: Quiz 1 Duration 25 minutes Aids allowed: none. Student Number:

CSCE 2014 Final Exam Spring Version A

the pointer range [first, last) into the tree

CS 115 Exam 3, Spring 2010

Huffman Coding Assignment For CS211, Bellevue College (rev. 2016)

(the bubble footer is automatically inserted into this space)

Chapter 12: Searching: Binary Trees and Hash Tables. Exercises 12.1

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Data and File Structures Laboratory

CS106X Handout 35 Winter 2018 March 12 th, 2018 CS106X Midterm Examination

LAB 5, THE HIDDEN DELIGHTS OF LINKED LISTS

ITI Introduction to Computing II

ADT Unsorted List. Outline

Algorithms and Data Structures CS-CO-412

Lecture 34. Wednesday, April 6 CS 215 Fundamentals of Programming II - Lecture 34 1

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

Binary Trees

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

ITI Introduction to Computing II

CS-301 Data Structure. Tariq Hanif

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

Lecture Notes on Tries

Topic Binary Trees (Non-Linear Data Structures)

Final Exam. Name: Student ID: Section: Signature:

Expression Trees and the Heap

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

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

Data compression.

Lecture Notes on Tries

Binary Trees and Binary Search Trees

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Data Structure. IBPS SO (IT- Officer) Exam 2017

C++ CODE DESIGN INTRO TO ABSTRACT DATA TYPES

"sort A" "sort B" "sort C" time (seconds) # of elements

Final exam. CS 2110, December 15, 2016, 9:00AM

Transcription:

AP Computer Science AB 2002 Free-Response Questions The materials included in these files are intended for use by AP teachers for course and exam preparation in the classroom; permission for any other use must be sought from the Advanced Placement Program. Teachers may reproduce them, in whole or in part, in limited quantities, for face-to-face teaching purposes but may not mass distribute the materials, electronically or otherwise. These materials and any copies made of them may not be resold, and the copyright notices must be retained as they appear here. This permission does not apply to any third-party copyrights contained herein. These materials were produced by Educational Testing Service (ETS ), which develops and administers the examinations of the Advanced Placement Program for the College Board. The College Board and Educational Testing Service (ETS) are dedicated to the principle of equal opportunity, and their programs, services, and employment policies are guided by that principle. The College Board is a national nonprofit membership association dedicated to preparing, inspiring, and connecting students to college and opportunity. Founded in 1900, the association is composed of more than 4,200 schools, colleges, universities, and other educational organizations. Each year, the College Board serves over three million students and their parents, 22,000 high schools, and 3,500 colleges, through major programs and services in college admission, guidance, assessment, financial aid, enrollment, and teaching and learning. Among its best-known programs are the SAT, the PSAT/NMSQT, and the Advanced Placement Program (AP ). The College Board is committed to the principles of equity and excellence, and that commitment is embodied in all of its programs, services, activities, and concerns. College Board, Advanced Placement Program, AP, SAT, and the acorn logo are registered trademarks of the College Entrance Examination Board. APIEL is a trademark owned by the College Entrance Examination Board. PSAT/NMSQT is a registered trademark jointly owned by the College Entrance Examination Board and the National Merit Scholarship Corporation. Educational Testing Service and ETS are registered trademarks of Educational Testing Service.

COMPUTER SCIENCE AB SECTION II Time 1 hour and 45 minutes Number of questions 4 Percent of total grade 50 Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN C++. Note: Assume that the standard libraries (e.g., iostream.h, fstream.h, math.h, etc.) and the AP C++ classes are included in any program that uses a program segment you write. If other classes are to be included, that information will be specified in individual questions. Unless otherwise noted, assume that all functions are called only when their preconditions are satisfied. A Quick Reference to the AP C++ classes is included in the case study insert. 2

1. Consider the problem of assigning passengers to seats on airline flights. Three types of information are needed passenger information, seat information, and flight information. Three classes will be used to represent this information, respectively: Passenger, Seat, and Flight. You will write three member functions for the Flight class: (a) (b) (c) EmptySeatCount that returns the number of empty seats of a specified type, FindBlock that returns information about the location of an empty block of seats, and AssignGroup that attempts to assign a group of passengers to adjacent seats. Passenger information is abstracted by a class and includes a name and other information. A default passenger, used to indicate no passenger in a seat, has the empty string "" as its name. The declaration for class Passenger is as follows. class Passenger public: Passenger(); // default passenger with name "" apstring GetName() const; // postcondition: returns passenger s name }; //... other public and private members not shown Seat information includes the passenger assigned to the seat and the type of the seat ( window, aisle, middle ). The Seat function GetPassenger returns the passenger assigned to the seat; if the seat is empty, GetPassenger returns a default passenger. The declaration for the class Seat is as follows. class Seat public: Passenger GetPassenger() const; // postcondition: returns passenger in this seat apstring GetType() const; // postcondition: returns the type of this seat void SetPassenger(const Passenger & p); // postcondition: assigns p to this seat (i.e., GetPassenger() == p) }; //... constructors and other public and private members not shown 3

Seat assignments are processed by the public member functions of the class Flight. The seating arrangement is represented internally by a matrix of seats in the class Flight. The declaration for the class Flight is as follows. class Flight public: int EmptySeatCount(const apstring & seattype) const; // postcondition: returns the number of empty seats // whose type is seattype; // if seattype is "any", returns the // total number of empty seats int FindBlock(int row, int seatsneeded) const; // postcondition: returns column index of the first (lowest index) // seat in a block of seatsneeded adjacent // empty seats in the specified row; // if no such block exists, returns -1 bool AssignGroup(const apvector<passenger> & group); // postcondition: if possible, assigns the group.length() passengers // from group to adjacent empty seats in a single row // and returns true; // otherwise, makes no changes and returns false //... constructors and other public member functions not shown private: apmatrix<seat> myseats; }; //... other private data members not shown 4

(a) You will write the Flight member function EmptySeatCount, which is described as follows. EmptySeatCount returns the number of empty seats of the specified type seattype. Recall that an empty seat holds a default passenger whose name is "". If seattype is "any", then every empty seat should be counted in determining the number of empty seats. Otherwise, only seats whose type is the same as seattype are counted in determining the number of empty seats. For example, consider the diagram of passengers assigned to seats as stored in myseats for Flight ap2002 as shown below. [0] [1] [2] [3] [4] [5] window middle aisle aisle middle window [0] Kelly Robin Sandy Fran window middle aisle aisle middle window [1] Chris Alex Pat Sam The following table shows several examples of calling EmptySeatCount for this flight. Function Call Value Returned ap2002.emptyseatcount("aisle") 3 ap2002.emptyseatcount("window") 0 ap2002.emptyseatcount("middle") 1 ap2002.emptyseatcount("any") 4 Complete function EmptySeatCount below. int Flight::EmptySeatCount(const apstring & seattype) const // postcondition: returns the number of empty seats // whose type is seattype; // if seattype is "any", returns the // total number of empty seats 5

(b) You will write the Flight member function FindBlock, which is described as follows. FindBlock searches for a block of seatsneeded adjacent empty seats in the specified row. If such a block of seats is found, FindBlock returns the column index of the first (i.e., the lowest index) seat in the block; otherwise, it returns -1. The seating diagram for passengers of Flight ap2002 is repeated here for your convenience. [0] [1] [2] [3] [4] [5] window middle aisle aisle middle window [0] Kelly Robin Sandy Fran window middle aisle aisle middle window [1] Chris Alex Pat Sam The following table shows several examples of calling FindBlock for Flight ap2002 as shown. Function Call Value Returned ap2002.findblock(0, 1) 2 or 4 ap2002.findblock(0, 2) -1 ap2002.findblock(1, 2) 2 Complete function FindBlock below. int Flight::FindBlock(int row, int seatsneeded) const // postcondition: returns column index of the first (lowest index) // seat in a block of seatsneeded adjacent // empty seats in the specified row; // if no such block exists, returns -1 6

(c) You will write the Flight member function AssignGroup, which is described as follows. The parameter to the Flight member function AssignGroup is an array of passengers, group. These passengers require a block of adjacent seats in a single row. AssignGroup searches for group.length() adjacent empty seats in some row. If such a block of seats is found, the passengers in group will be assigned to those seats, and AssignGroup returns true. Otherwise, no passengers are assigned to seats, and AssignGroup returns false. For example, the seats in Flight ap314 are as shown in the first diagram below. If the array adults contains three passengers, the call ap314.assigngroup(adults) makes no changes to ap314 and returns false, because there is no block of three adjacent empty seats in a single row. On the other hand, suppose the array kids contains passengers "Sam" and "Alex". The call ap314.assigngroup(kids) will assign "Sam" and "Alex" to the seats shown in the second diagram below and return true. Contents of myseats for ap314 before any call to AssignGroup [0] [1] [2] [3] [4] window aisle aisle middle window [0] Kelly Sandy Fran window aisle aisle middle window [1] Chris Pat Contents of myseats for ap314 after call to ap314.assigngroup(kids) [0] [1] [2] [3] [4] window aisle aisle middle window [0] Kelly Sandy Fran window aisle aisle middle window [1] Chris Sam Alex Pat In writing AssignGroup, you may call FindBlock specified in part (b). Assume that FindBlock works as specified, regardless of what you wrote in part (b). Complete function AssignGroup below. bool Flight::AssignGroup(const apvector<passenger> & group) // postcondition: if possible, assigns the group.length() passengers // from group to adjacent empty seats in a single row // and returns true; // otherwise, makes no changes and returns false 7

2. Consider the following class declaration for representing cards to be used in a program that simulates a card game. class Card public: int Value() const; // postcondition: returns the value of this card //... constructors and other public member functions not shown private: //... private data members not shown }; The card game to be simulated is a two-player game called Compete that is played with a set of cards, each having a numeric value. The object of the game is to win all of the cards. Each player has a pile of cards and both piles start with the same number of cards. Play consists of a sequence of rounds. Play continues until at the end of a round at least one player is out of cards. A discard pile is created and used during the rounds and is empty at the beginning of each round. You will write code to move cards from one pile to another and to play one round. Each pile of cards will be represented by a queue. A round is played by executing the following steps until the round ends. 1. If exactly one player s pile is empty, the other player adds all the cards in the discard pile to the bottom of his or her pile without changing the order. The round ends. 2. If both players piles are empty at the same time, both players piles remain empty. The round ends. 3. Each player turns over the top card from his/her pile. 4. If the cards match in value, both cards are added to the discard pile in either order and play returns to step 1. 5. If the cards do not match in value, the player whose card has the greater value adds any cards in the discard pile to the bottom of his/her pile without changing the order of the cards in the discard pile. He/she then adds both cards just played to the bottom of his/her pile in either order. The round ends. (a) You will write free function AppendQueue, which is described as follows. AppendQueue should remove all the cards from the parameter source and add them to the parameter destination in the same order. Complete function AppendQueue below. void AppendQueue(apqueue<Card> & destination, apqueue<card> & source) // postcondition: all cards have been removed from source // and added to destination in the same order 8

(b) You will write free function OneRound. OneRound takes the two players piles of cards as parameters and carries out one round of the game. For your convenience the steps for one round are repeated here. 1. If exactly one player s pile is empty, the other player adds all the cards in the discard pile to the bottom of his or her pile without changing the order. The round ends. 2. If both players piles are empty at the same time, both players piles remain empty. The round ends. 3. Each player turns over the top card from his/her pile. 4. If the cards match in value, both cards are added to the discard pile in either order and play returns to step 1. 5. If the cards do not match in value, the player whose card has the greater value adds any cards in the discard pile to the bottom of his/her pile without changing the order of the cards in the discard pile. He/she then adds both cards just played to the bottom of his/her pile in either order. The round ends. In writing OneRound, you may call Card::Value() and AppendQueue from part (a). Assume that AppendQueue works as specified, regardless of what you wrote in part (a). Complete function OneRound below. void OneRound(apqueue<Card> & pile1, apqueue<card> & pile2) // precondition: pile1.length() > 0; pile2.length() > 0 // postcondition: pile1 and pile2 have been updated according to the // rules of the game 9

3. This question involves reasoning about the code from the Marine Biology Case Study. A copy of the code is provided as part of this exam. The original version of the case study uses a two-dimensional matrix, myworld, to represent the world in which the simulation takes place. Consider an alternate representation where the fish in each row are stored in a singly linked list. The implementation of myworld becomes an array in which each element contains a pointer to the first node in the linked list for that row. If there are no fish in that row, the pointer is NULL. Each list node contains a fish, the column index for that fish, and a pointer to the node containing the next fish in that row. The linked list is ordered by column index, from smallest to largest. In the example below, myworld[0] is a pointer to the first node of a list containing two fish: fish D at column 1 and fish A at column 2. The element myworld[3] is NULL, indicating that there are no fish in that row. The list of fish will be implemented using the following declaration. struct ListNode Fish thefish; int columnindex; ListNode * next; ListNode(); // sets thefish to emptyfish, columnindex to -1, next to NULL ListNode(const Fish f, int c, ListNode * link); // sets thefish to f, columnindex to c, next to link }; 10

Consider the following changes (shown in bold) to the private section of the Environment class. private: bool InRange(const Position & pos) const; // postcondition: returns true if pos in grid, // returns false otherwise apvector<listnode *> myworld; // grid of fish int mynumcols; // from file input when environment constructed int myfishcreated; int myfishcount; // # fish ever created // # fish in current environment 11

(a) Modify the Environment member function AllFish to use the revised data structure. In writing AllFish, you may use any other Environment member functions or the public member functions of any other class used in this case study. Assume that all member functions work as specified. Complete function AllFish below. Note the changes shown in bold. apvector<fish> Environment::AllFish() const // postcondition: returned vector (call it fishlist) contains all // fish in top-down, left-right order: // top-left fish in fishlist[0], // bottom-right fish in fishlist[fishlist.length()-1]; // # fish in environment is fishlist.length() apvector<fish> fishlist(myfishcount); int r, k; // c from original not needed int count = 0; apstring s = ""; ListNode * tempptr; // look at all grid positions, store fish found in vector fishlist // insert code here } // end of inserted code for (k = 0; k < count; k++) s += fishlist[k].location().tostring() + " "; } DebugPrint(5, "Fish vector = " + s); return fishlist; 12

(b) Modify the Environment member function AddFish to use the revised data structure. The new fish should be inserted into the correct row s linked list, maintaining the order of the list sorted by column index. In writing AddFish, you may use any other Environment member functions or the public member functions of any other class used in this case study. Assume that all member functions work as specified. Complete function AddFish below. void Environment::AddFish(const Position & pos) // precondition: no fish already at pos, i.e., IsEmpty(pos) // postcondition: fish created at pos if (! IsEmpty(pos)) cerr << "error, attempt to create a fish at non-empty: " } << pos << endl; // insert code here } 13

4. Consider the problem of encoding words as a string of 0 s and 1 s using a codetree. A codetree is a binary tree containing letters in its leaves. The encoding of a letter is represented by the root-to-leaf path for the letter. The same codetree is used for encoding and decoding. The following properties hold for every codetree. (i) Every node is either a leaf or has exactly 2 children. (ii) Letters appear only at the leaves of the codetree. (iii) There are at least 2 letters in the codetree. (iv) Each letter appears at most once in the codetree; thus there is a unique root-to-leaf path encoding for each letter. For example, consider the following codetree, C. The code for each letter is a string formed by appending a 0 when taking a left branch and a 1 for a right branch when traversing the root-to-leaf path. In the codetree above, the code for u is "010" (left, right, left), the code for s is "00", and the code for n is "10". A word is encoded by appending the codes for letters in the word together. For example, the code for "sun" is "0001010", which is formed by appending the codes for s, u, and n. 14

Consider the following declarations for a tree node and a class that represents the codetree. struct Node char letter; // value ignored except in leaves Node * left; Node * right; }; // link to left child // link to right child class CodeTree public: apstring BitsToWord(const apstring & code) const; // precondition: code is a string of 0 s and 1 s representing // a valid encoded word // postcondition: returns decoded word for code apstring WordToBits(const apstring & word) const; // precondition: each character in word is in a leaf // of the codetree // postcondition: returns the code for word //... constructor and other public member functions not shown private: apstring CharToBitsHelper(char ch, Node * T, const apstring & pathsofar) const; // postcondition: if ch is in subtree T, returns code for ch Node * myroot; }; //... other private data members and functions not shown 15

(a) You will write the CodeTree member function BitsToWord, which is described as follows. BitsToWord is given a coded word (a string of 0 s and 1 s) and returns the decoded word. Each character of code represents a branch in the codetree, where 0 represents a left branch and 1 represents a right branch. To decode the word represented by code, begin at the root and follow a branch for each 0 or 1 character in code. When a leaf is reached, one letter in the decoded word has been found. The decoding process begins again at the root of the codetree with the next 0 or 1 character in code. For example, using the CodeTree C as shown, if code is "1110", the call C.BitsToWord(code) returns the word "in". This result is obtained as follows. The path starts at the root and goes right for the first 1, right again for the second 1, and a leaf is reached, meaning the decoded word begins with i. Starting back at the root of the codetree and with the next 1 in code, the path goes right for 1 and left for 0, reaching the leaf with the letter n. The decoded word is now "in", and since all characters in code have been processed, "in" is returned. Similarly, C.BitsToWord("000101010011") returns the word "sunny". Complete function BitsToWord below. apstring CodeTree::BitsToWord(const apstring & code) const // precondition: code is a string of 0 s and 1 s representing // a valid encoded word // postcondition: returns decoded word for code 16

(b) The implementation of WordToBits given below forms the code for word by appending the result of calling the private member function CharToBitsHelper once for each character in the parameter word. apstring CodeTree::WordToBits(const apstring & word) const // precondition: each character in word is in a leaf of the codetree // postcondition: returns the code for word apstring bits; for (int k = 0; k < word.length(); k++) bits += CharToBitsHelper(word[k], myroot, ""); } } return bits; You will write the CodeTree private member function CharToBitsHelper. CharToBitsHelper has a third parameter, pathsofar, that can be used to keep track of the current path from myroot to T, should you choose to do so. For this reason, the value of pathsofar in the call from WordToBits is "". Using CodeTree C as shown, CharToBitsHelper( y, myroot, "") would return the string "011" and CharToBitsHelper( n, myroot, "") would return "10". Complete function CharToBitsHelper below. apstring CodeTree::CharToBitsHelper(char ch, Node * T, const apstring & pathsofar) const // postcondition: if ch is in subtree T, returns code for ch END OF EXAMINATION 17