CS211 Prelim Oct 2001 NAME NETID

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

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

CS100J Prelim 1 22 February 2007

XC Total Max Score Grader

CS100J Prelim I, 29 Sept. 2003

Prelim 1. Solution. CS 2110, 14 March 2017, 7: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

CS 374 Fall 2014 Homework 2 Due Tuesday, September 16, 2014 at noon

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

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, 5:30 PM Total Question Name Short answer

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

Comp Intermediate Programming EXAM #2 April 02, 2003 Rice University - Instructors: Cox & Nguyen

Final Exam COMP 401 Fall 2014

Final Exam COMP 401 Fall 2014

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

Exam 1 Recursion, Induction, and Object-Oriented Programming

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

Lecture 10. Overriding & Casting About

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

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

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

CS 1110 Prelim 2 November 6th, 2012

Midterm Exam CS 251, Intermediate Programming October 8, 2014

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

CS 342 Software Design Fall 2018 Term Project Part II Addition of Artifacts and Data File Processing

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

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

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Final Exam CS 251, Intermediate Programming December 10, 2014

Csci 102: Sample Exam

CS/ENGRD 2110 SPRING 2018

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

Inheritance (Part 5) Odds and ends

Learning objectives. Reading carefully. Managing your time. CS2110 Fall 2017 Assignment A1. PhD Genealogy. See CMS for the due date

Objects and Classes Lecture 2

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

CS 61B (Clancy, Yelick) Solutions and grading standards for exam 2 Spring 2001 Exam information

Prelim 2, CS2110. SOLUTION

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

Homework Set 2- Class Design

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

1 Inheritance (8 minutes, 9 points)

CS211 Spring 2005 Prelim 1 March 10, Solutions. Instructions

CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Final Exam CS 152, Computer Programming Fundamentals May 9, 2014

Chapter 02 Building Multitier Programs with Classes

Final Exam CS 152, Computer Programming Fundamentals December 5, 2014

Subclassing for ADTs Implementation

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.

Self-test Java Programming

Exam Duration: 2hrs and 30min Software Design

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Interfaces and related issues Reading for these lectures: Weiss, Section 4.4 (The interface), p no multiple inheritance with classes

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

CIS 110: Introduction to computer programming

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 1

Project #2: Linear Feedback Shift Register

Software and Programming 1

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

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Prelim One Solution. CS211 Fall Name. NetID

ext Total Score /20 /20 /20 /20 /20 /5 Grader

Midterm Exam 2 CS 455, Spring 2015

» Access elements of a container sequentially without exposing the underlying representation

Lecture 5 Tao Wang 1

Topic 7: Algebraic Data Types

Highlights of Last Week

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

CS 1110, LAB 12: SEQUENCE ALGORITHMS First Name: Last Name: NetID:

Computer Science E-119 Fall Problem Set 3. Due before lecture on Wednesday, October 31

JAVA CONCEPTS Early Objects

Outline. 15. Inheritance. Programming in Java. Computer Science Dept Va Tech August D Barnette, B Keller & P Schoenhoff

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Lab 4 Due April 18 th

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

CS 1331 Fall 2016 Exam 2

MTAT Software Engineering. Written Exam 17 January Start: 9:15 End: 11:45

2. [20] Suppose we start declaring a Rectangle class as follows:

Sample Examination Paper Programming and Software Development

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

Casting. References. References

CS 101 Fall 2006 Midterm 3 Name: ID:

Midterm Exam CS 251, Intermediate Programming March 6, 2015

09/08/2017 CS2530 INTERMEDIATE COMPUTING 9/8/2017 FALL 2017 MICHAEL J. HOLMES UNIVERSITY OF NORTHERN IOWA TODAY S TOPIC: Exceptions and enumerations.

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

CS211 Spring 2005 Prelim 2 April 19, Instructions

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

Total Score /15 /20 /30 /10 /5 /20 Grader

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

C08: Inheritance and Polymorphism

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

CS61B - Exam 2 March 17, Consider the following example of a program that uses inheritance.

Maps and Trees. CS310: Program 2. 1 Overview. 2 Requirements. Instructor Provided Files. Late Work: Up to 72 hours Penalty: 10% per 24

Final Exam CS 251, Intermediate Programming December 13, 2017

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Recommendation: Play the game and attempt to answer the questions yourself without looking at the answers. You ll learn much less if you just look at

CS 61B, Midterm #3 and solutions, Spring 1996

Transcription:

This prelim has 4 questions. Be sure to answer them all. Please write clearly, and show all your work. It is difficult to give partial credit if all we see is a wrong answer. Also, be sure to place suitable comments --method specifications, variable definitions-- in your programs. Write your name and netid at the top of each page. Question 1 Recursion (20 points). Write a (static) recursive function that, given a nonnegative int n, yields the number of 1 s in its binary representation. For example, suppose n = 17. The binary representation of 17 is 10001, so the function would return 2. Your answer will be graded on (a) a good specification of the function (b) the correctness of your function ---a suitable base case and recursive case, both handled correctly. No points will be given if your program uses a loop. Question 1 out of 25 Question 2 out of 25 Question 3 out of 25 Question 4 out of 25 Total out of 100 The following facts may be useful: (1) The rightmost bit of the binary representation of n is 1 if and only if n is odd. (2) For n > 0, the binary representation of n is: (binary representation of n/2) followed by (the rightmost bit of the binary representation of n). 1

