Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

non-null object whose value is initialized - Abstract Class  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
14. Assuming we have a valid, non-null HenHouse object whose value is initialized by the
blank line shown here, which of the following are possible outputs of this application?
(Choose all that apply)



A. The code will not compile because of line 6.
B. The code will not compile because of lines 7–8.
C. The application will compile but not produce any output.
D. The application will output Cluck exactly once.
E. The application will output Cluck more than once.
F. The application will compile but produce an exception at runtime.




"Assuming we have a valid, non-null HenHouse object whose value is initialized by the
blank line shown here"

You can't instantiate an abstract class?

I'm trying to compile this in IDE but not sure what to put here: 5: HenHouse house = ______________

Can't understand it really if someone is able to help. Thank you
 
Rancher
Posts: 3453
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It could be:

where RealHenHouse is a concrete implementation of HenHouse.

Or:

where the HenHouse comes from that method call.

The point being that exactly how the HenHouse is instantiated is not relevant to the question, so they have left that part out.
All you need to assume is that house is valid and not null.
 
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My, I have seen some badly‑formatted code, but that is pretty bad. I know they like bad formatting for exam revision, however; it is intended to confuse you about the structure of the program. Always tell us where such code comes from, to avoid copyright problems, and to allow us to assess the source for ourselves.
Please don't add line numbers; the code tags add their own numbers.

What do they give as a correct answer? I made some changes and got “Cluck” thrice. I am sure it would be possible to get different results depending on what appears in line 5. I think that is a poor question because it can have several correct, but mutually exclusive, answers.
Where is there an abstract class? I can't see any. I can see an interface though. Of course you can create an instance of HenHouse; you simply need to create a class implementing that interface. Also have a look through this section of the Java™ Tutorials, where you will at least two other ways to implement that interface.
I suggest you read that part of the Java™ Tutorials until you know how to instantiate something in line 5. Also go through the code and see under which circumstances if any, you can get one Cluck or several or none. Also: did my changes involve more than correcting line 5 and improving the formatting?
 
Campbell Ritchie
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:. . . exactly how the HenHouse is instantiated is not relevant to the question . . .

I think it is relevant; it is possible to instantiate a hen house containing any non‑negative number of fowl. That is why I called it a bad question.
 
Dave Tolls
Rancher
Posts: 3453
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it's not relevant.
The sole method returns a List, which can either be null, empty, or have some Chickens in it, or it can throw an exception.

Those are the only options, which should give a pointer to the expected answers.
 
Niall Kev
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about code formatting. Will do in future. It is from Jeanne Boyarsky book under the unusually hard assessment test before chapter 1.

Plus I still don't understand putting in:

HenHouse house = new RealHenHouse();

HenHouse house = methodThatReturnsAHenHouse();

Do I need to declare methodThatReturnsAHenHouse() in HenHouse then and return something.









 
Campbell Ritchie
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends whether you only want it as an answer to the question, or whether you want to execute the code in real life.
 
Niall Kev
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to understand it and run it in Intellij.

Answer in book is:

14. D, E, F. The code compiles without issue, so options A and B are incorrect. If house
.getChickens() returns an array of one element, the code will output Cluck once, so
option D is correct. If house.getChickens() returns an array of multiple elements, the
code will output Cluck once for each element in the array, so option E is correct. Alternatively,
if house.getChickens() returns an array of zero elements, then the code will
throw an IndexOutOfBoundsException on the call to house.getChickens().get(0);
therefore, option C is not possible and option F is correct. The code will also throw an
exception if the array returned by house.getChickens() is null, so option F is possible
under multiple circumstances. For more information, see Chapter 2.
 
Master Rancher
Posts: 2545
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a reason why option C could be valid, see this  
 
Niall Kev
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok my mind is blown but I still don't understand what to put at line 5 to get the answers they suggest in the answer.
 
Piet Souris
Master Rancher
Posts: 2545
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Niall,

hope I didn't scare you too much!

Henhouse is an interface that has one abstract method: List<Chicken> getChickens();

The line to be filled is:
HenHouse house = ______________

So, on the right side we need something that returns an instance of a class that implements Henhouse.
Dave mentioned already two possibilities, but there are more.

1) what about an instance of an anonymous class?
  

2) Not for OCAJP, but to get an idea for the OCPJP:
  

3) (Dave's example) suppose you have a class: RealHenHouse implements Henhouse {...}
   Then you could have the line:

4) or suppose you have somewhere a method that returns a Henhouse, say
  
   Then the line could be:
  

5) and that clever Yuri created a Henhouse that would exit java alltogether, leaving no trail and thus no output (option c!)

Can you think of other possibilities?

For the assignment it doesn't matter what that line contains, as long as it delivers a Henhouse that is not null.
So you know you can invoke the method 'getChickens'.

Now, that method could deliver a null as list; what option would apply then? It could deliver a genuine list containing no chicken at all. What would 'list.get(0)' give in that case? And what option would apply then? And if the returned list contains just one chicken, or two, or..., what options would then apply?

 
Niall Kev
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:hi Niall,

