This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP Brainteaser (8) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP Brainteaser (8) " Watch "SCJP Brainteaser (8) " New topic
Author

SCJP Brainteaser (8)

Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Here is another.....
Place your declaration of x and i





And the reverse one is also.:




Enjoy.....

Explanations: ???Needed properly....


The Best way to predict your future is to create it - Every great individual common man
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Hi Above,

The first one,

public class Sample{
public static void main(String[] args) {
byte i, x = 1;// Put your declarations for x and i here
x += i; // Must be LEGAL
x = x + i; // Must be ILLEGAL

}
}

The second one,

public class Sample{
public static void main(String[] args) {
double i, x = 1;// Put your declarations for x and i here
x = x + i; // Must be LEGAL
x += i; // Must be ILLEGAL
}
}

Thanks.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Hi Sharma Ji,

I have come across this before but I want to know the reason why this difference...When we say the declarations seperately like

double i = 1;
double x = 1;

it works fine, but when we club the declarations like,

double i, x = 1, it says the variable might not be initialized in the expression,x+=i;

but runs fine for x = x + 1;

What is the technical reason behind it?? Explanation would be appreciated.
[ November 10, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by Jothi Shankar Kumar Sankararaj:

double i = 1;
double x = 1;

it works fine, but when we club the declarations like,

double i, x = 1, it says the variable might not be initialized in the expression,x+=i;

but runs fine for x = x + 1;


writing doesn't means that x is initialized.

that means you are writing same as



Hope it helps you out.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Hi Sharma Ji,

Your Quote,

code:

int x,i=1;

doesn't means that x is initialized.

that means you are writing same as

code:

int x;
int i=1;


Hope it helps you out.


Then how come,

double i, x = 1;// Put your declarations for x and i here
x = x + i; // Must be LEGAL
x += i; // Must be ILLEGAL

Works out?? As per your post, I'm doing like,

double i;
double x = 1;
x = x + i; //How this becomes legal now??? i is still not initialized...

Any thoughts on that??
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by Jothi Shankar Kumar Sankararaj:
Hi Sharma Ji,

Your Quote,



Then how come,

double i, x = 1;// Put your declarations for x and i here
x = x + i; // Must be LEGAL
x += i; // Must be ILLEGAL

Works out?? As per your post, I'm doing like,

double i;
double x = 1;
x = x + i; //How this becomes legal now??? i is still not initialized...

Any thoughts on that??


I got somehow confused,what you are using exactly, and which one is working according to you...

Could you please show us. what you have done as declarations..

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Hi Sharma Ji,

I'm using netBeans 5.0 to run my programs, and now I'm realizing the disadvantage of using an IDE to learn...

When I tried the code below,

double i, x = 1;
x += i; // Must be LEGAL
x = x + i; // Must be ILLEGAL

My IDE showed error only in the first line...I mean I did not compile it and it showed me by underlining the first line in red that variable might not have been initialized but nothing was shown in the next line which confused me. After I commented the first line, I got it cleared.

Anyways, for the first question you posted, the explanation is compound assignment operators does not require an explicit cast but whereas normal operators would result in an int value for which an explicit cast to int is required. On the other hand if one of the variable is of type double, the resultant of the arithmetic operation is promoted to double.

What's the logic behind the second question???
Dan Polak
Ranch Hand

Joined: Nov 06, 2006
Posts: 32
hello

the first one is easy

short x = 0;
int i = 1;// Put your declarations for x and i here
x += i; // Must be LEGAL
x = x + i; // Must be ILLEGAL


is te second possible ???


<a href="http://www.dantheman.pl" target="_blank" rel="nofollow">http://www.dantheman.pl</a>
Ganesh Pujar
Ranch Hand

Joined: Mar 22, 2006
Posts: 45
Any explenations for the second? please.
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by Ganesh Pujar:
Any explenations for the second? please.


First we are expecting some solution atleast, then only we can give some explanations.

Right ?
Ganesh Pujar
Ranch Hand

Joined: Mar 22, 2006
Posts: 45
Compiler helps!


Above code does not work for BOTH

So that means, we dont have the answer for the ABOVE yet.
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Aren's this working for first question.

Just check




Now we are seeking for second question .....
Ganesh Pujar
Ranch Hand

Joined: Mar 22, 2006
Posts: 45
Please answer it Sharma Ji
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Sharma Ji,

Is there a way for the second question??
 
 
subject: SCJP Brainteaser (8)