• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question on K&B question, expecting reply from Bert Bates himself...

 
Pad Ven
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
For the following question:

Given:
1. public class TestObj {
2. public static void main(String[] args) {
3. Object o = new Object() {
4. public boolean equals(Object obj) {
5. return true;
6. }
7. }
8. System.out.println(o.equals("Fred"));
9. }
10. }
What is the result?
A. An exception occurs at runtime.
B. true
C. fred
D. Compilation fails because of an error on line 3.
E. Compilation fails because of an error on line 4.
F. Compilation fails because of an error on line 8.
G. Compilation fails because of an error on a line other than 3, 4, or 8.



though I realized that the problem was a missing semicolon in line 7, I could not make out which of the choices and I ultimately made the wrong choice of B although I knew pretty well that it can't be. But I could never imagine the answer would be G. My understanding was compiler can throw error in a line after the erroneous line but never before. What is the real cause of my mistake? Is it my lack of understanding of Java compilation or Java language fundamentals or something else? Or is it that K&B questions are like this only (a little cryptic than actual exam questions)? Or can I expect these kind of questions in the exam? I am worried, since though I got the basics right, I haven't been able to choose the right answer and will not get the marks. Bert Bates, can you guide me in this case?

TVP
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, let's wait for Bert to respond. It's possible he's away for a few days, so be patient...

So if any body else knows what the problem is, we can discuss it later (when Bert gets back).
 
Ivan Rebrov
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not Bert, but couldn't help myself to test this code.
Compiler says, that error is in line "7", so the answer "G" is quite correct.

What's your problem?
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi TV,

it seems that this question is more about the wording of the question rather than the question itself. as a bit of background, we try to match the style and wording of our mock questions as closely as possible to the real exam questions. the only place where we knowingly differ is that we sometimes say "choose all that apply" and on the real exam you will always know exactly how many answers are correct. so...

the exam creation team uses the wording "due to an error on line x". you'll definitely see that wording several times on the real exam - the reason is that it's not possible to know what a particular compiler might say, but it is possible for you to know where the code has a problem.

does that help?

bert
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic