AP Computer Science AB 2007 Free-Response Questions

Similar documents
AP Computer Science AB 2002 Free-Response Questions

AP Computer Science AB 2005 Scoring Guidelines

AP Computer Science A 2004 Scoring Commentary

AP COMPUTER SCIENCE AB 2007 SCORING GUIDELINES

AP Computer Science A 2008 Scoring Guidelines

AP Calculus BC 2009 Free-Response Questions Form B

AP Computer Science A 2013 Free-Response Questions

AP Computer Science A 2003 Scoring Guidelines

Unit 9 Practice Test (AB27-30)

Java 1996 AP Computer Science Question 3

DUKE UNIVERSITY Department of Computer Science. Test 2: CompSci 100e

AP Computer Science A

Dartmouth College Computer Science 10, Winter 2012 Final Exam

Results from!the!2011 AP!Computer!Science A Exam Administration

AP Computer Science A

Midterm Exam 2 CS 455, Spring 2015

CS 314 Final Fall 2012

CS 61B, Spring 1999 MT3 Professor M. Clancy

CSE 143 Sample Final Exam #3

AP Computer Science A

Section I B COMPUTER SCIENCE SOLUTION

CSE 143, Winter 2009 Final Exam Thursday, March 19, 2009

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Student Performance Q&A:

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2011

Midterm Exam 2 CS 455, Fall 2014

EXAM Computer Science 1 Part 1

CS314 Exam 2 - Spring Suggested Solution and Criteria 1

Computer. Course Description

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

CMPSC 111 Introduction to Computer Science I Fall 2016 Lab 8 Assigned: October 26, 2016 Due: November 2, 2016 by 2:30pm

CS61BL Summer 2013 Midterm 2

CS 61B, Summer 2001 Midterm Exam II Professor Michael Olan

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

Computer Science 62. Midterm Examination

Chapter 20: Binary Trees

DUKE UNIVERSITY Department of Computer Science. Test 2: CompSci 100

APCS :: Winter Quarter Exam Review Packet

tree nonlinear Examples

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

STUDENT LESSON AB30 Binary Search Trees

Outline. iterator review iterator implementation the Java foreach statement testing

APCS Semester #1 Final Exam Practice Problems

COMP 103 Introduction to Data Structures and Algorithms

TREES Lecture 12 CS2110 Fall 2016

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

a graph is a data structure made up of nodes in graph theory the links are normally called edges

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

CS 314 Final Fall 2011

CS 216 Exam 1 Fall SOLUTION

CS 314 Exam 2 Spring

Computer Science E-119 Fall Problem Set 4. Due prior to lecture on Wednesday, November 28

2003 AP COMPUTER SCIENCE AB FREE-RESPONSE QUESTIONS

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Lab 8: Search Trees Due: Start of your next lab session

Lab 4: Super Sudoku Solver CSCI 2101 Fall 2017

AP Computer Science in Java Course Syllabus

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

Computer Science 136 Exam 2

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

Summer Final Exam Review Session August 5, 2009

Student Performance Q&A:

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

CS 206 Introduction to Computer Science II

AP Computer Science AB

CS 307 Final Spring 2008

Computer Science Foundation Exam

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

ITI Introduction to Computing II

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

CompSci 201 Tree Traversals & Heaps

CSE wi Midterm Exam 2/8/18. Name UW ID #

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

Largest Online Community of VU Students

CSCI Lab 9 Implementing and Using a Binary Search Tree (BST)

COMP : Trees. COMP20012 Trees 219

ITI Introduction to Computing II

CSE 143 Sample Final Exam #5

CSE 143, Winter 2010 Final Exam Thursday, March 18, 2010

Homework 3 Huffman Coding. Due Thursday October 11

CSE 143 Au03 Final Exam Page 1 of 15

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

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

EXAMINATIONS 2005 END-YEAR. COMP 103 Introduction to Data Structures and Algorithms

CS 314 Exam 2 Fall 2017

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

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

SCourse Description. Includes important information regarding the introduction of the language Java. MAY 2004, MAY 2005

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

9/19/2018 Programming Data Structures. Polymorphism And Abstract

AP Computer Science A

CSE143 Summer 2008 Final Exam Part A KEY August 21, 2008

CS200: Balanced Search Trees

PRIORITY QUEUES AND HEAPS

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Transcription:

