This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes what will be the ans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what will be the ans" Watch "what will be the ans" New topic
Author

what will be the ans

shirsa ray chaudhuri
Greenhorn

Joined: Apr 08, 2008
Posts: 10
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
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
This is rather easy to test by compiling and running it, no?


Ping & DNS - updated with new look and Ping home screen widget
shirsa ray chaudhuri
Greenhorn

Joined: Apr 08, 2008
Posts: 10
i have no java compiler so please give me the answer...
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
We don't do your homework for you here, but I will give you a hint: read this.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36578
    
  16
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

Joined: Mar 02, 2008
Posts: 87
i got 18 ... leading zero interpreted as numeric literal ... 010 is eight in octal.
[ April 10, 2008: Message edited by: f. nikita thomas ]

Imagination is more important than knowledge "Albert Einstein"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36578
    
  16
Let him work out the answer for himself . . .

And it really did print out 22 when I tried it.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

I also get it as "18" only, which seems to be obvious here!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36578
    
  16
And I got 22.

Work it out
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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
Sheriff

Joined: Sep 28, 2004
Posts: 18136
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Oct 02, 2003
Posts: 10925
    
  12

BIG hint: what is 2*8 + 2*1?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Oct 13, 2005
Posts: 36578
    
  16
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

Joined: Oct 13, 2005
Posts: 36578
    
  16
Oh, all right then. I printed it with the %o tag in printf.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Oct 13, 2005
Posts: 36578
    
  16
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

    Joined: Apr 08, 2008
    Posts: 10
    this all are wrong cause opptions are 1)28,2)30,3)34,4)36,5)101010
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 39576
        
      27
    Originally posted by shirsa ray chaudhuri:
    opptions are

    If this is a question from book or mock exam, then please QuoteYourSources.
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19556
        
      16

    The options are wrong, since the answer really is 18.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36578
        
      16
    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

    Joined: Oct 13, 2005
    Posts: 36578
        
      16
    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

    Joined: Mar 02, 2008
    Posts: 87
    good eye Campbell
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    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

    Joined: Apr 20, 2006
    Posts: 3344

    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

    Joined: Oct 13, 2005
    Posts: 36578
        
      16
    . . . 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

    Joined: Apr 20, 2006
    Posts: 3344

    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

    Joined: Apr 08, 2008
    Posts: 10
    the answer is 36
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36578
        
      16
    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.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: what will be the ans
     
    Similar Threads
    What is the value of 8|9&10^11? (scjp07-Q100)
    Numeric and Integer value? Same?
    CNN Money: Signs you have a great job ... or not
    WA #1.....word association
    lateral thinking