CS211 Prelim 1. 18 Oct 2001 NAME NETID Question 2. Classes and subclasses (20 points). This question is designed to determine whether you know the basics of subclasses and inheritance. To the right is a class Member. It contains more methods, but we have shown only the ones that you will need. Below, write a subclass Student of Member. An instance of the class has a name, address, and status, which should be one of the static constants FROSH, SOPH, JUNIOR, SENIOR of this class Student (don t forget to define these constants). The class should have a suitable constructor, tostring method, and a getter method (named getstatus) for the status. // A member of Cornell University public class Member { private String name; // member s name private Address add; // member s address // Constructor: a member with name n // and address b public Member(String n, Address b) { // = String representation of this Member public String tostring() { After writing the class, answer the following two questions. (a) Draw variable b and all the objects that are created by the following expression (you don t have to fill in the contents of the instance of class Address): Member b= new Student( Jack, new Address( ), Student.SOPH); (b) Below is a list of method calls, which refer to variable b above. Indicate which of these method calls is legal and, if legal, which method it refers to. (1) b.tostring() (2) b.getstatus() 2

Question 3. Interfaces (20 points). To the right is interface Enumeration, in case you have forgotten it. Each instance of the class below implements a set of at most 100 Animals, where Animals is some class. We have not shown all the methods of class SetOfAnimals because you won t need them. Method enumerate (see below) is supposed to return an Enumeration of the Animals currently stored in the instance. It does this by returning the value of the expression new Enum(). However, class Enum has not been written yet. Write class Enum as an inner class of class SetOfAnimals. Class Enum may need a field that indicates which element of the array to enumerate next. public interface Enumeration { // = there exists another element in the // enumeration public boolean hasmoreelements(); // = the next element in the enumeration. // Should be called only if it is known to // exist public Object nextelement(); public class SetOfAnimals { private Animal[] b; // There are n animals, and they private int n; // are stored in b[0..n-1] // Constructor: an instance that contains a set of 0 Animals public SetOfAnimals() { b= new Animal[100]; n= 0; // = an Enumeration of the Animals in this instance public Enumeration enumerate() { return new Enum(); 3

Question 4. Short questions (25 points) (a) What is meant by a base case (in a recursive procedure)? (b) What is a nested class? (c) Consider a class C with a method m. What are the two consequences of making C and m abstract? (d) Suppose function translateday(d,m,y) translates a date given by a day d, month m, and year d into the day number of that date within a year and returns that day number. For example, translate(2,1,2001) equals 2, since 2 January 2001 is day 2 of the year. This function throws a DateException if the given date is not a valid date. Write a statement that stores in a variable dn the value of the function call translateday(d1,m1,y1) ---but stores 1 in dn if translateday throws a DateException. Note: you do NOT have to write method translateday or class DateException. 4

CS211 Prelim 1 Answers. 18 Oct 2001 NAME David Gries NETID djg17 There is usually more that on way to answer a question. These are just sample solutions. Question 1. // = number of 1 s in the binary representation of n // Precondition: n >= 0 public static int numberofones(int n) { if (n == 0) return 0; return numberofones(n/2) + (n % 2); Question 2. public class Student extends Member { public static final int FROSH= 1; public static final int SOPH= 2; public static final int JUNIOR= 3; public static final int SENIOR= 4; private int status; // Constructor: instance with name n, address a, // and status s (one of the class constants) public Student(String n, Address a, int s) { super(n,a); status= s; // = a representation of this student public String tostring() { return super.tostring() + status: + status; // = the status of this Student public int getstatus() { return status; (a) the call b.tostring() is legal. It refers to tostring in the Student portion of the instance. (b) the call b.getstatus is illegal. You should be able to see the results of this prelim by noon tomorrow morning on the same website where you submit your assignments electronically. Question 3. Here is the inner class. Place it right after method enumerate. // An enumeration of the Animals in this instance public class Enum() { int k= 0; // index of next element of b to enumerate // = there is another Animal to enumerate public boolean hasmoreelements() { return k < n; // = the next Animal to enumerate // Precondition: another Animal to enumerate exists public Object nextelement() { k= k+1; return b[k-1]; Question 4. (a) A base case of a recursive method is a property of the parameters for which no recursive call is needed --the result is calculated directly. (b) A nested class is a static class that is defined within another class. (c) Abstract class C cannot be instantiated, and abstract method m must be defined in any subclass (that is not itself abstract). (d) try {dn= translateday(d,m,y); catch (DateException d) {dn= 1; a0 add a2 Member b a0 a1 Jack String name a1 Member status 2 Student Student tostring getstatus a2 whatever Address 5