AP Computer Science AB 007 Free-Response Questions The College Board: Connecting Students to College Success The College Board is a not-for-profit membership association whose mission is to connect students to college success and opportunity. Founded in 1900, the association is composed of more than 5,000 schools, colleges, universities, and other educational organizations. Each year, the College Board serves seven million students and their parents, 3,000 high schools, and 3,500 colleges through major programs and services in college admissions, 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 excellence and equity, and that commitment is embodied in all of its programs, services, activities, and concerns. College Board, Advanced Placement Program, AP, AP Central, SAT, and the acorn logo are registered trademarks of the College Board. PSAT/NMSQT is a registered trademark of the College Board and National Merit Scholarship Corporation. Permission to use copyrighted College Board materials may be requested online at: www.collegeboard.com/inquiry/cbpermit.html. Visit the College Board on the Web: www.collegeboard.com. AP Central is the official online home for the AP Program: apcentral.collegeboard.com.

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 JAVA. Notes: Assume that the classes listed in the Quick Reference found in the Appendix have been imported where appropriate. The java.util.stack and java.util.priorityqueue classes and the java.util.queue interface (page A in the Appendix) each inherit methods that access elements in a way that violates their abstract data structure definitions. Solutions that use objects of types Stack, Queue, and PriorityQueue should use only the methods listed in the Appendix for accessing and modifying those objects. The use of other methods may not receive full credit. Assume that the implementation classes ListNode and TreeNode (page A4 in the Appendix) are used for any questions referring to linked lists or trees, unless otherwise specified. ListNode and TreeNode parameters may be null. Otherwise, unless noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied. In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. Writing significant amounts of code that can be replaced by a call to one of these methods may not receive full credit. When Big-Oh running time is required for a response, you must use the most restrictive Big-Oh expression. For example, if the running time is On ( ), a response of On ( ) will not be given credit. --

1. The sliding puzzle is a popular puzzle toy and can be represented as a square grid in which all but one location contains a numbered tile. For example, the following diagram shows a 4 3 4 puzzle containing tiles numbered 1 through 15 and a single "hole." 1 4 3 1 11 5 7 13 14 6 10 8 15 9 The puzzle is solved by sliding tiles until all numbers are arranged in numerical sequence when traversed in rowmajor order, as shown in the following diagram. Note that the hole may be in any position. 1 3 4 5 6 7 8 9 10 11 1 13 14 15 The puzzle is represented by the SlidingPuzzle class shown below. In this question, you will implement two methods of this class. public class SlidingPuzzle private int side; // the side length of the puzzle grid private int[][] values; // the tile values with the hole represented by 0 /** @param sidelength the side length of the square grid * Precondition: sidelength > 0 public SlidingPuzzle(int sidelength) side = sidelength; values = new int[side][side]; initialize(); -3-

