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 question on operators 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 "question on operators" Watch "question on operators" New topic
Author

question on operators

Gauri Tilloo
Greenhorn

Joined: Jun 22, 2005
Posts: 5
What happens when you try to compile and run the following program?

public class Prog1 {
public static void main(String[] args) {
int k = 1;
int i = ++k + k++ + + k;
System.out.println(i);
}
}

Select the one correct answer.

a)The program will not compile. The compiler will complain about the expression ++k + k++ + + k.

b)The program will compile and will print the value 3 when run.

c)The program will compile and will print the value 4 when run.

d)The program will compile and will print the value 7 when run

Ans-d
I am not able to get that how to resolve last + + k.
I tried to run this program and the output is also 7.
if we evaluate this then...i=2 + 2 + + 3
So does this mean that no matter how many + signs u put it doesnt m,atter & its not gonna affect the output?
Nicky Eng
Ranch Hand

Joined: Mar 26, 2005
Posts: 378
hi...

i think you're right.
no matter how many + signs you have, the output still 7.

check this out:

public class Prog1 {
public static void main(String[] args) {
int k = 1;
int i = ++k + k++ + + + + + k;
System.out.println(i);
}
}

you still get 7 as output.

correct me if i'm wrong. thanks.
[ June 30, 2005: Message edited by: Nicky Eng ]

From NickyEng
Diploma in Computer Studies
SCJP 1.4
SCWCD 1.4
Formula 1 app by Maxis (Playbook)
bhavesh bhanushali
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
yes nick i think you are correct

regards ,
bhavesh
Biju Joseph
Greenhorn

Joined: Jul 30, 2003
Posts: 5
When you say int x = 6, implicitly it means we assign (+ve) 6 to the variable x.

int x = 6;
int x = +6;
int x = + 6;
int x = + + 6; //All will produce the same result (+ve 6).

int x = -6;
int x = + + - + 6; // (+ve) (+ve) (-ve) (+ve) 6. is negative 6.

But in the below case x will be (+ve 6).
int x = - - 6; // (-ve) (-ve) 6.

The ultimate conclusion is that, we have to do an AND operation across the + - operators. If you put any other operator(*,/), compiler will throw an error back.
Gauri Tilloo
Greenhorn

Joined: Jun 22, 2005
Posts: 5
Thank u all for the replies..this was a real strange thing i came across.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question on operators
 
Similar Threads
Is the expression correct??
Arrays - puzzling question?
a question
Khalid M Operator and Assigment question
Some Runtime vs. Compile Time SCJP 6.0 Mock Exam Questions