Questions on Introduction to Computing Using Java - Exam 3 | CS 1110, Exams of Computer Science

Material Type: Exam; Professor: Gries; Class: Introduction to Computing Using Java; Subject: Computer Science; University: Cornell University; Term: Spring 2009;

Typology: Exams

Pre 2010

Uploaded on 08/30/2009

koofers-user-vg9
koofers-user-vg9 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Cornell net id ________________________ Name ___________________________
Section day ________________________ Section time _______________________
1
CS 100J Prelim 3 15 April 2008
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 answering any. Budget your time wisely. Use the back of the pages, if you
need more space. We have a stapler at the front of the room, so you can tear the pages apart.
Question 0 (2 points). Write your netid and your name, legibly, at the top of each page (Hint: do it now).
Question 1 (18 points) Recursion. Write function hasName, defined below in
class Rhino —we put only the fields and methods needed for this question. Do not
use loops; you must use recursion. Do not write any other methods.
To the right is a possible tree for a rhino, showing the rhino (r1) and its known
ancestors.
Note that hasName is not a static function; it appears in every object of class
Rhino.
public class Rhino {
private Rhino father; // this Rhino’s father (null if unknown)
private Rhino mother; // this Rhino’s mother (null if unknown)
private String name; // this Rhino’s name
/** = “this rhino or one of its ancestors has name n. */
public boolean hasName(String n) {
}
father
mother
r1
r4
r5
mother
r3
r2
father
pf3
pf4
pf5

Partial preview of the text

Download Questions on Introduction to Computing Using Java - Exam 3 | CS 1110 and more Exams Computer Science in PDF only on Docsity!

Section day ________________________ Section time _______________________

CS 100J Prelim 3 15 April 2008

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 answering any. Budget your time wisely. Use the back of the pages, if you need more space. We have a stapler at the front of the room, so you can tear the pages apart. Question 0 (2 points). Write your netid and your name, legibly, at the top of each page (Hint: do it now). Question 1 ( 18 points) Recursion. Write function hasName, defined below in class Rhino —we put only the fields and methods needed for this question. Do not use loops; you must use recursion. Do not write any other methods. To the right is a possible tree for a rhino, showing the rhino (r1) and its known ancestors. Note that hasName is not a static function; it appears in every object of class Rhino. public class Rhino { private Rhino father; // this Rhino’s father ( null if unknown) private Rhino mother; // this Rhino’s mother ( null if unknown) private String name; // this Rhino’s name /** = “this rhino or one of its ancestors has name n. */ public boolean hasName(String n) { } father mother r r5 r mother r3 r father

Section day ________________________ Section time _______________________ Question 2 (20 points). Exceptions. (a) What is the output of the call P3.foo(), given the following definition for class P3? class P3 { private static int calls = 0; public static void foo() { try { bar(); System.out.println(“first bar done”); bar(); } catch (Exception e) { System.out.println(“Exception!!”); } } private static void bar() throws Exception { if (calls > 0) System.out.println(“Call number “ + calls); else throw new Exception(); } } (b) On the back of the previous page, write a subclass BadNumberException of class Exception (which is a subclass of Throwable). It needs the usual two constructors. (c) The specification of the following function has a precondition. (The function calculates the greatest common divisor gcd(b,c) of b and c.) To make it easier to write robust code that calls this function, change the function to throw a BadNumberException (with a suitable detail message) if the precondi- tion is false — first change the specification; then change the body. Use the back of the previous page for your answer. In writing the body, you don’t have to copy the cde shown below, but you do have to show where it goes. You don’t have to understand completely how the body works, but if you are interested, note that it rests on two properties: (1) gcd(b, c) = gcd(c, b), and, for c > b, gcd(b, c) = gcd(b, c-b) be- cause anything that divides b and c also divides b and c-b, and vice versa. /** = the greatest common divisor of x and y. Precondition: x > 0 and y > 0. */ public static int GCD( int x, int y) { int b= x; int c= y; // invariant: gcd(x, y) = gcd(b, c) and b > 0 and c > 0 while (b != c) { if (b < c) c= c – b; else b= b – c; } return b; }

Section day ________________________ Section time _______________________ (b.) Now complete function isMagicSquare, whose specification is given below. Here are some ground rules.

  1. There should be one set of nested for-loops —do not write more for-loops or while-loops.
  2. Do not use recursion.
  3. We do not give you a postcondition or invariant for the loops. You do not have to write them, but it may help you to do so. /** = “sq is a magic square with sum sum”. Precondition: sq is not null and indeed is a square array. */ public static boolean isMagicSquare( int [][] sq, int sum){ }

Section day ________________________ Section time _______________________ Question 4 (12 points). Classes. Answer the follow questions in a few sentences: (a) Explain the "Inside out rule". (b) Explain the two uses of this and super in Java. (c) What is meant by "overriding a method"? How does one call an overridden method from within the class that does the overriding?