Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from K&B mock test | Anonymous inner classes

 
Nidhi Bangur
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Yesterday, I took the mock test included in the CD with Kathy and Bert's book. I came across a question which tested my knowledge of anonymous inner classes.
The question was as follows (The words might not be exactly similar here):
___________________________________________________________________________
Given the following code definition, which two declarations of anonymous inner classes inerted at line 1, will compile successfully?
class Bing{
Bing(String s){
}
Bing(){
}
}
class Bang extends Bing{
Bang(String s){
super(s);
}

Bang(){
super();
}

// Line 1
}
___________________________________________________________________________
There were 2 correct answers to this question which are as follows:
1) Bing b = new Bang(){}
2) Bing b = new Bing(){"Foo";}
I have no problem with option 1 but how is option 2 correct here? I have tried inserting option 2 at line 1 and compiling the code but it doesn't compile and I am sure that this is not a correct syntax of instance initializer as well.
Has anybody else come across this question? Is the question wrong or if the option 2 is still correct, Please let me know how?
Thanks,
Nidhi.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2) Bing b = new Bing(){"Foo";}
Are you sure that's one the possible answers? I didn't come across to this possible answer.
[ October 02, 2003: Message edited by: Andres Gonzalez ]
 
Nidhi Bangur
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Andres, After your message I rechecked the question. The answers given are the same except that I missed one ";" at the end of both options while writing them here.
I am myself surprised as to how can it be the answer? Any comments from Bert?
Thanks,
Nidhi.
 
Cathy Bryant
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just did that same test, and have the same question. I tried running that code, and it doesn't compile. I don't see how "Foo" can be used in the class definition.
 
Steve Lovelace
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a typo. You can find a discussion of it here: link
[ October 02, 2003: Message edited by: Steve Lovelace ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic