• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rules Roundup 232

 
Steve Butcher
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#232 TRUE or FALSE. An inner class has free access to ALL member data of its enclosing class.
The "correct" answer is "true"
I marked "false" because a static inner class (see similar discussion in this folder) cannot access non-static members of its enclosing class. Since ALL is capitalized I figured this is the one case they were looking for that would make the statement false, not the general rule applying to inner classes.
I had similar problems with things like "x=false; y=true; what is the result of x^y?" Becaue one of the answers is "doesn't compile" I assume that meant either "they" were testing to see if ^ would work on booleans as well as ints or ints as well as booleans. But the typing of x and y aren't specified so if it is float x; float y; x=false; y=true; y=x^y; "doesn't compile" is the right answer. This seems especially true (that type should be specified to clearly delineate what is being tested for) when other questions specify the type for reasons germane to the question.
I understand how this is nit-picky...the question is some of the sample "SCJP" questions ARE this tricky and this nit-picky...how can you tell which is which?
Do they come with Question Modifiers like "GENERAL", "TRICKY" and "NITPICKY"?
Steve
sgwbutcher@aol.com

[This message has been edited by Steve Butcher (edited April 04, 2000).]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First question: A "static inner" class isn't really an inner class, it's a top-level class, and should properly be called a top-level nested class or a static nested class. See the Nested / Inner Classes Specification here to see where this is stated. A true inner class is always associated with an instance of the outer class, and does indeed have access to all member variables. The problem however is that various people, including Sun employees like Roberts and Heller, have perpetuated the incorrect usage of "static inner" so that any time you see the word "inner", it's no longer certain what it really means.
As far as I know, the term "static inner class" may appear on the test - if so, just pretend they said "static nested class" instead. All other times, assume "inner" means what it's supposed to mean - a nested class which is associated with an instance of the outer class. This does not include a static nested class.
Second question - I don't really see the problem here. If either x or y are boolean, the statement "x ^ y" will not compile. If either x or y are not boolean, the statements "x = true" or "y = true" will not compile. So either way, something will not compile.
In general, the questions will not be trying to trick you exactly, but they can depend on small points that are easy to overlook. But most people report that the actual questions are much better worded, and often easier, than most sample questions available online and in books.
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim
Having known the ambuiguity of what really is an inner class , for the Exam which concept shd we use. What is the correct answer for the above qstn for the Exam? .
regds
maha anna
 
Steve Butcher
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
1) yeah, what she said.
2) &, ^, and | are defined for boolean types so:
boolean x = true;
boolean y = false;
System.out.println( "" + ( x ^ y ) );
does compile (and run).
The problem for me is that when the question is framed as:
x = true;
y = false;

What is x ^ y?
and the answers are:
a) won't compile
b) false
c) true
How do you decide what they are testing you on?
(1) whether ^ is defined for boolean types (it is)
(2) what ^ means (XOR)
(3) whether x ^ y is legitimate (it is because XOR is defined for boolean types or it isn't because you have to assign the operation to something: x ^ y; by itself won't compile)
(4) whether assignment is possible without declaration (it isn't).
(4) was the least likely and the one I chose, quite stupidly, in retrospect. It was highly unlikely that that was the purpose of the question.
Howver, a similar problem crops up with the question: TRUE or FALSE: you can pass a double type to a method that takes a float type as an argument. To which I reply,
FALSE - if the double is not cast to a float
TRUE - if the double is cast to a float
The correct answer was TRUE (which I simply guessed that they were assuming you would cast it) but this kind of question still worries me--I know what I know but how do I know what I know won't trip up me proving what I know?
Your comments are somewhat reassuring which was the main thing I thing I was looking for by posting.
I like to know why I am getting questions wrong (1) I don't know the material, (2) I'm not reading the question carefully enough [happens all the time, "reading" overloading instead of overriding is my favorite ], (3) the question is ambiguous--if the question had provided one more jot or tittle of information, I would have answered it correctly. (1) and (2) are definitely my fault but (3)? Well, I'm not a mind reader much less a Borg--although I have been accused.
Thank you for your kind attention.
Steve
sgwbutcher@aol.com
[This message has been edited by Steve Butcher (edited April 05, 2000).]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Eh, who knows? I haven't heard of any question from the exam that depends on this definition (other than what I said, that they use the term "static inner class"). If any exist, I'd like to hear about them (not actual quotes of course, but some kind of summary of how "inner" was used in a question. I'm only talking about questions where it's an issue of whether top-level nested classes are included - we know what all the other inner classes are of course.
On reflection, I guess that (contrary to what I said earlier) for the exam (and only the exam), it would really be safer to assume "inner" includes all nested classes. Since we know it's at least sometimes used that way. In general, you must always be aware that it's an issue, and for a particular book or post you're reading, pay attention to whether the author has clarified which they mean. Until they do, you must be careful.
Note that the JLS 2nd ed. (draft) keeps the usage originally defined in the Nested/Inner Classes Specification. (And if they had fixed the name of that document in the first place there might be much less confusion than there is today.) The 2nd edition is much clearer than having to read the original JLS + amendments, and so I'm hoping that once it's officially published the "official" definintion of "inner" will catch on more completely. Of course, the certification exam will still have to be changed too.
2. Oops. I'm not sure where I got the idea ^ wasn't defined for booleans - it is. So my argument here falls apart. In this case, yes, we need to know the type of x and y, and the question is in error for not telling us. In the event this happens on the real exam, you just have to guess what they mean. In this case, note that "won't compile" isn't necessarily correct either, because it's possible that x and y are boolean. So there are two possible but exclusive answers, and we need to choose the one that is most likely what the author intended. I vote for assuming that x and y are indeed boolean, because otherwise the code author would have to be a complete idiot to think that "x = false" could possibly make sense.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic