Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what will be the ans

 
shirsa ray chaudhuri
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pulic class Integers{
public static void main(String[] args){
System.out.println(0*10+10+010);
}
}
what will be the ans, i think 20, is it right.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is rather easy to test by compiling and running it, no?
 
shirsa ray chaudhuri
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have no java compiler so please give me the answer...
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We don't do your homework for you here, but I will give you a hint: read this.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49788
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer will of course be 22.

I'll leave it to you to work out how to get it to print 22!
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got 18 ... leading zero interpreted as numeric literal ... 010 is eight in octal.
[ April 10, 2008: Message edited by: f. nikita thomas ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49788
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let him work out the answer for himself . . .

And it really did print out 22 when I tried it.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also get it as "18" only, which seems to be obvious here!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49788
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I got 22.

Work it out
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell, what are we missing out here? It looks like a puzzle wherein you are asked to just get a different answer by just changing one line or operator etc? -- so as to get 22!

here, it is very simple -- it has 3 operands [0*10+10+010] in which the the result of multiplication yields 0 and the rest two gives you 10 and 8 (010 in octal yielding 8 in decimal). The sum is 18!

How and why you get a different answer?
 
Henry Wong
author
Marshal
Pie
Posts: 21409
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How and why you get a different answer?


He didn't get a different answer. The answers are the same. Keep in mind that he didn't say what the exact output was, just what the result value was.

Now admittingly, Campbell, when something has to be explained, it isn't funny....

Henry
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's correct Henry. But Campbell's posts were not saying anything about the formatting of output leaving room to think on other sides/aspects of it.

As it is you run the program, it gives 18 only. It is what my concern here! That's what i asked what is being missed out, if at all?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BIG hint: what is 2*8 + 2*1?
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you run as it is with the default priority of operators, it is 18. But how it helps in the OP's question.?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49788
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did run it with default priority. Henry and Fred know what I did, but I shall keep quiet for a little.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49788
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, all right then. I printed it with the %o tag in printf.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell that is fine

But we were talking about running the program without modifying and of course as it was.

But isn't it cheating?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49788
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All right, it was cheating. Sorry.

But, has Shirsa Ray Chaudhuri worked out why it is 18 or 22 and not 20?

There were three terms to the arithmetic:
  • 0 * 10, which comes to 0.
  • 10, which is one more than nine and one less than eleven.
  • and 010 which is one more than seven and one less than nine.
  • 0 + 10 + 8 is 18.
  • Beginners are reminded that any integer literal beginning with 0 is in octal, so 010 is 10 in octal or 8 in decimal.
    Any integer literal beginning with 0x is in hexadecimal, so 0x10 is 16 in decimal.

    When I had the 18 decimal, the %o tag made it print in octal, so I got the printout of 22.
     
    shirsa ray chaudhuri
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    this all are wrong cause opptions are 1)28,2)30,3)34,4)36,5)101010
     
    Ulf Dittmer
    Rancher
    Posts: 42968
    73
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by shirsa ray chaudhuri:
    opptions are

    If this is a question from book or mock exam, then please QuoteYourSources.
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20608
    63
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The options are wrong, since the answer really is 18.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49788
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by shirsa ray chaudhuri:
    this all are wrong
    No, the results people have posted, with or without cheating, are all correct. You can get 12 as an output too, by using the %x tag to print in hexadecimal. If the answers are to be picked from a multiple choice list of 28 30 34 36 and 101010, you must have made a mistake when you copied the original question.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49788
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Have you mistaken 0x10 for 0 multiplied by 10 (0*10)? If you were supposed to write 0x10, that would give one of the answers you listed. And as you have been asked, please post where you got the question from.
     
    f. nikita thomas
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    good eye Campbell
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3381
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Campbell Ritchie:


    When I had the 18 decimal, the %o tag made it print in octal, so I got the printout of 22.


    Campbell, not intended to beat around the bush! My intention and puzzling all was about the "%o" tag which you used by yourself! and that caused the confusion!
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3381
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Campbell Ritchie:
    No, the results people have posted, with or without cheating, are all correct. You can get 12 as an output too, by using the %x tag to print in hexadecimal. If the answers are to be picked from a multiple choice list of 28 30 34 36 and 101010, you must have made a mistake when you copied the original question.


    That's true. As Ulf suggested, you should have better Quoted your source, which would have avoided the confusions as Campbell pointed out -- misinterpreting the 0X as * (multiplication).
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49788
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    . . . and if I'm right, I can still get the result to print as 22!

    Raghavan, sorry for confusing people, but I challenge you to work out how to get the result of 22 on screen.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3381
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell, that's perfectly agreed. If you see my very first post i told that it looked like a puzzle wherein you are expected to bring a change in the output by just altering a single value or operator etc., -- Here it was achieved by changing the output formatter!

    That's okay but people should get clarified rather than getting confused -- Right?
    [ April 11, 2008: Message edited by: Raghavan Muthu ]
     
    shirsa ray chaudhuri
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    the answer is 36
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49788
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by shirsa ray chaudhuri:
    the answer is 36
    Really? That doesn't look right to me. Please quote the question exactly as written, and tell us where you got it from.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic