• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Precedence query

 
Kshitija Mitter
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String ...arg seems compiler error to me. Looks too advanced
 
Swapnil Sonawane
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surprisingly, compiles on my Java SDK 5.0
 
Swapnil Sonawane
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!!
 
Makadia Jaydip
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you get it too Jaydip.
 
Seema Gaurav
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Pravin Jadhav
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Makadia Jaydip
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank's Seema Gaurav
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per Bert's feedback, we dropped this question from Mark's quiz on the latest version of Inquisition.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic