30.01.2015 Views

COMP 132 Practice Midterm Exam I Solution 75 points name: 1. (12 ...

COMP 132 Practice Midterm Exam I Solution 75 points name: 1. (12 ...

COMP 132 Practice Midterm Exam I Solution 75 points name: 1. (12 ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The Pizza class does not implement the tastierThan method, even though it implements the Tasty<br />

interface.<br />

In main, an instance of Anchovy is passed as a parameter to the tastierThan method, but Anchovy<br />

does not implement the Tasty interface.<br />

In main, an instance of Salad is cast to Pizza - this would cause a ClassCastException at runtime.<br />

2. (5 <strong>points</strong>) Was the Minesweeper lab a good example of the Model-View-Controller design pattern If<br />

so, which parts of the code were the model and which parts were the view If not, why not<br />

Yes. The model is the MineSweeperBoard class because it maintains the state of the application and<br />

does all computation. The view is the MineSweeper class because it defines the GUI for the application.<br />

3. Suppose that class <strong>Exam</strong>pleClass in package comp<strong>132</strong>.exam1 declares a field as follows:<br />

public static final int TWO = 2;<br />

State whether or not each of the following method definitions would compile (your answer for each<br />

should just be yes or no):<br />

(a) (2 <strong>points</strong>) Assume this method is in class <strong>Exam</strong>pleClass in package comp<strong>132</strong>.exam<strong>1.</strong><br />

public void printFour() { System.out.println(TWO + 2); } Yes<br />

(b) (2 <strong>points</strong>) Assume this method is in class <strong>Exam</strong>pleClass in package comp<strong>132</strong>.exam<strong>1.</strong><br />

public void addTwo() { TWO = TWO + 2; } No<br />

(c) (2 <strong>points</strong>) Assume this method is in class SampleClass in package comp<strong>132</strong>.examples.<br />

public void printTwo() { System.out.println(comp<strong>132</strong>.exam<strong>1.</strong><strong>Exam</strong>pleClass.TWO); }Yes<br />

4. (8 <strong>points</strong>) What output is displayed by the following code snippet<br />

int [] [] mat = new int [4][4];<br />

for (int i = 0; i < mat.length; i++) {<br />

for (int j = 0; j < mat[0].length; j++) {<br />

mat[i][j] = (i + 1) * (j + 1);<br />

}<br />

}<br />

int j = mat[0].length - 1;<br />

for (int i = 0; i < mat.length; i++) {<br />

System.out.println(mat[i][j]);<br />

j = j - 1;<br />

}<br />

5. (5 <strong>points</strong>) Suppose that a program crashes because a method is called when its precondition is not<br />

satisfied. Is this the fault of the programmer who wrote the method, or of the programmer who wrote<br />

the code that called the method Why<br />

The programmer who wrote the code that called the method. The precondition is a contract between the<br />

method and calling code, and a method is only guaranteed to work correctly when its precondition is<br />

satisfied. Hence, the second programmer broke the contract by calling the method when its precondition<br />

was not satisfied.<br />

4<br />

6<br />

6<br />

4<br />

2

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!