Question

I had a question on a test that I thought was poorly worded (that was apparently on an actual AP test). I wanted to see what you guys think. Here it is:

A certain interface provided by a Java package contains just a single method:

publicinterface SomeName

{

int method(Object o);

}

A programmer adds some functionality to this interface by adding another method to it, method2:

publicinterface SomeName

{

int method1(Object ob1);

void method2(Object ob2);

}

As a result of this addition, which of the following is true?

(A) A ClassCastException will occur if ob1 and ob2 are not compatible.

(B) All classes that implement the original SomeName interface will need to be rewritten because they no longer implement SomeName.

(C)A class that implements the original SomeName interface will need to modify its declaration as follows:

public class ClassName implements SomeName extends method2

{ ...

(D) SomeName will need to be changed to an abstract class and provide implementation code for method2, so that the original and updated versions of SomeName are compatible.

(E) Any new class that implements the upgraded version of SomeName will not compile.

What do you guys think? Am I just retarded?

[1660 byte] By [gold-starDukesed] at [2007-11-26 12:12:53]
# 1

Some of the answers are poorly worded, but that's because they're deliberately wrong.

I guess you could argue that (D) is poorly worded in such a way that it comes close to being a possible right answer (sort of) but not enough, so it qualifies as wrong, but I guess maybe it's close enough that a reasonably smart person might get confused anyway. Is that what you're concerned about?

platinumsta at 2007-7-7 14:13:28 > top of Java-index,Archived Forums,Socket Programming...
# 2
> Is that what you're concerned about?Im concerned because I got it wrong! ;-)The right answer is actually B, but I didnt put that because its poorly worded in my opinion.
goldstar at 2007-7-7 14:13:28 > top of Java-index,Archived Forums,Socket Programming...
# 3
But B is the only correct answer.D sounds nice except that that doesn't fix anything. Changing an interface to an abstract class would break existing implementations (since interfaces are implemented and classes are extended).
silverstar at 2007-7-7 14:13:28 > top of Java-index,Archived Forums,Socket Programming...
# 4

> > Is that what you're concerned about?

>

> Im concerned because I got it wrong! ;-)

>

> The right answer is actually B, but I didnt put that

> because its poorly worded in my opinion.

I don't feel it's that poorly worded... more like tricky I suppose. But this is the way these tests are. While which it is not good is unlikely to change anytime soon.

silverstar at 2007-7-7 14:13:28 > top of Java-index,Archived Forums,Socket Programming...
# 5

What do you feel is wrong with (B)?It sounds pretty straightforward (and correct) to me.

(D) is wrong, but I can see how someone might think that something like it would be an approach one could take if a new method were needed.

Not that I think that (D) is the right approach. If you have an interface Fred, and an implementing classes Barney and Wilma, and you need a new method and have an implementation for it and you want all implementing classes to have it, then I'd argue that the correct solution is to create an abstract class Betty (actually a better name would be FredImpl), make it implement Fred, and make Barney and Wilma subclass Betty (aka FredImpl). That is, interfaces are good; if you created one it's probably because the application needed this purely conceptual type, and you don't want to get rid of one just because you have an implementation to use widely.

What answer did you select? What in particular confused you?

platinumsta at 2007-7-7 14:13:28 > top of Java-index,Archived Forums,Socket Programming...
# 6
I actually chose E. I was going to choose B, but the "because they no longer implement SomeName" part confused me I guess. I chose E because they would not compile since they do not implement method2.
goldstar at 2007-7-7 14:13:28 > top of Java-index,Archived Forums,Socket Programming...
# 7
(E) Any new class that implements the upgraded version of SomeName will not compile.I read new class and interpreted that as: if it is a new class then it would have the new method as well and therefore would compile.
goldstar at 2007-7-7 14:13:28 > top of Java-index,Archived Forums,Socket Programming...
# 8
> I read new class ...Yup, didn't catch that. ;-)
goldstar at 2007-7-7 14:13:28 > top of Java-index,Archived Forums,Socket Programming...
# 9

> I actually chose E. I was going to choose B, but the

> "because they no longer implement SomeName" part

> confused me I guess. I chose E because they would

> not compile since they do not implement method2.

The "first principle of reusable object-oriented design" advocated by the classic Gang of Four design patterns book is: "Program to an interface, not an implementation".

you should read some books about design patterns.

;)

bronzestar at 2007-7-7 14:13:28 > top of Java-index,Archived Forums,Socket Programming...
# 10

Ah, well, don't worry about it. Mulitiple-choice questions always lead to a degree of randomness in their answering. I mean, I'll bet everyone gets a few answers wrong on their SATs just because their eyes didn't focus on the right tiny circle they have to fill in. People who get 100% on their SATs have to be both smart and lucky.

(For those of you not in North America; the SATs are standardized tests in which students read multiple-choice questions from a booklet and select their answers by filling in tiny ovals in a tight grid on a separate sheet of paper. You have to use a #2 pencil. Then answer sheet is then scanned in and loaded into a computer. Maybe since I was in high school the computers have gotten smarter and don't rely on the students using a form that's designed for machines and not humans, but I doubt it.)

platinumsta at 2007-7-7 14:13:28 > top of Java-index,Archived Forums,Socket Programming...
# 11
> Maybe since I was in high> school the computers have gotten smarter and don't> rely on the students using a form that's designed for> machines and not humans, but I doubt it.Yup, they still use those. They have also added an essay.
goldstar at 2007-7-7 14:13:28 > top of Java-index,Archived Forums,Socket Programming...
# 12
> The "first principle of reusable object-oriented> design" advocated by the classic Gang of Four design> patterns book is: "Program to an interface, not an> implementation". Erm, how does that apply to what I said?
goldstar at 2007-7-7 14:13:28 > top of Java-index,Archived Forums,Socket Programming...
# 13
As a consolation, the question should not have said "Which is true". In my experience multiple choice questions should say "Which is the best answer". This is because you could put forward a reasonable argument for some of the other choices but B is clearly the best choice.
goldstar at 2007-7-7 14:13:28 > top of Java-index,Archived Forums,Socket Programming...