CS 100J Prelim 2 Have a good break!!! 15 March 2007

Similar documents
Lecture 10. Overriding & Casting About

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

Prelim 1. CS 2110, 13 March 2018, 5:30 PM Total Question Name Short answer

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

Prelim 1, Solution. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

CS100J Prelim I, 29 Sept. 2003

Prelim 1. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

CSE wi Midterm Exam 2/8/18 Sample Solution

Comp Intermediate Programming EXAM #1 February 16, 2004 Rice University - Instructors: Cox & Nguyen

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

CS100J Prelim 1 22 February 2007

EXAMINATIONS 2009 MID-TERM TEST. COMP 202 / SWEN 202 Formal Methods of Computer Science / Formal Foundations of Software Engineering WITH ANSWERS

CS Prelim 2 Review Fall 2018

Question 0. (out of 01) Question 1. (out of 10) Question 2. (out of 12) Question 3. (out of 12) Question 4. (out of 12) Question 5.

C a; C b; C e; int c;

1B1b Classes in Java Part I

Name Section Number. CS210 Exam #3 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

3.1 Class Declaration

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

Prelim 1. CS 2110, March 15, 2016, 7:30 PM Total Question Name True False. Short Answer

public class Q1 { public int x; public static void main(string[] args) { Q1 a = new Q1(17); Q1 b = new Q1(39); public Q1(int x) { this.

CS 1110 Final, December 8th, Question Points Score Total: 100

CS211 Spring 2005 Prelim 1 March 10, Solutions. Instructions

CS 455 Final Exam Fall 2013 [Bono] December 12, 2013

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

CS 455 Final Exam Fall 2012 [Bono] Dec. 17, 2012

Assignment 4. Aggregate Objects, Command-Line Arguments, ArrayLists. COMP-202B, Winter 2011, All Sections. Due: Tuesday, March 22, 2011 (13:00)

CS 61B, Spring 1999 MT3 Professor M. Clancy

Prelim 1. CS 2110, March 15, 2016, 5:30 PM Total Question Name True False. Short Answer

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

1 (5pts) 2 (20 pts) 3 (20 pts) 4 (10 pts) 5 (15 pts) 6 (30 pts) Total NAME: CU ID: Recitation instructor/time

CS170 Introduction to Computer Science Midterm 2

Prelim 2, CS2110. SOLUTION

Prelim 1. Solution. CS 2110, 14 March 2017, 5:30 PM Total Question Name Short answer

CS/120 Final Exam. Name

The class Object. Lecture CS1122 Summer 2008

CS 1110 Final, December 8th, Question Points Score Total: 100

Computer Science 1 Ah

University of Massachusetts Amherst, Electrical and Computer Engineering

Midterm Exam 2 CS 455, Spring 2011

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

RECURSION (CONTINUED)

public UndergradStudent(String n, String m, String p) { programme = p; super(n, m);

CSE 214 Computer Science II Java Classes and Information Hiding

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

Domain-Driven Design Activity

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

University of Palestine. Mid Exam Total Grade: 100

Exam 1 - (20 points)

NAME: c. (true or false) The median is always stored at the root of a binary search tree.

Section 05: Midterm Review

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

CSE 331 Midterm Exam 2/13/12

Chapter Two: Classes and Objects

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

Final Exam CS 251, Intermediate Programming December 13, 2017

CS/ENGRD 2110 FALL2017. Lecture 4: The class hierarchy; static components

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Model Solutions. COMP 102: Test 1. 6 April, 2016

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

CS180. Exam 1 Review

CS/ENGRD 2110 SPRING Lecture 4: The class hierarchy; static components

CS1020: DATA STRUCTURES AND ALGORITHMS I

Total Score /1 /20 /41 /15 /23 Grader

Constants are named in ALL_CAPS, using upper case letters and underscores in their names.

CS 61B Discussion 5: Inheritance II Fall 2018

Assertions, pre/postconditions

CSE 331 Midterm Exam 11/9/15 Sample Solution

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

CSE 131 Introduction to Computer Science Fall Final Exam

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

CS 455 Midterm Exam 1 Fall 2015 [Bono] Thursday, Oct. 1, 2015

CS 455 Midterm 2 Fall 2017 [Bono] Nov. 7, 2017

CS 314 Exam 2 Spring

Prelim One Solution. CS211 Fall Name. NetID

COMP 102: Test August, 2017

Aggregation and Composition. [notes Chapter 4]

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

CS171 Midterm Exam. October 29, Name:

Final Examination CS 125 Introduction to Computer Science Fall Hours

CS 520 Theory and Practice of Software Engineering Fall 2017

Lecture 4: The class hierarchy; static components

Prelim 2, CS :30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

CS18000: Problem Solving And Object-Oriented Programming

Control Flow: Loop Statements

Programming II (CS300)

CIT-590 Final Exam. Name: Penn Key (Not ID number): If you write a number above, you will lose 1 point

CS536 Spring 2011 FINAL ID: Page 2 of 11

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

CS 1110 Prelim 2 November 6th, 2012

CSE 331 Spring 2018 Midterm

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

JAVA NOTES DATA STRUCTURES

CS201 - Assignment 3, Part 2 Due: Wednesday March 5, at the beginning of class

SYSC Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

Transcription:

CS 100J Prelim 2 Have a good break!!! 15 March 2007 This 90-minute exam has 6 questions (numbered 0..5) worth a total of 100 points. Spend a few minutes looking at all questions before beginning. Use the back of the pages if you need more space. Question 0 (2 points). Fill in the information, legibly, at the top of each page (Hint: do it now.) Question 1 (18 points) (a) What is a loop invariant? 0 out of 02 1 out of 18 2 out of 20 3 out of 20 4 out of 20 5 out of 20 Total out of 100 (b) Below is a loop. Fill in the invariant, then the initialization, then the loop body. // n > 0. // Assume that function f(int i) returns an int value. // Store in m the maximum value of f(i) for i in the range 0..n-1. int max= ; // invariant: for (int k= 1; k < n; k= k+1) { // m = maximum value of f(i) for i in the range 0..n 1 1

Question 2 (20 points): Below is a diagram of the x-y plane. Each point in the plane is determined by its x-coordinate and y-coordinate. In the diagram, point P has x-coordinate 3 and y-coordinate 5. /** instance is a pt in x-y plane */ public class Point { private int x; // x coordinate private int y; // y coordinate /** Constructor: Point with x coordinate a and y-coordinate b. */ public Point (int a, int b) { x= a; y= b; /** = x-coordinate */ public int getx() { return x; /** = y-coordinate */ public int gety() { return y; An instance of class Point contains the x-coordinate and y-coordinate of a point. Note that an object of class Point is immutable : there is no way to change its fields. On the back of the previous page, write the body of the procedure that is specified below. If you write a loop, you need not write a loop invariant, although it may help you to do so. /** Replace all points in v whose x- and y-coordinates are both negative by the corresponding points whose x- and y-coordinates are positive. E.g. replace a point (-3, -5) in v by point (3, 5). All other points in v remain unchanged. E.g. change v = [(-3, 5), (-3, -5), (-2, -6), (3, 5)] to [(-3, 5), (3, 5), (2, 6), (3, 5)] */ public static void makepos(vector< Point > v) You can use the following methods: Assume v has type Vector<C> for some class C Return Method Purpose C v.get(int k) = v[k]. int v.size() = the number of elements in v. v.set(int k, C ob) v.remove(int k) Store ob in v[k]. Remove element v[k], changing v so that it contains v[0..k 1] followed by v[k+1..] 2

Question 3 (20 points). Questions 3 and 4 deal with the three class definitions (of classes Animal, Elephant, and AsianElephant) that appear in the following two boxes: public class Animal { private String name; // name of this animal private int weight; // weight in pounds /** Constructor: animal with name n and weight w. */ public Animal(String n, int w) { name= n; weight= w; /** = name of this animal. */ public String getname() { return name; /** = weight of this animal, in pounds. */ public int getweight() { return weight; /** = Type of this animal. */ public String AnimalType() { return "Animal"; /** = "Animal a is in v*/ public static boolean isin(animal a, Vector<Animal> v) { for (int k= 0; k < v.size(); k= k + 1) { Animal b= v.get(k); if (a == b) return true; return false; public class Elephant extends Animal { private int ht; // elephant height in inches /** Constructor: instance with name n, weight w, and height h. */ public Elephant(String n, int w, int h) { super(n, w); ht= h; /** = height of this elephant. */ public int getheight() { return ht; /** = Type of this animal. */ public String AnimalType() { return "Elephant"; public class AsianElephant extends Elephant { /** Constructor: instance with name n, weight w, and height w. */ public AsianElephant(String n, int w, int h) { super(n, w, h); /** = "Asian Elephant". */ public String AnimalType() { return "Asian Elephant"; (a) What is the apparent type of a variable? What is the real type of a variable? What are the apparent and real types of variable v after execution of the following statement? Animal v= new AsianElephant( Ganesha, 2000, 60); 3

(b) Draw a folder (object) of class AsianElephant. Do not include the partition for class Object. (c) Each of the five cases below consists of a statement followed by an expression. Write the value of the expression after the statement is executed if you think that execution would lead to an error, then explain the error. Remember, it often helps to draw objects and variables. (1) Elephant e= new AsianElephant("Elephas", 10000, 120); "Elephant".equals(e.AnimalType()) (2) Animal c= new Elephant("Maximus", 9000, 100); c.animaltype() (3) AsianElephant b= (AsianElephant)(new Elephant("Indicus", 8000, 90)); b.animaltype() (4) Animal f= (Animal)(new Elephant("Loxodonta", 12000, 130)); f.getheight() 4

Question 4. (20 points). (a) Consider the statement shown below. Draw the frame for the call on static function isin, where isin is defined in class Animal on page 3. You do not have to assign argument values to parameters, and you do not have to execute the method body. We want to see only what the frame for the call looks like after it has been created. Boolean b= Animal.isIn(null, new Vector<Animal>()); (b) Write an instance method equals(object obj) for class Elephant in Question 3. To help you out, here is the beginning of the class definition for Elephant, showing its one field and method equals, whose body you have to write. public class Elephant extends Animal { private int ht; // elephant height in inches /** = "obj is an Elephant with the same values in its fields as this Elephant" */ public boolean equals(object obj) { 5

Question 5 (20 points). In Assignment A1 and A3, you implemented a class Rhino, part of which is shown below we put only the fields and methods needed for this question. The family tree of a rhino consists of that rhino, its known parents, their known parents, etc. The diagram to the right shows rhino r1 s family tree, which consists of r1, r1 s mother r2; r1 s father r3, and r2 s parents. Rhino r3 s parents are unknown. The diagram also shows r6 s family tree. Two rhinos are related if their family trees share a rhino. Thus, r1 r7 r8 r6 and r1 are related, because their trees share rhinos r2, r4, mother father and r5. r6 Write the body of static recursive function arerelated, which has been put into class Rhino. You may not use a loop; you must use recursion. Remember that two rhinos are related if there is some rhino that is in both of their trees. Also, if a Rhino variable is null, its family tree is empty. In solving this problem, think of all possible base cases first and write Java code for them. Finally, in dealing with the recursive case, you will may need several recursive calls. Remember, the idea in the recursive case is to solve the original problem in terms of the same kind of problems but on a smaller scale. public class Rhino { private Rhino father; // this Rhino s father (null if unknown) private Rhino mother; // this Rhino s mother (null if unknown) /** = Rhinos ra and rb are related */ public static boolean arerelated (Rhino ra, Rhino rb) { r3 father r5 father r2 r4 mother mother mother r9 father 6