• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Q. from JQ+

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, this is a q. from JQ+.
What statements regarding the following code snippet are ture?
int a = 4;
int b = 6;
System.out.println( a--b ); // ie. a (no space) minus (no space) minus (no space) b

options are-
a. It will print 10
b. It won't compile
c. It will compile if a--b is replaced by a- -b
d. It will compile if a--b is replaced by a---b
e. It will compile if a--b is replaced by a+-b
ANS. are b,c,d,e.
They have given explanation,but I want more explanation for this
--Thanks.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
b. a--b is not a valid statement. The compiler can't tell if the double -- is a decrement or a minus sign or a negative sign indicator.
c. a- -b is (a minus negative b)
4 - (-6) = 4 +_ 6 = 10
d. a---b is (a with post decrement minus b)
4 - 6 = -2
a = 3 because of the post decrement
e. a+-b is (a plus negative b)
4 + (-6) = -2
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ans. is b. Check yourself by compiling it.
Jyotsna
 
Jyotsna Umesh
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Thomas Paul:
b. a--b is not a valid statement. The compiler can't tell if the double -- is a decrement or a minus sign or a negative sign indicator.
c. a- -b is (a minus negative b)
4 - (-6) = 4 +_ 6 = 10
d. a---b is (a with post decrement minus b)
4 - 6 = -2
a = 3 because of the post decrement
e. a+-b is (a plus negative b)
4 + (-6) = -2


Thomas, how did you get the code compiled with making suggested changes? I somehow failed to compile with Visual Cafe.
Jyotsna
 
A berm makes a great wind break. And we all like to break wind once in a while. Like this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic