Options : 1)Compilation error at: "++k+k++ + + k" expression] 2)Compile and will print 7 and 3 3)Compile and will print 5 and 2 4)Compile and will print 9 and 3 5)Compile and will print 5 and 3 The answer is :2) It will compile and will print the value 7 and 3 when run. I feel the asnwer is 3). because value assigned to i will be 5 in the end and the value assigned to k will 2(twice ++k) please explain. Sonir
Mike Beaty
Greenhorn
Joined: Dec 19, 2001
Posts: 20
posted
0
You have:
after ++k, k is 2 then 2 + 2 then add one to k then you have 2 + 2 + 3, for i
Mike Beaty<br />Sun Certified Programmer for the Java 2 Platform
Arsho, Ayan
Ranch Hand
Joined: Nov 14, 2001
Posts: 60
posted
0
Hi Sonir ... I hope this helps visualize the expression as ++k + k++ + +k (2)1 + 2(3) + +3 = 7 i.e i Now add only the right side of each element. ie 2+2+3 the value of k will be 3 Later [ January 12, 2002: Message edited by: Arsho, Ayan ]
(2)1 + 2(3) + +3 = 7 i.e i Now add only the right side of each element. ie 2+2+3
That would be : (2)2+ 2(3) + +3 = 7 i.e i Now add only the right side of each element. ie 2+2+3 If I am NOT mistakes. - satya
Ragu Sivaraman
Ranch Hand
Joined: Jul 20, 2001
Posts: 464
posted
0
Originally posted by sonir shah:
Options : 1)Compilation error at: "++k+k++ + + k" expression] 2)Compile and will print 7 and 3 3)Compile and will print 5 and 2 4)Compile and will print 9 and 3 5)Compile and will print 5 and 3 The answer is :2) It will compile and will print the value 7 and 3 when run. I feel the asnwer is 3). because value assigned to i will be 5 in the end and the value assigned to k will 2(twice ++k) please explain. Sonir
Be careful with this post/pre incrementors people love asking questions on this . donno why int i = ++k + k++ + + k ; ++k, increments itself and then gives it to i so its equal to 2 k++ , gives its value 2 to the 'i' to participate in the addition ( ++k + k++) and then increments itself.. + +k - this incremented k is again added , so the value of i is 7 and k is 3 hope you got it Ragu