• 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:

question #47 java certification - AndreKaan

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following incomplete method.
1. public void method(){
2.
3. if (someTestFails()){
4.
5. }
6.
7.}


You want to make this method throw an IOException if, and only if, the method someTestFails() returns a value of true. Which changes achieve this?

A. Add at line 2: IOException e;
B. Add at line 4: throw e;
C. Add at line 4: throw new IOException();
D. Add at line 6: throw new IOException();
E. Modify the method declaration to indicate that an object of [type] Exception might be thrown.

In the above question the answer should be c,d because we want to throw an IOException only if someTestFails() returns true and that means it has to pass if-statement . but answer given is d,e
can any one comment on it?
thankyou
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks to me like the correct ansewr would be C and E. I can't see any reason for D - it would obviously send an exception all the time, not just when someTestFails() is true.
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi satyavani,
I believe the answer should be c and e.
1. public void method(){
2.
3. if (someTestFails()){
4.
5. }
6.
7.}

C. Add at line 4: throw new IOException();

It says that you throw an exception if and only if someTestFails() is true. So the throw statement should be inserted at line 4.
E. Modify the method declaration to indicate that an object of [type] Exception might be thrown.
As a general rule, Java requires that any method that might throw an exception must declare the possibility using throws statement.

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks jim and suma. and one more question:
can an inner class be an abstract class?
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no problem to use inner class as an abstrat class but the rules applicable to the abstract class applies the same to it. Try it now!

Originally posted by satyavani:
Thanks jim and suma. and one more question:
can an inner class be an abstract class?



 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-
What i feel is about this question is, we have to take the literal meaning of sometestfails() i.e. it returns false.
When it fails [i.e. some test fail is true] then if block
will not be executed and it goes to line 6. But, again if, if{}
is executed again line 6 akso will be executed.
So, if throw statement is placed in line 6 exception will be thrown no matter if block executes or not.
So, throw has to be placed in line 4.
I guess I did confuse a lot.
apologies.

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Maha Anna
Thank you For sending above piece of code , which gives clear understanding of strings & POOL of Strings. I would be glad if you could clear my doubt about POOL of strings , to the given code I added to 2 more tests which I have listed below
if(deepaLowercaseStr.intern() == "deepa ".trim())System.out.println("Pass15");
else System.out.println("Pass15 failed"); //result is: Pass15 Failed
if(deepaLowercaseStr.intern() == "deepa".trim()) System.out.println("Pass16");
else System.out.println("Pass16 failed"); // result is: Pass16

by this can we say that Strings are added to the string pool only if created by " " this way ( i.e.using Double Quotes ) or by calling intern method on string Objects. All other strings objects returned from methods or String Objects constructed using new operator are not added to the POOL
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sammet,
Let us go to the correct place and discuss. See you .
regds
maha anna
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic