• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Inner Classes

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
In this following program,
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

I thought Answer is F(because before line 8 there sholud be statement seperator ';').But in the book it shows the correct answer is G.
Can anybody clarify this answer?It will be very helpful for me.Thanks.
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a mistake in the book. You are correct the correct answer is F.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect if F is the correct answer. Line 3 starts with anonymous class
implementation. Eclipse gives error at line 7 as your code goes. Rather answer
'G' looks more genuine.


Thanks,
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
G is the correct answer.
The compilation error is at Line 7(semicolon missing).
 
kathir vel
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we compile this program ,we get error like this...

TestObj.java :8: ';' expected
System.out.println(o.equals("Fred"));

This error shows ..so it is in Line 8.Isn't it?
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is because of the curly brace at line 7. At line 3, instead of semi colon, a open curly brace is used, because of that the compiler reports that semicolon is missing at line 8, since the block opened at line 3 ends at line 7.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI i confuced about this
Compiler giving error at line 8:
but the couse is because of missing semicolon at line 7
but the statement started at line 3 (3 to 7 only one statement)
what shuold be the correct answer
regards
Mallik
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

the question asks which line contains the error and not which line is reported by the compiler. And I think there is no doubt that line 7 is the faulty one.
 
Sandeep Bangalore
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me make it clear.

In line 4, an inner class is being created, and the definition of the inner class ends on line 7.
Since inner class is a member of the enclosing class, you have to end the definition by a semicolon, as you do for any other class member.
 
reply
    Bookmark Topic Watch Topic
  • New Topic