Step Right Up, Test your Skill

R

Roedy Green

What does the following program snippet print?

System.out.println ( 'a' + '\r' );
System.out.println ( 'a' + "\r" );
System.out.println ( "a" + '\r' );
System.out.println ( "a" + "\r" );

for bonus points, what will it print on the Eclipse console?

Be honest. post your answer before you test.
 
L

Luc The Perverse

Roedy Green said:
What does the following program snippet print?

System.out.println ( 'a' + '\r' );
System.out.println ( 'a' + "\r" );
System.out.println ( "a" + '\r' );
System.out.println ( "a" + "\r" );

for bonus points, what will it print on the Eclipse console?

Be honest. post your answer before you test.

Is this the test you give to "astute" Java programmers?
 
S

slippymississippi

I'd guess that adding 'a' + '\r' promotes the result to an int.

Adding 'a' + String probably results in a String, as does a String +
'\r' and String + String.

So whatever int value 'a' + '\r' becomes, plus:
a

a

a
 
M

Max

I don't really understand how knowing this code-snippet corresponds to
skill. It seems more like... trivia?
 
S

slippymississippi

Actually, some of the crap that they ask you on the Sun Certification
exam is not much different than this.

Finding out the hard way that an arithmetic operator will cause a
character to be promoted to (I think) an integer has been branded onto
my frontal lobe for life. Who thought up *that* nonsense!?!? :)
 
J

Jeffrey Schwab

Roedy said:
What does the following program snippet print?

System.out.println ( 'a' + '\r' );
System.out.println ( 'a' + "\r" );
System.out.println ( "a" + '\r' );
System.out.println ( "a" + "\r" );

for bonus points, what will it print on the Eclipse console?

Be honest. post your answer before you test.


Well, my theory *was* a compile-time error on 'a' + "\r". Live and learn.
 
J

Jim Korman

What does the following program snippet print?

System.out.println ( 'a' + '\r' );
System.out.println ( 'a' + "\r" );
System.out.println ( "a" + '\r' );
System.out.println ( "a" + "\r" );

for bonus points, what will it print on the Eclipse console?

Be honest. post your answer before you test.

Ok,

110
a
a
a

from my Windows console I expected......

Where is Eclipse putting in the extra linefeeds???????

Which made the following test not work at all

System.out.println ( 'a' + "\r" + "B");

Jim
 
R

Roedy Green

I don't really understand how knowing this code-snippet corresponds to
skill. It seems more like... trivia?

It is a gotcha, a flat in the design of the language where you get
astonishing results and even experienced people can trip. It helps to
be made aware of them from time to time. I have collected some at
http://mindprod.com/jgloss/gotchas.html
 
N

Noodles Jefferson

Roedy Green said:
What does the following program snippet print?

System.out.println ( 'a' + '\r' );
System.out.println ( 'a' + "\r" );
System.out.println ( "a" + '\r' );
System.out.println ( "a" + "\r" );

a

\r is a carriage return and with no line feed it just overwrites what
was there before.
for bonus points, what will it print on the Eclipse console?

No idea.
Be honest. post your answer before you test.

--
Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "Icicle" (Tour Rehearsal) -- Tori Amos

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.
 
R

Roedy Green

\r is a carriage return and with no line feed it just overwrites what
was there before.
on windows yes. But on the eclipse console it is a linefeed, as on
the mac.
 
D

Danno

My guess (without running it or looking at answers, or looking up the
unicode/ascii values)
is:

1. an integer with the unicode value of a + unicode value of a carriage
return (probably around the early 100s) with one return on any OS.

2. an integer with the value of a (probably late 90s) with two
returns(on a mac & linux) one return (on windows)

3. the letter a with two returns (on a mac & linux) one return (on
windows)

4. the letter a with two returns (on a mac & linux) one return (on
windows)

To hell with the bonus,. Eclipse is 4 chumps!
;)
 
D

Danno

Damn, missed the second one. I thought that since from left to right
the String wasn't established that it would still consider it a number.


nice Roedy.
 
D

Dag Sunde

Roedy Green said:
What does the following program snippet print?

System.out.println ( 'a' + '\r' );
System.out.println ( 'a' + "\r" );
System.out.println ( "a" + '\r' );
System.out.println ( "a" + "\r" );

for bonus points, what will it print on the Eclipse console?

Be honest. post your answer before you test.

I was working on the excact same solution you got from Gordon
Beaton, when he beat me to it... :)

In Eclipse, the code above will print:
a#
a#
a#
a#
(Where # = a square (unprintable char))

but it works ok when run in a console window under windows...
 
D

Dag Sunde

Dag Sunde said:
I was working on the excact same solution you got from Gordon
Beaton, when he beat me to it... :)

