• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Is the expression correct??

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have:

after ++k, k is 2
then 2 + 2
then add one to k
then you have 2 + 2 + 3, for i
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

(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
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic