aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  Jq + Question ID :988380923984 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " Jq + Question ID :988380923984" Watch " Jq + Question ID :988380923984" New topic
Author

Jq + Question ID :988380923984

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
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
Jim Bedenbaugh
Ranch Hand

Joined: Nov 09, 2001
Posts: 171
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.


Regards,
Jim
SCJP, SCJD, SCWCD, SCEA Part I
sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
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
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
m1 is not executed because the call to method m2 does not complete normally. An exception is thrown.


SCJP2. Please Indent your code using UBB Code
sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
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
Kelley Koszegi
Greenhorn

Joined: Aug 20, 2001
Posts: 27
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


Kelley Koszegi<br />Sun Certified Java Programmer
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jq + Question ID :988380923984