aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how this uotput comes? 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 "how this uotput comes?" Watch "how this uotput comes?" New topic
Author

how this uotput comes?

amrita sankar
Greenhorn

Joined: Jan 25, 2006
Posts: 26
public class Q13
{
static int call(int x)
{
try
{
System.out.println(x---x/0);
return x--;
}
catch(Exception e)
{
System.out.println(--x-x%0);
return x--;
}
finally
{
return x--;
}
}
public static void main(String[] args)
{
System.out.println(" value = "+ call(5));
}
}

For this question output is 3.How it comes? Please explain.
harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
after the call(5)

control goes to the method

as divison by zero ,it goes to the exception block there the value is 4,
and then as finally block always executes in the finally block value becomes 3
hope so i am right

good book for SCJP is by kathy sierra and bert bates,ita available inall the book stores.

hope this helps u
good luck


Harish<br />SCJP 1.4 (85%)
amrita sankar
Greenhorn

Joined: Jan 25, 2006
Posts: 26
Originally posted by harish shankarnarayan:
after the call(5)

control goes to the method

as divison by zero ,it goes to the exception block there the value is 4,
and then as finally block always executes in the finally block value becomes 3
hope so i am right

good book for SCJP is by kathy sierra and bert bates,ita available inall the book stores.

hope this helps u
good luck



Thank you.I understood.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

SCJP questions, like this one, go in the SCJP forum. I'll move it there for you.


[Jess in Action][AskingGoodQuestions]
vandu matcha
Ranch Hand

Joined: Dec 28, 2005
Posts: 57
In the statement...System.out.println(x---x/0);
how it is considered..it can be taken as
(x--) -(x/0) aswell as
(x)- (--x/0..)
so for the first one ..ans is 2
second one ans is 3...
i am a bit confused.....how this statement is to be considered...
ven kaar
Ranch Hand

Joined: Nov 01, 2005
Posts: 39
based on operator precedence,

(x--)-(x/0) is my guess

but still, both version give 3 as output


Parallax - Change in observational position that provides a new line of sight.
Tilo Hemp
Ranch Hand

Joined: Nov 21, 2005
Posts: 91
ven, you are right. a simple test reveals it. if anybody wants to cut and paste, for your convenience:

public class Minusminus {
static public void main(String...args){
int i = 3, j = 2;
System.out.println("the mysterious expression is: " + (i---j) +" i = "+i+", j = "+j);
}
}
[ January 25, 2006: Message edited by: Tilo Hemp ]
ven kaar
Ranch Hand

Joined: Nov 01, 2005
Posts: 39
based on operator precedence,

(x)-(--x/0) prefix goes before postfix

http://highered.mcgraw-hill.com/sites/0072488190/student_view0/operator_precedence_chart.html
vandu matcha
Ranch Hand

Joined: Dec 28, 2005
Posts: 57
thank u tilo for the example...and ven ..for the chart repersenting the operators and their precendence..thank a lot...going to write exam within 2 days..bit tensed.....thanks for the help
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
operators chart is good but, the chart is not showing any bitshift operators.
ven kaar
Ranch Hand

Joined: Nov 01, 2005
Posts: 39
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html
Simi gupta
Greenhorn

Joined: Oct 17, 2005
Posts: 7
Hi All,
Simi gupta
Greenhorn

Joined: Oct 17, 2005
Posts: 7
I'm sorry,
The last post was by mistake, i want to know what happens when an Exception occurs in catch block itself as in the above code it does but it doesn't print the exception stack trace but simply prints the output from main().
Regards,
Simi
Tilo Hemp
Ranch Hand

Joined: Nov 21, 2005
Posts: 91
ven, I think your first explanation was right... prefix has a higher priority than postfix, but the expression is evaluated from left to right.

here's another one:
int i = 3, j = 2;
System.out.println("the expression is: " + (i---++j) +" i = "+i+", j = "+j);

quite simple i guess.

but I wonder where the precedence "prefix before postfix" ever is of importance? an expression like ++i-- is not accepted by the compiler.
ven kaar
Ranch Hand

Joined: Nov 01, 2005
Posts: 39
Yes Tilo u r rite, the first answer is correct
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how this uotput comes?