In Eclipse, the code above will print:
a#
a#
a#
a#
(Where # = a square (unprintable char))

but it works ok when run in a console window under windows...

I'll be damned...

(embarrased...)
 
P

Peter Davies

What does the following program snippet print?

System.out.println ( 'a' + '\r' );
System.out.println ( 'a' + "\r" );
System.out.println ( "a" + '\r' );
System.out.println ( "a" + "\r" );

for bonus points, what will it print on the Eclipse console?

We don't do people's homework for them here.
 
R

Roedy Green

Damn, missed the second one. I thought that since from left to right
the String wasn't established that it would still consider it a number.


nice Roedy.

One of the annoying things about this is the compiler does not
complain. You might do this in obscure error messages that don't get
tested, so it can hide in there for years to baffle your customers.
 
L

Lion-O

What does the following program snippet print?
System.out.println ( 'a' + '\r' );
System.out.println ( 'a' + "\r" );
System.out.println ( "a" + '\r' );
System.out.println ( "a" + "\r" );

I'm rather new to Java myself so I welcome these brain teasers. Having made the
mistake a few times to use 'test' instead of "test" when I needed a String its
my idea that it will do something funny with the '' encapseling.


cr/lf = 0d0a, but I'm not too sure what the ascii value of 'a' is. So my idea
is that it won't pick up the 'a' and '\r' as being Strings (perhaps a char) and
as such add them all up. No idea what the end result may be, probably an ASCII
value (if my theory is correct then it can't turn into high ascii) so I think
it'll start to print a letter in the range of ASCII value 70 - 100 (decimal).

Next the "" does denote a String but iirc \r doesn't resemble a cr/lf, thats
what \n does in a String. My guess is that it will simply print a character
here as well without anything special about it.

Since the 3rd line is basicly the same as the 2nd the same theory applies.

Last you have 2 Strings but since I'm positive \n to be cr/lr I think it'll
just print the 'a' as well.


I can't go for the bonus I guess since I'm using Netbeans. I haven't done much
with something specific like this on stdout so I'll have to guess as well; I
hope it behaves in the same was as the program does on the console.

Right, and now to test this later on :cool:

Thanks for the teaser, its a nice one IMO.
 
L

Lion-O

No idea what the end result may be, probably an ASCII value (if my theory is
correct then it can't turn into high ascii) so I think it'll start to print a
letter in the range of ASCII value 70 - 100 (decimal).

Well, not a character but a specific ASCII value (110) was printed. I guess I
have still some things to learn on this issue, never messed with char's so far.
My guess is that it will simply print a character here as well without
anything special about it.

And I have to admit being pleased that I got this part correct. And the theory
about this applying to the next 3 lines was correct as well :)
I haven't done much with something specific like this on stdout so I'll have
to guess as well; I hope it behaves in the same was as the program does on
the console.

I'm using Linux myself (as workstation) but one of the first things I did was
ditch the JVM which was shipped with it and replaced it with Sun's native JDK.
In my experience the 'open source variant' isn't very usefull and my personal
opinion on it is even worse.

Alas, on my box the output which was produced matches that of the rest. What I
do find interesting is that according to some posters Eclipse produced a rather
funny output, but Netbeans (on Linux anyway, I think I'll have a look see on
Windows later this evening) produced the exact same output in the output window
as the native jvm did on the console. I know I'm biased being a Netbeans user
myself but thats an extra point for Netbeans IMHO ;-)
 
O

Oliver Wong

Couldn't see the original post on my newsgroup server, so I found it on
Google Groups. Sorry if this headers of this message implies that I'm
replying to someone other than the OP.

</quote>
What does the following program snippet print?

System.out.println ( 'a' + '\r' );
System.out.println ( 'a' + "\r" );
System.out.println ( "a" + '\r' );
System.out.println ( "a" + "\r" );

for bonus points, what will it print on the Eclipse console?

Be honest. post your answer before you test.
</quote>

I don't know what the ascii value of a and \r are, so I'll just pretend
they are 42 and 10. Adjust the answers I post accordingly for the real ASCII
values of those characters.

'a' + '\r' should be seen as a char plus a char, so the two operands
should get promotoed to integers, and you'd get "52" (or 42 + 10) on the
first line.

'a' + "\r" should do string conversion, and I think (but am not sure)
that 'a' would actually get converted to a string representation of its
ASCII value, rather than to the string "a", so I'd say "42" would be printed
on the next line, followed by an extra blank line due to the "\r".

Similarly for "a" + '\r', I expect to see "a10" on the next line.

For "a" + "\r", this should just be string concatenation, so "a" with a
extra newline.

So my final answer is:

<quote>52
42

a10
a

</quote>

Of course, depending on the console, maybe sending \r alone might mess
things up if it's not considered a line terminator, and you get question
marks or other strange characters. I think Eclipse will handle newlines in
an "intelligent" manner, and so would produce the output I gave above.

<tries it out>

Damn. I got 2 out of 4.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top