CS100J Prelim 1 22 February 2007

Similar documents
CS100J Prelim I, 29 Sept. 2003

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

CS211 Prelim Oct 2001 NAME NETID

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

Exam 1 Recursion, Induction, and Object-Oriented Programming

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

CS 1110 Prelim 1 October 4th, 2012

CS 1110 Prelim 2 November 6th, 2012

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

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

CS 1110 Prelim 2 November 14th, 2013

Prelim 1. CS2110, October 2, 2014, 5:30 PM Extra Total Question TrueFalse Multiple Object Oriented

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

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

Prelim 1. CS 2110, 14 March 2017, 7:30 PM Total Question Name Short answer. OO Recursion Loop invariants Max Score Grader

Prelim 1. CS 2110, 14 March 2017, 5:30 PM Total Question Name Short answer. OO Recursion Loop invariants Max Score Grader

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

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

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

CS 1110 Final Exam May 9th, 2013 SOLUTIONS

CS/ENGRD 2110 SPRING Lecture 5: Local vars; Inside-out rule; constructors

CS 1110 Prelim 2 November 10th, 2016

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

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

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

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

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

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

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

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

CS211 Spring 2005 Prelim 1 March 10, Solutions. Instructions

CS 1110 Prelim 2 November 12th, 2015

CS 1110 Prelim 2 April 22, 2014

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

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

Exam Duration: 2hrs and 30min Software Design

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

Java Object Oriented Design. CSC207 Fall 2014

XC Total Max Score Grader

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

CS 1110 Prelim 1 March 7th, 2013

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

CS 116 Week 8 Page 1

CS 1110 Prelim 1 October 17th, 2013

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

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.

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

Prelim 2 Solution. CS 2110, 24 April 2018, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph.

Prelim 2 Solution. CS 2110, 24 April 2018, 5:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

CS 1110 Prelim 2 November 13th, 2014

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

CS 1110 Final, December 16th, 2013

CS 1110 Regular Prelim 1, March 14th, 2017

Midterm 2 Grading Key

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

Introduction to Programming Using Java (98-388)

CS 1110 Prelim 2 November 12th, 2015

CSE351 Winter 2016, Final Examination March 16, 2016

Inheritance (Part 5) Odds and ends

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

Chapter 6 Introduction to Defining Classes

Csci 102: Sample Exam

C12a: The Object Superclass and Selected Methods

5.6.1 The Special Variable this

CS 455 Midterm Exam 1 Fall 2017 [Bono] Thursday, Sep. 28, 2017

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

1B1b Classes in Java Part I

Week 5-1: ADT Design

Prelim 2. CS 2110, 24 April 2018, 5:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Distributed Systems Recitation 1. Tamim Jabban

Prelim 2. CS 2110, 24 April 2018, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph.

CSCI 102 Fall 2010 Exam #1

CMSC131. Objects: Designing Classes and Creating Instances

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Software and Programming 1

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

Distributed Systems Recitation 1. Tamim Jabban

Last name:... First name:... Department (if not D-INFK):...

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

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

CS 455 Final Exam Fall 2015 [Bono] Dec. 15, 2015

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

CS 251, Intermediate Programming Midterm Exam October 9, 2013

Last Name: First: Netid: Section. CS 1110 Final, December 17th, 2014

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

JAVA MOCK TEST JAVA MOCK TEST II

Making New instances of Classes

CS 1110 Final, December 16th, 2013

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #9. Review is-a versus has-a. Lecture #9 Inheritance I

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Prelim 2. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs. Max Score Grader

When the above code runs, which type s execute method will be run? When the above code runs, which type s execute method will be run?

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

CS 1110 Prelim 1 October 12th, 2017

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

CS 1110 Prelim 1 October 15th, 2015

Darrell Bethea June 6, 2011

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Transcription:

