Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

OCP Practice Exams Sef-Assessment Test 1 Q8

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the Java OCP Java SE 6 Programmer Practice Exams book, on page 17 is the code fragment below :



Unfortunately, this code doesn't compile, giving the following error :

No enclosing instance of type Birthdays is accessible. Must qualify the allocation with an enclosing instance of type Birthdays (e.g. x.new A() where x is an instance of Birthdays).

. I tried this as shown in the second code below and it compiled BUT in the book, the answer is option A which is null instead of C which is the best answer given the code in the text book is NOT correct and they gave this explanation :

A is correct. The Friends class doesn't override equals() and hasCode(), so the key to the HashMap is a specific instance of Friends, not the value of a given Friends instance's name

. Given the code in the book, I couldn't even get it to compile. I tried going by the instruction given by the error message and modified it as shown below :



It compiles and runs but I got two questions:
1. Why is the code in the text book wrong ?
Since the code is wrong, the answer is supposed to D which is "Compilation fails" but the authors chose A which is null. There is no way one can get null given the code in the book.
2.Could anyone explain why the answer is null. I am not so sure I understood the explanation given in the text book.

Thanks
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

henry joe wrote:In the Java OCP Java SE 6 Programmer Practice Exams book, on page 17 is the code fragment below :



Unfortunately, this code doesn't compile, giving the following error :

No enclosing instance of type Birthdays is accessible. Must qualify the allocation with an enclosing instance of type Birthdays (e.g. x.new A() where x is an instance of Birthdays).

.



The reason it doesn't compile is because the Friends class is an inner class, and you can't instantiate an instance of an inner class without an instance of it's outer class.

Now, we could try to work with you to figure out how to use the inner class, but something tells me that you entered it wrong. In the book, have you learned inner classes yet? Meaning is the Friends class supposed to be inner class? Or should it have been just another class ?

[EDIT. Never mind. It looks like your example partially uses the inner class as an inner class, and partially doesn't. The part that doesn't is the incorrect part]

Henry
 
henry joe
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

henry joe wrote:In the Java OCP Java SE 6 Programmer Practice Exams book, on page 17 is the code fragment below :



Unfortunately, this code doesn't compile, giving the following error :

No enclosing instance of type Birthdays is accessible. Must qualify the allocation with an enclosing instance of type Birthdays (e.g. x.new A() where x is an instance of Birthdays).

.



The reason it doesn't compile is because the Friends class is an inner class, and you can't instantiate an instance of an inner class without an instance of it's outer class.

Now, we could try to work with you to figure out how to use the inner class, but something tells me that you entered it wrong. In the book, have you learned inner classes yet? Meaning is the Friends class supposed to be inner class? Or should it have been just another class ?

[EDIT. Never mind. It looks like your example partially uses the inner class as an inner class, and partially doesn't. The part that doesn't is the incorrect part]

Henry




you can check the code in the text book and you will see the Friends class is an inner class. I did instantiated the outer class to get it to work. I am now interested in understanding why output was null when I printed it to the console.
 
Henry Wong
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

henry joe wrote:
you can check the code in the text book and you will see the Friends class is an inner class. I did instantiated the outer class to get it to work. I am now interested in understanding why output was null when I printed it to the console.



Perhaps you can tell us what you think it should be, if it is not null.

Henry
 
henry joe
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

henry joe wrote:
you can check the code in the text book and you will see the Friends class is an inner class. I did instantiated the outer class to get it to work. I am now interested in understanding why output was null when I printed it to the console.



Perhaps you can tell us what you think it should be, if it is not null.

Henry



Please Henry Wong, I am just seeking to understand why they gave the answer as null. IF I knew, would I be here asking? I am a beginner java developer and interested in understanding some things that are not so clear with the language. The programme did output null but I want to know why?

 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry Joe,

The following works -



Regards,
Dan
 
Enthuware Software Support
Posts: 4584
45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

henry joe wrote:

Henry Wong wrote:

henry joe wrote:
you can check the code in the text book and you will see the Friends class is an inner class. I did instantiated the outer class to get it to work. I am now interested in understanding why output was null when I printed it to the console.



Perhaps you can tell us what you think it should be, if it is not null.

Henry



Please Henry Wong, I am just seeking to understand why they gave the answer as null. IF I knew, would I be here asking? I am a beginner java developer and interested in understanding some things that are not so clear with the language. The programme did output null but I want to know why?


Well, it is very simple actually. You put something in the Map and you are trying to retrieve it back. The way a map works is that first you associate a key with a value and to retrieve the value later on, you need to give that same key. Here, "same" means that the object that you gave as a key earlier should be "equal" to the object that you are giving now to retrieve the value. i.e. key1.equals(key2) should return true.

Do you think that is happening in your code for it to not print null?
 
henry joe
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Anilprem wrote:

henry joe wrote:

Henry Wong wrote:

henry joe wrote:
you can check the code in the text book and you will see the Friends class is an inner class. I did instantiated the outer class to get it to work. I am now interested in understanding why output was null when I printed it to the console.



Perhaps you can tell us what you think it should be, if it is not null.

Henry



Please Henry Wong, I am just seeking to understand why they gave the answer as null. IF I knew, would I be here asking? I am a beginner java developer and interested in understanding some things that are not so clear with the language. The programme did output null but I want to know why?


Well, it is very simple actually. You put something in the Map and you are trying to retrieve it back. The way a map works is that first you associate a key with a value and to retrieve the value later on, you need to give that same key. Here, "same" means that the object that you gave as a key earlier should be "equal" to the object that you are giving now to retrieve the value. i.e. key1.equals(key2) should return true.

Do you think that is happening in your code for it to not print null?



Thanks alot for the clarity. According to the text book, that code was wrong and I had thought the answer would be C which is : it won't compile. The reason is because you have to access the Friends methods through the Birthdays Object since Friends is an inner class but in the code, that was NOT the case. So, why did they give a wrong code and instead of choosing C as the answer, they chose D as the answer?
 
Paul Anilprem
Enthuware Software Support
Posts: 4584
45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

henry joe wrote:

Paul Anilprem wrote:

henry joe wrote:

Henry Wong wrote:

henry joe wrote:
you can check the code in the text book and you will see the Friends class is an inner class. I did instantiated the outer class to get it to work. I am now interested in understanding why output was null when I printed it to the console.



Perhaps you can tell us what you think it should be, if it is not null.

Henry



Please Henry Wong, I am just seeking to understand why they gave the answer as null. IF I knew, would I be here asking? I am a beginner java developer and interested in understanding some things that are not so clear with the language. The programme did output null but I want to know why?


Well, it is very simple actually. You put something in the Map and you are trying to retrieve it back. The way a map works is that first you associate a key with a value and to retrieve the value later on, you need to give that same key. Here, "same" means that the object that you gave as a key earlier should be "equal" to the object that you are giving now to retrieve the value. i.e. key1.equals(key2) should return true.

Do you think that is happening in your code for it to not print null?



Thanks alot for the clarity. According to the text book, that code was wrong and I had thought the answer would be C which is : it won't compile. The reason is because you have to access the Friends methods through the Birthdays Object since Friends is an inner class but in the code, that was NOT the case. So, why did they give a wrong code and instead of choosing C as the answer, they chose D as the answer?



I don't have the book so I am unable to comment on what is given in the book and why. But if you typed the code given in the book exactly as given, and if it doesn't compile, then the answer should be it doesn't compile.

I have seen it happen many times that people make some minor typo while typing the code and get an impression that the code given in a book is wrong. So you might want to double check

HTH,
Paul.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic