• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JQ+ Question ID :1003242858408

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question from JQ+ practice test.
Consider the following code:
class A
{
byte getStatusCode(Object obj) throws NullPointerException
{
if(obj != null ) return 1;
else return 0;
}
}
class B extends A
{
//override getStatusCode method.
}

Which of the following statements are valid?

Overriding method cannot throw IOException
Overriding method can throw Throwable
class A will not compile
Overriding method can throw any exception
Overriding method may choose not to throw any exception
My choice was the last one. However, JQ+ say 'class A will not compile'.
Because the return type of the method is byte, but it is returning int.
The above code compiles fine on my machine, unless you change the return
literal to < -128 or > 127 in which case you get 'possible loss of precision error'
Does JQ plus speak with fork tongue or am i missing something?

many thanks
Gerry
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overriding method cannot throw IOException
True
Overriding method can throw Throwable
False
class A will not compile
False - It will compile
Overriding method can throw any exception
False
Overriding method may choose not to throw any exception
True
My choice was the last one. However, JQ+ say 'class A will not compile'.
Because the return type of the method is byte, but it is returning int.
The above code compiles fine on my machine, unless you change the return
literal to < -128 or > 127 in which case you get 'possible loss of precision error'
Since your using interger constant, so it will compile. If you use integer varible, it will not compile.

Originally posted by Gerry R Binns:
Question from JQ+ practice test.
Consider the following code:
class A
{
byte getStatusCode(Object obj) throws NullPointerException
{
if(obj != null ) return 1;
else return 0;
}
}
class B extends A
{
//override getStatusCode method.
}

Which of the following statements are valid?

Overriding method cannot throw IOException
Overriding method can throw Throwable
class A will not compile
Overriding method can throw any exception
Overriding method may choose not to throw any exception
My choice was the last one. However, JQ+ say 'class A will not compile'.
Because the return type of the method is byte, but it is returning int.
The above code compiles fine on my machine, unless you change the return
literal to < -128 or > 127 in which case you get 'possible loss of precision error'
Does JQ plus speak with fork tongue or am i missing something?

many thanks
Gerry

 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in my opinion the right answer is the first answer AND the last answer.
i agree with u that it compiles but the first answer is correct also.
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i agree with your choices (sun ram).
first and last are true ie
Overriding method cannot throw IOException
True
and
Overriding method may choose not to throw any exception
True

Originally posted by sun ram:
Overriding method cannot throw IOException
True
Overriding method can throw Throwable
False
class A will not compile
False - It will compile
Overriding method can throw any exception
False
Overriding method may choose not to throw any exception
True

My choice was the last one. However, JQ+ say 'class A will not compile'.
Because the return type of the method is byte, but it is returning int.
The above code compiles fine on my machine, unless you change the return
literal to < -128 or > 127 in which case you get 'possible loss of precision error'
Since your using interger constant, so it will compile. If you use integer varible, it will not compile.

 
Gerry R Binns
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I missed the first one.
thanks
Gerry
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I took look at the question too... but the way they (JQ+) explain is
"If you answered this question wrong, that means you did not read the question properly. It is very important that you read the question properly. It may seem to have some problem at the first look but may have an entirely different issue." // end copy paste
Perhaps they can "word" it better?
[ February 08, 2002: Message edited by: Bob Tai ]
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1)Overriding method cannot throw IOException
2)Overriding method can throw Throwable
3)class A will not compile
4)Overriding method can throw any exception
5)Overriding method may choose not to throw any exception


Why an overriding method cannot throw IOException?
If I override it, and do an I/0, either read from a file or read from console input, then I need to catch an IOException or throw it to the caller. Am I right?
Thanks
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kawaii desu:

If I override it, and do an I/0, either read from a file or read from console input, then I need to catch an IOException or throw it to the caller.


true
, i.e. ... OR ...
In this case only catching it is possible (unless you change the superclass, of course).
When overriding a method you're not allowed to
throw a more general exception, i.e. you can't
throw an exception, that's higher in the exception hierarchie or in a different branch.
You may throw a more specific one, or none at all.
 
reply
    Bookmark Topic Watch Topic
  • New Topic