/** Precondition: the puzzle grid contains the distinct values 0 through side 1 * @return true if the tiles in the puzzle are all arranged in increasing order * (the hole value 0 may be in any position); * false otherwise public boolean isdone() /* to be implemented in part (a) /** Initializes the puzzle by placing numbers 0 through side 1 into random locations public void initialize() /* to be implemented in part (b) // There may be fields, constructors, and methods that are not shown. (a) Write the SlidingPuzzle method isdone. Method isdone returns true if the values in the puzzle appear in increasing order when traversed in row-major order; otherwise, it returns false. The value 0 (denoting the hole) may appear anywhere within the puzzle. Complete method isdone below. /** Precondition: the puzzle grid contains the distinct values 0 through side 1 * @return true if the tiles in the puzzle are all arranged in increasing order * (the hole value 0 may be in any position); * false otherwise public boolean isdone() -4-

(b) Write the SlidingPuzzle method initialize. Method initialize fills the side side values grid with random integers 0 through side 1, without repeating numbers. Your implementation must use the following algorithm. 1. Initialize an ArrayList<Integer> named temp with values 0 through n = side 1. The code for this step is provided. Starting with the first element of the grid values, repeat steps and 3 until the grid has been filled.. Pick a random element from temp and place that element into the next empty grid location. 3. Remove that element from temp by calling the ArrayList remove method on its index. Complete method initialize below. The method has been started for your convenience. /** Initializes the puzzle by placing numbers 0 through side 1 into random locations public void initialize() ArrayList<Integer> temp = new ArrayList<Integer>(); for (int j = 0; j < side * side; j++) temp.add(new Integer(j)); // Write your solution below. -5-

(c) What is the expected big-oh running time of the initialization algorithm described in part (b), in terms of n, the number of tiles? (d) Consider a variation of the algorithm described in part (b) in which Step 3 is changed. 1. Initialize an ArrayList<Integer> named temp with values 0 through n = side 1. Starting with the first element of the grid values, repeat steps and 3 until the grid has been filled.. Pick a random element from temp and place that element into the next empty grid location. 3. Replace that element with the element in the last index of temp, then remove the last element from temp. (Note: removing the last item of an ArrayList is a constant time operation.) What is the expected big-oh running time of this variation in terms of n, the number of tiles? -6-

. Consider a system for choosing pairs of people to be lab partners based on a compatibility score. A Person class (whose implementation is not shown) is used by the Pair class and the PairMatcher class. The Person class implements appropriate hashcode and equals methods. The declaration for the Pair class is shown below. A Pair object is constructed with two Person objects and has a method that calculates and compares the compatibility scores of two Pair objects. public class Pair implements Comparable /** @param p1 the first Person of the Pair * @param p the second Person of the Pair public Pair(Person p1, Person p) /* implementation not shown /** @return first Person of this Pair public Person getperson1() /* implementation not shown /** @return second Person of this Pair public Person getperson() /* implementation not shown /** @param other the object to be compared to this Pair * Precondition: other is a Pair object * @return the result of the comparison of the compatibility scores of this Pair and other public int compareto(object other) /* implementation not shown // There may be fields, constructors, and methods that are not shown. -7-

The PairMatcher class declared below will be used to maintain compatibility information about a group of people. A PairMatcher object is constructed with a list of Person objects. The constructor creates a mapping between each Person in the list and a priority queue of pairings between that Person and every other Person in the list. public class PairMatcher private Map<Person, PriorityQueue<Pair>> personmap; /** Initializes and fills personmap so that each Person in personlist is a key, * and the value associated with each key k is a PriorityQueue of Pair objects * pairing k with all other Persons in personlist * @param personlist a nonempty list of Person objects public PairMatcher(List<Person> personlist) /* to be implemented in part (a) /** @param p the Person to be matched * @param num the number of Person objects to remove * Precondition: if p is in personmap, then num is > 0 and less than or equal to * the number of pairs in the priority queue associated with p * @return an array of the num removed Person objects; * null if p is not in personmap public Person[] removenummatches(person p, int num) /* to be implemented in part (b) // There may be fields, constructors, and methods that are not shown. -8-

(a) Write the PairMatcher constructor. The constructor builds personmap such that each Person object in personlist is a key in the Map. The value associated with each key k is a PriorityQueue of Pair objects such that for all p in personlist, if p is not equal to k, there is a Pair in which k is the first Person and p is the second Person. For example, the following shows a list of Person objects and the map that would be created as a result of constructing a PairMatcher object with that list. The Pair objects in the map show the first Person, the second Person, and the compatibility score (lower scores mean better compatibility). personlist Jamie Chris Pat Terry Jamie : personmap Jamie Jamie Pat Chris (10) (11) Jamie Terry () Chris : Chris Jamie (11) Chris Terry (11) Chris Pat (1) Pat : Pat Jamie (10) Pat Chris (1) Pat Terry (3) Terry : Terry Chris (11) Terry Jamie () Terry Pat (3) Complete the PairMatcher constructor below. /** Initializes and fills personmap so that each Person in personlist is a key, * and the value associated with each key k is a PriorityQueue of Pair objects * pairing k with all other Persons in personlist * @param personlist a nonempty list of Person objects public PairMatcher(List<Person> personlist) -9-

(b) Write the PairMatcher method removenummatches. Method removenummatches removes the first num Pair objects from the priority queue associated with p in personmap and returns an array containing the second Person of each Pair that was removed. The Person objects in the returned array should be ordered by their compatibility with Person p. If Person p is not in the map, null is returned. Complete method removenummatches below. /** @param p the Person to be matched * @param num the number of Person objects to remove * Precondition: if p is in personmap, then num is > 0 and less than or equal to * the number of pairs in the priority queue associated with p * @return an array of the num removed Person objects; * null if p is not in personmap public Person[] removenummatches(person p, int num) -10-

3. A game called TreeBall has been created in which a player drops a ball into the top of the game board. The score is computed by summing the values in the nodes along the path that the ball follows. The game board for TreeBall is a full binary tree in which the root represents the top of the game board. For this question, assume that a full binary tree is one in which all leaves are at the same level and all non-leaf nodes have exactly two children. Each node in the tree contains an integer from 0 to 9. The diagram below shows an example TreeBall game board with 4 levels. In this example, the path with the greatest total is shown by the shaded nodes from the root (8) to the leaf (9). The score for this path is 5 = 8 + 7 + 1 + 9. The declaration for the GameBoard class is shown below. public class GameBoard private TreeNode root; // the root of the tree /** Creates a full binary tree rooted at root with numlevels levels * with a random integer from 0 to 9, inclusive, generated for each node * @param numlevels the number of levels in the tree * Precondition: numlevels > 0 public GameBoard(int numlevels) /* to be implemented in part (b) /** @return the maximum path score for this GameBoard public int getmaxscore() return getmaxhelper(root); /** @param current the root of the subtree to be processed * @return the maximum path score for the subtree rooted at current private int getmaxhelper(treenode current) /* to be implemented in part (a) // There may be fields, constructors, and methods that are not shown. -11-

(a) Write the GameBoard method getmaxhelper, which returns the maximum path score that can be obtained from the tree rooted at current. A path score is computed by summing the node values along the path from current to a leaf node. The maximum path score for an empty tree is 0. Complete method getmaxhelper below. /** @param current the root of the subtree to be processed * @return the maximum path score for the subtree rooted at current private int getmaxhelper(treenode current) (b) Write the GameBoard constructor, which creates a full binary tree with numlevels levels. Each node in the tree should contain an independently generated random Integer value from 0 to 9, inclusive. Recall that for this question a full binary tree has the property that all leaves are on the same level. You may find it useful to write and use a helper method to create the tree. Complete the GameBoard constructor below. /** Creates a full binary tree rooted at root with numlevels levels * with a random integer from 0 to 9, inclusive, generated for each node * @param numlevels the number of levels in the tree * Precondition: numlevels > 0 public GameBoard(int numlevels) -1-

4. This question involves reasoning about the code from the Marine Biology Simulation case study. A copy of the code is provided as part of this exam. Suppose that you want to visit all locations of a square environment in a single loop. There is a familiar interface for this purpose: the iterator. In this question, you will complete a method in a class EnvIterator that implements the Iterator interface. Here is an incomplete definition of the EnvIterator class. public class EnvIterator implements Iterator<Location> private Environment env; // the environment over which to iterate private Location loc; // the next location to be returned /** @param anenv the environment over which to iterate * Precondition: anenv is square, i.e., anenv.numrows() == anenv.numcols() public EnvIterator(BoundedEnv anenv) env = anenv; loc = new Location(0, 0); /** @return true if this EnvIterator has more elements * false otherwise public boolean hasnext() return env.isvalid(loc); /** Precondition: hasnext() returns true * Postcondition: loc has been updated to the successor location * @return the next location in the environment public Location next() /* to be implemented in part (a) /** Throws an UnsupportedOperationException since it is impossible to * remove a location from an environment. public void remove() throw new UnsupportedOperationException(); -13-

(a) Write the EnvIterator method next. The next method should return the next location in the environment, that is, the location that is referenced in the loc instance field when the method is called. The next method should also update the loc instance field to the successor location, as described below. Note that the first location returned by next is (0, 0) as initialized in the EnvIterator constructor. Your implementation of next should allow the iterator to visit all elements of a square BoundedEnv, following the diagonal pattern shown in the diagram below. The following describes the algorithm for determining the successor location in the diagonal pattern shown in the diagram above. 1. If the current location is at the bottom edge of the environment, move to the top of the next diagonal. For example, in the diagram given above, (4, 1) is followed by (, 4).. Otherwise, if the current location is at the left edge of the environment, move to the top of the next diagonal. For example, in the diagram given above, (1, 0) is followed by (0, ). 3. Otherwise, move down and left. For example, in the diagram given above, (1, 1) is followed by (, 0). Complete method next below. /** Precondition: hasnext() returns true * Postcondition: loc has been updated to the successor location * @return the next location in the environment public Location next() -14-

(b) A client class contains the method emptylocs, which returns a list of the first n empty locations when a given square environment env is traversed by an EnvIterator. If there are fewer than n empty locations in env, emptylocs should return all of them. For example, suppose the environment env is as shown in the diagram below where x indicates an occupied location. In this example, the call emptylocs(env, 5) returns a list of locations [(0, 1), (1, 0), (, 0), (0, 3), (1, )]. 0 1 3 0 x x 1 x x x 3 x Complete method emptylocs below. /** @param env the environment over which to iterate * Precondition: env is square, i.e., env.numrows() == env.numcols() * @param n the desired number of empty locations to be returned * Precondition: n > 0 * @return a list of the first n empty locations; * all empty locations if there are fewer than n empty locations. * Locations are ordered in the order in which they are visited by the EnvIterator public List<Location> emptylocs(boundedenv env, int n) STOP END OF EXAM -15-