hope I didn't scare you too much!

Henhouse is an interface that has one abstract method: List<Chicken> getChickens();

The line to be filled is:
HenHouse house = ______________

So, on the right side we need something that returns an instance of a class that implements Henhouse.
Dave mentioned already two possibilities, but there are more.

1) what about an instance of an anonymous class?
  

2) Not for OCAJP, but to get an idea for the OCPJP:
  

3) (Dave's example) suppose you have a class: RealHenHouse implements Henhouse {...}
   Then you could have the line:

4) or suppose you have somewhere a method that returns a Henhouse, say
  
   Then the line could be:
  

5) and that clever Yuri created a Henhouse that would exit java alltogether, leaving no trail and thus no output (option c!)

Can you think of other possibilities?

For the assignment it doesn't matter what that line contains, as long as it delivers a Henhouse that is not null.
So you know you can invoke the method 'getChickens'.

Now, that method could deliver a null as list; what option would apply then? It could deliver a genuine list containing no chicken at all. What would 'list.get(0)' give in that case? And what option would apply then? And if the returned list contains just one chicken, or two, or..., what options would then apply?




Sorry but I still don't get it. Your explanation appears very good but then when I try to come up with the code I get errors:




Wheres this RealHenHouse come from. Still lost or just stupid
 
Niall Kev
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I put HenHouse house = new HenHouse(); it says abstract class can't me instantiated
 
Dave Tolls
Rancher
Posts: 3453
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's where Piet's number 1 example comes in.  An anonymous class, where you defined the method where you instantiate the instance.

Assuming you know about those, then that's the easiest one to use to show all the different results.
For example, "If house.getChickens() returns an array of one element, the code will output Cluck once, so option D is correct.":

If you use that for line 5 then you'll see why 'D' is one of the correct answers.

You can then replace the return value with some of the other points in the answer.
Like return null;.
Or return Collections.emptyList(Chicken.class);.
Or a List with multiple chickens in it.
 
Campbell Ritchie
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:For a reason why option C could be valid, see this  

That is inconsistent with this stipulation in the question:-

Assuming we have a valid, non-null HenHouse object whose value is initialized by the blank line shown here . . .

The trick shown in that link doesn't return a valid non‑null reference. Since the question asks for a list of all possible outputs, D E F is actually correct and I was mistaken to say it was a bad question. Sorry.
I tried the methods shown above and got the different outputs: one Chicken: D, multiple Chickens: E, empty List: IndexOutOfBoundsException.
 
Niall Kev
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I finally got it to compile as below. I see you left some typo's to trick me ;) haha

The below produces Cluck once....

Thanks to all for your help


 
Campbell Ritchie
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Niall Kev wrote:Thank you. . . . Thanks to all for your help

That's a pleasure. Interesting question

Try the following variants:-One returns an empty List and the other a two‑element List.
 
Dave Tolls
Rancher
Posts: 3453
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That is inconsistent with this stipulation in the question:-

Assuming we have a valid, non-null HenHouse object whose value is initialized by the blank line shown here . . .

The trick shown in that link doesn't return a valid non‑null reference.



Except it does.
It's the method that doesn't, but the question doesn't stipulate what the method does, it only stipulates that the HenHouse is non-null.

It's why I mentioned that the method could itself throw an exception, and I didn't even think of it calling System.exit() otherwise I would have mentioned that as well.

It's one of the (many) reasons I expect I'd be rubbish at doing these sorts of tests.
 
Campbell Ritchie
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the HenHouse reference points to a valid non‑null object in that case, where is it?

I feel this thread is beginning to go round and round in little circles while we argue about whether the reference in line 5 is valid or not.
 
Dave Tolls
Rancher
Posts: 3453
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The whole point behind the question is to ask "what could possibly happen in this code assuming that line 5 results in a valid non-null HenHouse".

Which means we aren't meant to know the internals of the method.

I suspect, based on the answers that are considered valid, and the explanation of those answers, that it ought to also say that the getChickens method returns successfully.
Without that it's possible for the call to getChickens to throw an Exception, or (as in the link above) simply System.exit.

Though I am probably overthinking it all...
 
Campbell Ritchie
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But throwing an exception or calling System.exit will mean the reference on line 5 never gets its non‑null status.
 
Dave Tolls
Rancher
Posts: 3453
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:But throwing an exception or calling System.exit will mean the reference on line 5 never gets its non‑null status.



No, that's not line 5.
That's getChickens, I'm talking about.

If getChickens looks like:

then HenHouse is valid, and non-null, but you'd get an UnsupportedOperationException from line 6, when getChickens is called.

Similarly:

HenHouse is still valid and non-null, but this time you'd not see a thing.
 
Campbell Ritchie
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that would fit the letter of the law, but it still looks like quibbling to me.
 
Dave Tolls
Rancher
Posts: 3453
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Yes, that would fit the letter of the law, but it still looks like quibbling to me.



That's a sizeable amount of my job.
Requirements haggling.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!