• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Queries from bills mock exam

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Assume the rst of the class defines doStuff , etc and that the class
compiles without error.Also assume that a java application creates a
whiffler object and calls the WHiffler method,that no other direct calls to Whiffler methods are made and that the Thread in this Object is the only
one application creates.

Here the answer given is that statement marked 1 is not reached which is
fine.Also 2nd answer is the doStuff method is never executed.I didnt
understand why? I felt it would call doStuff repeatedly since its inside an never ending while loop

Here answer given is method calling X should catch IndexOutOf Bounds
Exception which is fine

Also second answer is that the declaration for method X does not have to
mention Exceptions?Why . I thought it should declare it using throws clause so that the calling method would be aware.Also it is given that X method does not have try catch blocks.Then dont we have to declare?


Thanks !!
[ August 17, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Smitha B", I seem to recall that I asked you to change your display name to conform to our standard format <first name><space><family name>. "B" is not your family name. For example: Barry is my first name, Gaunt is my family name. Please change your display name, or I will soon request suspension of your account.
Thanks,
-Barry
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Swpna

your question on exceptions is not clear for me..can you please give the exact answer choices you have for them.Actually i've seen this question in one of the mockexams.. i dont remmber where?/??

As I undersatnd Runtime Exceptions dont have to be declared in the throws clause.

If the method X does not have try catch blocks in it.... it is okay.
But the calling method should declare try catch blocks...

In answer choices we have to be very careful about few words like "must","always","should " etc..
 
naraharirao mocherla
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm sorry.just now i've seen that it is from Bill's mock exam
 
Smitha Ballikar
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry,


I wanted to tell you that you have not told me anytime to change my display name.Before this my display name was Smitha Amey Ballikar.But I changed it thinking a full name is not necessary.Infact I did notice in the forums that you have told it to Swapna..

My last name is Ballikar, and its not taking that since long back I created a id with same name and somehow I was not able to login in that account back.
But now I can change it to Smitha A Ballikar.I am sorry and please dont suspend my account since I am being informed for the first time.

Cheers
Smitha
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2.Integer nA = new Integer(4096);
3.Object nB = new Long(4096);
4.if(nA.equals(nB))System.out.println("equals");



At compile nB is of Object type and not Long type and Integer's equals method overrides Object's equal method whose argument is of type Object.
With this said, line#4 shouldn't result in compile error.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whats interesting is that even having nB as Long does not give a equal result. You would think that the equals would be true in this case:

Integer nA = new Integer(4096);
Long nB = new Long(4096);
if(nA.equals(nB))
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1.public void test(){ 2.Integer nA = new Integer(4096);3.Object nB = new Long(4096);4.if(nA.equals(nB))System.out.println("equals");5.if(nA.intValue()==((Long)nB).longValue()){6. System.out.println("EQ");7. }8.}




Compile time error will never occur if you compare wrapper objects of different types.When wrapper objects are of diff types(say Long & Integer) are compared equals method will simply return false no matter what value it contains.But when wrapper objects are of same type are compared equals method will check the value in both & if same it returns true else it returns false.



public class Whiffler extends Object implemente Runnable{Thread myT;public void start(){myT = new Thread(this);}public void run(){while(true){ doStuff(); } SYstem.out.println("Exiting Run");//1 } // rest of code.



In the above code unless u call the run method explicitly it wont be executed & in turn dostuff() method would never be called.If u call the run method explicitly as u said dostuff() would be called infinitely.If run method need to be executed by another thread you should call start() method of thread class.i.e., u should execute myT.start().

Suppose exception Hierarchy is Exception | RuntimeException | IndexOutofBoundsException | ArrayIndexOutOfBoundsException StringIndexOutOfBoundsException Supose there is a method X which could throw both ArrayIndexOutOfBoundsException and tringIndexOutOfBoundsException.



Answer is pretty straightforward if u closely observe the hierarchy diagram.Just bcoz those exceptions are sub types of RuntimeException it need not to be declared in throws clause or to be caught in a catch block.
Try to remember one simple rule which says "No handle or declare rule applies to RuntimeException or its sub types".

Regards,
Priya.
 
Smitha Ballikar
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Priya,

Thanks for your wonderful explanation to all my questions..
You explain it pretty well...

Cheers
Smitha
 
reply
    Bookmark Topic Watch Topic
  • New Topic