This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes dan exam dought 5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "dan exam dought 5" Watch "dan exam dought 5" New topic
Author

dan exam dought 5

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
hi guys
in one question of DAN ...

class Sienna {
static double a; static float b; static int c; static char d;
public static void main(String[] args) {
a = b = c = d = 'a';
System.out.println(a+b+c+d == 4 * 'a');
}}

What is the result of attempting to compile and run the program?

a. Prints: true
b. Prints: false
c. Compile-time error
d. Run-time error
e. None of the above


its answer is "a"

with explanations
The literal, 'a', is promoted to type int; and is then multiplied by the value of the left operand, 4. If one of the two operands of a numeric expression is of type int, then the other operand will be promoted to type int if it is of type short, char or byte.


----------------
the above explanation is true but its only half explantion... !!!

but why "a" and not "b" ??
as in above question... left side of == operator will be changed into double .... and which is not equalant to interger no. on the right hand side...




pls correct my post ...if wrong ...

thanx


Thanks and Regards, Amit Taneja
Akash Roy
Greenhorn

Joined: Jan 08, 2005
Posts: 16
Hi,

During == operator implicit widening takes place. Hence the Integer on the right side gets promoted to double.

Regards,
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Amit,

Did you try compiling and running the code? Playing around with it a little to see what it is doing? (see code below, I added a couple of print statements since I didn't remember what int value 'a' had and wanted to "SEE IT".)

If the == part was confusing, how about this:

1. static double a; static float b; static int c; static char d;
2. a = b = c = d = 'a';

Line 2 assigned a char to a char to an int to a float to a double!


amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
thanx a ton Carol Enderlin

regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dan exam dought 5
 
Similar Threads
method arguments ?
Precedence/Associativity
Dan Chisolm Exam Question
Questions from Dan and Green's Exams..
casting - from Dan's mock casting1