



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
new- exp. Max. 1. 42. 20. 14. 8. 12. 3. 100. Score. Grader. The exam is closed book and closed notes. Do not begin until instructed. You have 90 minutes.
Typology: Exercises
1 / 7
This page cannot be seen from the preview
Don't miss anything!




Name: NetID:
1 2 3 4 5 6 7 Total Question Name Short answer
OO Recursion Loop invariants
Exception handling
new- exp Max 1 42 20 14 8 12 3 100 Score Grader The exam is closed book and closed notes. Do not begin until instructed.
You have 90 minutes. Good luck!
Write your name and Cornell NetID, legibly, at the top of the first page, and your Cornell ID Number (7 digits) at the top of pages 2-7! There are 6 questions on 7 numbered pages, front and back. Check that you have all the pages. When you hand in your exam, make sure your pages are still stapled together. If not, please use our stapler to reattach all your pages!
We have scrap paper available. If you do a lot of crossing out and rewriting, you might want to write code on scrap paper first and then copy it to the exam so that we can make sense of what you handed in.
Write your answers in the space provided. Ambiguous answers will be considered incorrect. You should be able to fit your answers easily into the space provided.
In some places, we have abbreviated or condensed code to reduce the number of pages that must be printed for the exam. In others, code has been obfuscated to make the problem more difficult. This does not mean that it’s good style.
Academic Integrity Statement: I pledge that I have neither given nor received any unau- thorized aid on this exam. I will not talk about the exam with anyone in this course who has not yet taken prelim 1.
(signature)
Write your name and NetID, legibly, at the top of page 1. Write your Student ID Number (the 7 digits on your student ID) at the top of pages 2-7 (so each page has identification).
(a) 6 points. Below are three expressions. To the right of each, write its value.
(b) 8 points. Circle T or F in the table below.
(a) T F Because of automatic conversion, int x= 1.0; is valid Java. false A double can’t be converted to an int without an explicit cast. (b) T F It’s possible to overload methods by swapping arguments of different types, e.g. foo(int x, char y) and foo(char y, int x). true The order of arguments is part of the method signature. (c) T F a[a.length] refers to the last item in the array a. false The last index is a.length-1. (d) T F An object whose class implements Comparable can be stored in a variable of type Comparable. true Polymorphism in Java allows this. (e) T F The method int getLength(String x) { return x.length(); } can throw a NullPointerException. true if x is null, calling length() on it will do so. (f) T F A static method m can be called only from other static methods. false Static method m can be called from a non-static method using ClassName.m(). (g) T F Access modifier private denies access to unrelated classes but allows superclasses to access the variable or method. false private denies access to all other classes. (h) T F A class cannot have more than one constructor. false You can overload the constructor with different arguments.
(c) 3 points. Does the code to the right compile? If not, explain why. Give your answer directly below. Specifications have been removed to make it easier to see the code.
No. Class Porsche doesn’t imple- ment abstract method makeNoise.
public abstract class Vehicle { public abstract void makeNoise();
public void numDoors() { System.out.println("I have 4 doors"); } }
public class Porsche extends Vehicle { @Override public void numDoors() { System.out.println("I have 2 doors"); } }
(g) 8 points. Consider the declarations to the right. These statements are syntactically correct and compile:
B b= new B(); I1 i1= b;
Suppose these two statements are followed by the four statements below. For each, circle yes if it compiles and no if it doesn’t, and also blot out completely the uncircled word (this may help in grading)
no yes String s= i1.toString(); no yes I2 k= b; no yes I2 k2= i1; no yes I2 k3= (I1) i1;
interface I1 { }
interface I2 { }
class A implements I1 {}
class B extends A implements I1, I2 {}
(a) 10 points. To the right is the beginning of class Animal, with two fields and method addToDiet (which you don’t have to write). Below, complete the constructor and method equals.
class Animal { private String name; // list of things it eats, not null private ArrayList
/** Add nF to this Animals diet. */ public void addToDiet(String nF){...}
/** Constructor: instance with name name and empty diet. */ public Animal(String name){ this.name= name; diet= new ArrayList<>();
/** Return true if this and ob are objects of the same
if (ob == null || getClass() != ob.getClass()) return false; Animal obA= (Animal) ob; return obA.name.equals(name);
(b) 10 points. To the right is the beginning of class Rabbit, which extends class Animal of part (a). It has one field. Below complete the constructor, method addToDiet, and method equals, all of which go in class Rabbit.
class Rabbit extends Animal { // how high it jumps private int height;
/** Constructor: rabbit with name name, empty diet, jumps h */ public Rabbit(String name, int h) { super(name); height= h;
/** Add nF to the diet only if it is "carrot". */ @Override public void addToDiet(String nF) { if (nF.equals("carrot")) super.addToDiet(nF);
/** Return true if this and ob are objects of the same class
(a) 4 points Method F to the right calculates Fi- bonacci numbers. Below, write the calls made in eval- uating the call F(3) in the order they are called, start- ing with F(3)
F(3) F(2) F(1) F(0) F(1)
/** Return Fibonacci number n.
Consider method mystery. Function s.toString() returns the value of s. To the right of the method are four calls on mystery. Under each write (1) the output printed by the call (on one line is OK), including any exception that is not caught, and (2) the value returned by the call (if there is one).
public static int mystery(String s) { try { s= s.toString(); System.out.println("B"); int k= Integer.parseInt(s); System.out.println("C"); int[] b= {50, 49, 49, 48}; return b[k]; } catch (NumberFormatException e) { System.out.println("D"); throw new RuntimeException(); } catch (NullPointerException e) { System.out.println("E"); return -1; } catch (Throwable e) { System.out.println("F"); } return -2; }
(a) 3 points mystery("3"); Output: Return: B C 48
(b) 3 points mystery(null); Output: Return: E -
(c) 3 points mystery("10"); Output: Return: B C F -
(d) 3 points mystery("mystery"); Output: Return: B D RuntimeException
Write the 3-step algorithm for evaluating the new-expression new ABC(5).