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

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question ID :988380923984
What will the following code print when run?
public class Test
{
static String s = "";
public static void m0(int a, int b)
{
s +=a;
m2();
m1(b);
}
public static void m1(int i)
{
s += i;
}
public static void m2()
{
throw new NullPointerException("aa");
}
public static void m()
{
m0(1, 2);
m1(3);
}
public static void main(String args[])
{
try
{
m();
}
catch(Exception e){ }
System.out.println(s);
}
}
Answer: 1
can any one explain me with reasons
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sonir shah:
Question ID :988380923984
What will the following code print when run?
public class Test
{
static String s = "";
public static void m0(int a, int b)
{
s +=a;
m2();
m1(b);
}
public static void m1(int i)
{
s += i;
}
public static void m2()
{
throw new NullPointerException("aa");
}
public static void m()
{
m0(1, 2);
m1(3);
}
public static void main(String args[])
{
try
{
m();
}
catch(Exception e){ }
System.out.println(s);
}
}
Answer: 1
can any one explain me with reasons


[p]
Okay. What happens is m() is called in the main. m() calls m0. m0 calls m2(). m2() throws an exception. Since it is not in a try/catch block, control reverts back to m0. m0() is not in a try/catch block, so control reverts back m(). m() is not in a try/catch block, so control reverts back to the main(), which called m(). Since it is in a try/catch block, the exception is caught. After the catch, the println is executed. s was set to equal ++a, which is 1, so 1 is printed.
[p]
If you read the JLS, it has a very good explanation about how exceptions pass back to previous calls. Hope this helps.
 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok.
I understood the initial part.
But the last part is still not clear.
why have u taken m0() method for the value of 's'.i agree that the value of 's'in m0() is 1.
but if we take m1() method as the part of the answer then the value of 's' will be 3.
please explain me.
Sonir
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
m1 is not executed because the call to method m2 does not complete normally. An exception is thrown.
 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But how is m1() related to m2() method.
both are completely different methods.
my question still remains unanswered .i.e
why only have we considered the m0() method instead of m1() for applying the value of 's'??
sonir
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sonir,
Yes, they are 2 completely different methods. Try tracing through the execution again. This is what happens:
  1. main() function tries a call to m().
  2. m() calls m0().
  3. m0() appends '1' to the string s. Then it calls m2().
  4. Look very carefully at m2(). It throws an exception. The exception is not handled by m2(), so control is thrown back up to the calling function, m0().
  5. m0() also does not handle it, so before the call to m1() can be executed, control is thrown back up to m0()'s calling function, m().
  6. m() also does not handle the exception, so the main()function regains control.
  7. [list]main() does handle the exception, so the line is executed.
    [/list][p]
    The only time s was ever successfully altered was when the function m0 appended a value to it. The function m1() is never reached by the executing code. If you comment out the call to m2() in m0(), then m1() will be executed. So the reason that m1() is not considered here is because it cannot ever execute as the code is written.
    Hope this clarifies the issue for you.
    --kkoszegi

    Originally posted by sonir shah:
    But how is m1() related to m2() method.
    both are completely different methods.
    my question still remains unanswered .i.e
    why only have we considered the m0() method instead of m1() for applying the value of 's'??
    sonir


 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jose... is correct..
The execeptions when not handled properly makes an abrubt termination.. This is what happening.
s +=a; // (1)
m2();
After (1) exeception is thrown but never handled so jvm looks goes higher and higher to see who has the handler to fix this exeception and it ends up in main..
So the only computation that had happened in this code is " s +=a;" , So the answer is 1..
Sonir.. Does this help you?
Ragu
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic