wood burning stoves 2.0
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes John Hunt mock exam Q 14 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "John Hunt mock exam Q 14" Watch "John Hunt mock exam Q 14" New topic

John Hunt mock exam Q 14

Moya Green
Ranch Hand

Joined: Jan 24, 2002
Posts: 49
Q14 Which of the following do not lead to a runtime error?
A) "john" + "was" + "here"
B) "john" + 3
C) 3 + 5
D) 5 + 5.5
Select all correct answers.
I know the answer is ABCD.
My question is: Will B or D lead to a compile error since the first operand and the second operand are different type? Say:
System.out.println("john" + 3);
System.out.println(5 + 5.5);
What the printing results will be?
Many many thanks!
Padma Shyam

Joined: Feb 14, 2002
Posts: 7
In the statement System.out.println("John"+3)
3 is also converted into String and so the output is John3.
In the second one,numeric promotion takes place and so 5 is converted into double.The output is 10.5.
Jennifer Wallace
Ranch Hand

Joined: Nov 30, 2001
Posts: 102
All the above cases would work just fine!
> When the operator '+' is used with numeric operands, if both the operands are not of the same type then binary numeric promotion takes place.
So here, System.out.println(5 + 5.5);
Integer literal '5' is converted to floating point literal (5.0) and added with 5.5 giving you
10.5 as the result .
Please see rules for this conversion in the below specified link!
> '+' is also String Concatenation operator when even one of the operand is a string.
So, 'any datatype' + String = String
System.out.println("john" + 3);
Wrapper Classes are used to convert numeric datatypes to their corresponding Wrapper classes and then that ref. is converted to a String.
The exact rules are found here in JLS 15.18
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Try the following code to understand what is going on!
System.out.println(3+5+" = "+3+5);
If you guessed the output would be "8 = 8" then you're wrong
The output will be (drum roll please):
"8 = 35"
Why such an output? Statements are evaluated from left to right, actually the + operator is left associative. Here is the break down of the steps that take place:
step 1: 3+5 gets evaluated first, the two are both numbers so an arithmetic addition takes place
step 2: the result from step one gets added to the string " = ", the result is "8 = " since the + operator is overloaded to concat with strings
step 3: "8 = " gets added to interger value 3, since Strings overload the + operator to concat strings, the value 3 is promoted to a string object and added resulting in "8 = 3"
step 4: like string 3, with 5 getting added to a string

<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Moya Green
Ranch Hand

Joined: Jan 24, 2002
Posts: 49
Thank you, guys!
Your explanations are very helpful. I got it.
mahesh zarkar

Joined: Feb 14, 2002
Posts: 24
Where can i get the mock exams for Juhn Hunt.
Please advice.
Thank you.

In search of Excellence.
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
There you go mahesh

[Blog] [Blogroll] [My Reviews] My Linked In
mahesh zarkar

Joined: Feb 14, 2002
Posts: 24
Thank you Valentin,
I agree. Here's the link: http://aspose.com/file-tools
subject: John Hunt mock exam Q 14
It's not a secret anymore!