File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Precedence query 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 "Precedence query" Watch "Precedence query" New topic
Author

Precedence query

Kshitija Mitter
Greenhorn

Joined: Mar 03, 2008
Posts: 11
Hi,
The following question is from the java inquisition quiz section, I am quite confused about the output. Could anyone clarify the same



[ June 03, 2008: Message edited by: Kshitija Mitter ]
Swapnil Sonawane
Ranch Hand

Joined: Jan 02, 2008
Posts: 190
String ...arg seems compiler error to me. Looks too advanced


Swapnil S. Sonawane<br /> <br />B.Tech (Expected May 2009)<br />Computer Science, NIT Durgapur, India<br />SCJA 1.0
Swapnil Sonawane
Ranch Hand

Joined: Jan 02, 2008
Posts: 190
Surprisingly, compiles on my Java SDK 5.0
Swapnil Sonawane
Ranch Hand

Joined: Jan 02, 2008
Posts: 190
The three dots specify that argument may be passed as an array or as a sequence of arguments. Variable number of arguments.
Kshitija Mitter
Greenhorn

Joined: Mar 03, 2008
Posts: 11
Hiya,

The code compiles well. Thats not the problem. I had evaluated th value to be 9, but the correct answer is 7....

Swapnil Sonawane
Ranch Hand

Joined: Jan 02, 2008
Posts: 190
Expression : 5+i++%4*i;
7 is the correct answer to me.
It is ( 5 + (i++ %4)*i)
So i%4=1, then 1*i = 2 (As i=2 now) and 5+2=7.
Please correct me if I am wrong.
Kshitija Mitter
Greenhorn

Joined: Mar 03, 2008
Posts: 11
Thanks!!
Makadia Jaydip
Greenhorn

Joined: Jun 03, 2008
Posts: 9
Sorry Swapnil, In Whole Expression the value of i=1. The Value of i is increased after the complition of Expression, because it is a post increament. then how it give 7 output.. i m not under-stand your logic. please let me know how this expression give's 7 output.

[ June 04, 2008: Message edited by: Makadia Jaydip ]
[ June 04, 2008: Message edited by: Makadia Jaydip ]
Rinku Mishra
Greenhorn

Joined: Feb 27, 2008
Posts: 8
What is put on screen?static public void main(String...arg) { int i=1; int result = 5+i++%4*i; System.out.println(result); }


hi Kshitija,

I think 7 is the right answer,because
I think this is what happens
5 + 1%4 * 2 [here 5 + (i(which is 1 first because of postincremnet)%4 gives 1)*i(which is 2 right after the increment)]
so here it goes like this
5 + 1*2
5+2
7 answer
Swapnil Sonawane
Ranch Hand

Joined: Jan 02, 2008
Posts: 190
I guess you get it too Jaydip.
Seema Gaurav
Ranch Hand

Joined: Apr 29, 2008
Posts: 47
Hi Jaydip,
7 is the correct answer. In this case, the operator pecedence will be as follows:
++ (Postfix Increment)
% * (Whichever comes first from Left to Right)
+

int i=1;
Exprn: 5 + i++ % 4 * i
Step 1: 5 + 1 % 4 *i //Now i=2 because of the postfix ++; i%4=1
Step 2: 5+1*2//1*2=3
Step 3: 5+2 //=7
HTH,
Seema
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
this kind of tricky precedence question used to be on the exam, but none of the current scjp exams have questions like this. the new exams are testing for other knowledge - memorizing the details of operator precedence is no longer necessary. philosophically, the idea is that good programming practices should encourage using parentheses

hth,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Pravin Jadhav
Greenhorn

Joined: May 27, 2008
Posts: 3
Thank You..Bert. I am Really relieved by your Answer..This kind of tricky questions always bother me alot, and thet are ususally asked for C Lang. As Java is High level.Its good to listen tht no need to scratch My brain just for this thing as i can always use it for high level purposes.


Software Engg<br />SCJP 5.0 - 97%<br />preparnig for SCWCD
Makadia Jaydip
Greenhorn

Joined: Jun 03, 2008
Posts: 9
Thank's Seema Gaurav
Matt Russell
Ranch Hand

Joined: Aug 15, 2006
Posts: 165
As per Bert's feedback, we dropped this question from Mark's quiz on the latest version of Inquisition.


Matt
Inquisition: open-source mock exam simulator for SCJP and SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Precedence query