Showing posts with label Operator. Show all posts
Showing posts with label Operator. Show all posts

Sunday, May 17, 2009

Puzzle 25 - Deja-Vu (Code completion -3)

Language – Java | Type – Problem (Code) | Last date 20-May-2009 9:00 p.m. IST | Points 2

I spend some time making a few changes with the blog look and feel – customizing thing around – I especially like the social sharing icons I put down there – gives the blog a lot more color! I wonder if there is a hint hidden here :). Folks time to get on with the next question.

If you haven't had a look at the previous puzzle – do have a look at it before you begin this one!

For the rest – getting a feeling of Deja-Vu? Think you have seen this puzzle before? – you have it's the same question as the previous one. Ditto – Same.

package com.twisters;
public class UnIncrement{
public static void main(String[] args) {
int x = 0;
x
++;
System.out.println(
"X is : " + x);//This must print 'X is 0'
}
}


Get the System.out.prinltn() to print 'X is 0' at line 6.
You may add (but not delete) any additional amount of code.

(Same lines – thats because I copy pasted them!!!)

Here the change – the puzzle a lot simpler now. My clause "You can just add one character to the above code to get it to work - any one character but that all"

Site of the day - http://interviewpattern.com/post/Puzzles-and-Riddles-on-the-interview-(Part-e28093-I).aspx. If you like analytical puzzles you might enjoy the post!

Got an answer? Do leave it here.

Thursday, May 14, 2009

Puzzle 24 - Recessional Increment (Code Complete - 2)

Language – Java | Type – Problem (Code) | Last date 17-May-2009 12:00 p.m. IST | Points 2

Without much ado - here is the next puzzle!

package com.twisters;
public class UnIncrement{
public static void main(String[] args) {
int x = 0;
x
++;
System.out.println(
"X is : " + x);//This must print 'X is 0'
}
}

Get the System.out.prinltn() to print 'X is 0' at line 6.
You may add (but not delete) any additional amount of code.

Sounds too simple doesn't it? Time to throw in a few additional clauses - generously I’ll just add one.
"You can just add two characters to the above code to get it to work - any two characters buts that all"

If you’re wondering why this puzzle is so easy - its because I have a few plans up my sleeves to make this more interesting in my next post-:)

Link of the day - http://javatutorialsworld.blogspot.com/2009/05/simple-java-twisters.html - have fun with the increment operator and see if you can get all six correct!

Got an answer? Do leave it here.

Wednesday, April 1, 2009

Puzzle-12 Object or Value

Language – Java | Type – Concept | Last date 05-Apr-2009 12:00 p.m. IST | Points 2

We all know that ‘==’ operator check for Object reference equality. It’s not supposed to compare values. That is why I was bugged when I tried the code below and got the ‘impossible output’. Can you tell me why?
package com.twisters;
class IntegerEquality{

public static void main(String args[]){
Integer first
= 100;
Integer second
= 100;
if(first == second)
System.out.println(“This is impossible
!!”)
else
System.out.println(“Everything is as expected out here.”)
}
}
Got an answer? Why don't you leave it here.

Wednesday, March 25, 2009

Puzzle 9 - Solution

The key to solving this puzzle is understanding 'hardware limitation'. Floating point numbers are represented in IEEE floating point format - however floating point operation - addition, subtraction, multiplication - don't give an accurate result but always have a small error associated with them due to hardware limitation.

The for loop for(double d = 0.0; d != 1.0; d = d + 0.1) does floating point addition. However as floating point addition is not 100% accurate adding 0.1 10 times gives us a number slightly more than 1.0 (say 1.00000001). The equality condition (1.0 == 1.00000001) fails and the loop continues forever.

Key Learning

With floating point numbers it is better to use the greater-or-equal or less-or-equal operator instead of equality operator i.e. use '<=' or '=>' instead of '=='.

Sorry no correct answers this week!

Wednesday, March 11, 2009

Puzzle 5 - Solution

The key to solving this puzzle is 'Operator Precedence'.
The post decrement operator '--' has precedence over the subtraction operator '-'.

The evaluation steps are as follows,

Step 1 : result = (a--)-b;
Step 2 : result = (5--)-1;
Step 3 : result = 4

The post decrement happens for 'a' so its value is decremented by 1.

The result is,

Value of a is : 4
Value of b is : 1
Value of result is : 4

Sorry no correct answers this week!

Sunday, March 8, 2009

Puzzle 5 – The Sign

Language – Java | Type – Concept | Last date 11-Mar-2009 9:00 p.m. IST | Points 2

Too many signs spoil the code …
package com.twisters;
public class Signs{
public static void main(String[] args) {
int result;
int a = 5, b =1;
result = a---b;
System.out.println("Value of a is : " + a);
System.out.println("Value of b is : " + b);
System.out.println("Value of result is : " + result);
}
}

Got an answer? Why don’t you leave it here.