• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Mock exam question

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are writing a set of classes related to cooking and have created your own exception hierarchy derived from java.lang.Exception
as follows

Exception
+----BadTasteException
+--------BitterException
+--------SourException

BadTasteException is defined as an abstract class.

You have a method eatMe that may throw a BitterException or a SourException.Which of the following declarations will be acceptable
to the compiler?(check all the correct answers)

a public void eatMe(ingredient[] list) throws BadTasteException
b public void eatMe(ingredient[] list) throws BitterException,SourException
c public void eatMe(ingredient[] list) may throw BadTasteException
d public void eatMe(ingredient[] list)

The given correct answers are a,b.

But when i execute all the options,a and d are executed without error.But i want to verify the result with someone.please clarify it for me.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shiva Mohan:
...The given correct answers are a,b.

But when i execute all the options,a and d are executed without error.But i want to verify the result with someone.please clarify it for me.


a and b are correct, but c and d are not.

Option d (with no declarations) would work if the exceptions are handled within the method. But in that case, it would not be accurate to say that the method may throw these exceptions. If you're testing this some other way, and 'd' is working for you, could you please post the test code?
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class BadTasteException extends Exception{}
class BitterException extends BadTasteException{}
class SourException extends BadTasteException{}
abstract class Priya{
abstract void eatMe()throws BitterException;
}

class Objective71 extends Priya{
void eatMe(){//is working
//void eatMe() throws BadTasteException is working
//void eatMe() throws BitterException,SourException{} is not working for me
}
public static void main(String[] args){

}
}

The Preceding one is my tested coding.Please marc help me further to get this.Where am i doing wrong?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From what was originally posted, the question says that BadTasteException should be an abstract class. But it does not ask you to put your method (with a declared exception) in an abstract class and try to override the method. This is complicating things.

To test this, write a method that actually throws a BitterException and a SourException. If the method doesn't have a real chance of throwing these, then the compiler won't make you declare anything, and you won't have a real test.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell us the exact Mock Exam from which you got this question.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.i got this mock question from Bill Brogden -exam cram java 2.

Thanks
Shiva.
 
We can walk to school together. And we can both read this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic