• 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

exception question from kb

 
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 Plane
{

static String s='-';
public static void main(String []s)
{
new Plane().s1();
System.out.println(s);
}

void s1()
{
try { s2();}
catch(Exception e) { s+="c" ;}
}

void s2()throws Exception
{
s3(); s+="2";
s3(); s+="2b";
}

void s3()throws Exception
{
throw new Exception();
}
}

ans: -c

please expalin me this answer.

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, What is the question and what are the given answer options?? Your code wont even compile. You are assigning char to a String. And put the code in code tags and take some time to post the question and answer choices. And quit doing that, if it was your head it would look like a squashed potatoe by now with all the banging you been doing LOL
[ May 10, 2008: Message edited by: sridhar row ]
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Modified code:

class Plane
{

static String s="-";
public static void main(String []s)
{
new Plane().s1();
System.out.println(s);
}

void s1()
{
try { s2();}
catch(Exception e) { s+="c" ;}
}

void s2()throws Exception
{
s3(); s+="2";
s3(); s+="2b";
}

void s3()throws Exception
{
throw new Exception();
}
}

option are :
1.-
2. -c
3.-c2
4.-c22b
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The result is "-c", clearly. The flow of your application is as follows:

in main: create a new Plane() object (Plane has a static string var initialized to "-").

call method s1().

method s1 calls s2 from within a try/catch block

s2 calls s3

s3 throws an exception

now the methods start popping off the stack.

Method s2 bubbles up the exception (which means it STOPS execution, does NOT continue on to s+="2")

Method s1 catches the exception and modifies variable s to "-c"

s1 returns control to main,

Main prints s to screen. "-c"


If you debug your code in eclipse (for example) you can see how the calls from method to method work and you would have a very good understanding of why this is the result.
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Main prints s to screen. "-c"



Main does not print "-c". The main method local parameter s will print some garbage(classname@hashcode value).
 
Noam Wolf
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, sridhar is correct if you use the exact implementation of the main method as suggested by Dinesh. BUT if your main method uses the name "args" for the arguments passed to main then you won't have a local reference that is actually an array and when you call toString() on it will print out, as sridhar mentioned, the classname@hashcode value.

Another option is to change

to


but I think this is besides the point. The confusion was something more basic which was what I was trying to point out, there is no need to be so negative. If you have nothing to add don't. If you want to point out a mistake be more elaborate so that people can learn from this thread.
 
reply
    Bookmark Topic Watch Topic
  • New Topic