NAME Cornell Net id 1/5 CS100J Prelim 1 22 February 2007 This 90-minute exam has 6 questions (numbered 0..5) worth a total of 100 points. Scan the whole test before starting. Budget your time wisely. Use the back of these pages if you need more space. You can tear the pages apart; we have a stapler at the front of the room. Question 0 (2 points): Write your name and NetID, legibly, at the top of each page. Question 1 (18 points): Answer the following questions concisely: (a) 5 pts. What is an argument? A parameter? Q 0 /02 Q 1 /18 Q 2 /30 Q 3 /10 Q 4 /20 Q 5 /20 (b) 5 pts. What is a local variable? What is its scope? Total /100 (c) 5 pts. Explain the three steps in evaluating a new-expression (e.g. new Time(c,d)). The previous sentence contains an example of a new-expression, but your answer should explain what any new-expression does and not simply the one in the example. (d) 3 pts. Draw a frame for the call m(2+3, 6) of the following procedure m. We want to see what the frame for the call looks like after the argument values are assigned to the parameters but before the method body is executed. public void m(int x, double y) { int z; z= x + y;

NAME Cornell Net id 2/5 Question 2 (30 points). Use the back of the next page to answer this question. (a) 8 pts. At the bottom of the page are two class definitions. Draw one folder (object, instance) of each. Do not show the partition for superclass Object. (b) 12 pts. Write a class definition for a class PercussionInstrument that 1. Is a subclass of MusicInstrument; 2. Has suitable specifications on methods and definitions on fields; 3. Has a field numdrums, which is the number of drums in this percussion instrument; 4. Has a constructor with the name of the instrument and the number of drums as parameters; 5. Overrides function play() to return the number of druuums, similar to the way function play in class StringInstrument works. (c) 10 pts. Suppose we want to keep track of the number of objects of class MusicInstrument that have been created. Write the declaration of the variable that will contain this value (this variable will be defined in class MusicInstrument). Then, rewrite the constructors in the class to maintain this variable. (Don't change the constructors on this page; just rewrite them on the back of the next page.) /** An instance represents an instrument */ public class MusicInstrument { // the instrument s name private String name= null; /** Constructor: an instrument with name s */ public MusicInstrument(String s) { name= s; /** Constructor: an instrument with name "" */ public MusicInstrument() { name= ""; /** = sound this instrument makes*/ public String play() { return music ; /** = a repr of this instrument */ public String tostring() { return "Instrument: " + name; /** An instance represents a string instrument with no name */ public class StringInstrument extends MusicInstrument { /** number of strings on this instrument */ private int numstrings; /** Set the number of Strings on this instrument to n */ public void setnumber(int n) { numstrings= n; /** = sound this instrument makes */ public String play() { return super.tostring() + numstrings + triiings ;

NAME Cornell Net id 3/5 Question 3 (10 points). Consider classes MusicInstrument and StringInstrument of question 2. Write a subclass Violin of StringInstrument that: 1. Has suitable specifications on methods and definitions on fields; 2. Has a String field named manufacturer; 3. Has a constructor with this specification: /** Constructor: a violin with n strings made by manufacturer s and no name */ public Violin(int n, String s) 4. Has a constructor with the specification shown below. The body of this constructor should be a single statement. /** Constructor: a violin with 6 strings made by manufacturer s and no name*/ public Violin(String s)

NAME Cornell Net id 4/5 Question 4 (20 points). Below is a definition of class Student. Assume the following three assignment statements are executed: Student p1= new Student( Bill, bk12 ); Student p2= new Student( Bill, bk13 ); Student p3= new Student( William, bk12 ); (a) What is the value of each of the following four expressions? p1.equals(p2) p1.equals(p3) p1 == p2 p1 == p3 (b) Now consider these statements: p1= new Student( Bill, bk12 ); p2= new Student( Bill, bk13 ); p3= p2; p3.setname( Jack ); Below, first draw all three variables. Then execute the four statements of course, draw any objects that are created during execution. /** An instance represents a Student*/ public class Student { // the student s name private String name; // the student s netid private String netid; /** Constructor: a Person with name n and netid i*/ public Student(String n, String i) {name= n; netid= i; /** = the Student s name */ public String getname() {return name; /** set the Student s name to n */ public void setname(string n) {name= n; /** = this Student and s have the same netid */ public boolean equals(student p) {return netid.equals(p.netid); /** = a representation of this Student*/ public String tostring() {return netid + ":" + name;

NAME Cornell Net id 5/5 Question 5: (20 pts) Write function fix, which is specified below. You may use the following methods (you may not need them all). This might help you: when you break String s up into pieces, store the pieces in local variables and then use these pieces. Return Method Purpose char s.charat(i) = the character at position i of s int s.length() = the number of characters in s int s.indexof(n) = the index within s of the first occurrence of String n (-1 if none) int s.indexof(n, k) = the index within s of the first occurrence of String n that begins at or after index k (-1 if none) String s.substring(h,k) = a String consisting of characters in s[h..k-1], ie. s[h], s[h+1],..., s[k-1] String s.substring(h) = a String consisting of characters s[h..s.length()-1] /** = Date s in a more suitable form. Precondition: s contains a date in the form month/day/year, with each part separated by "/". Examples are: 4/26/39 and 04/005/1939. The output should be in the form year.month.day. Examples are: 39.26.4 and 1939.04.005. Each of day, month, and year may be any length. They appear in exactly the same form in the input and output; just their order and the separator are changed. */ public static String fix(string s) {