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 Is the expression correct?? 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 "Is the expression correct??" Watch "Is the expression correct??" New topic
Author

Is the expression correct??

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435

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
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
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 ]
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Some of the best ++ operations that we discussed in the past....atleast I could understand.
1. http://www.javaranch.com/ubb/Forum24/HTML/000775.html
2. http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=001715
regds.
- satya


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

(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
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is the expression correct??