Puzzle 52 – "Satyameva Jayate" : Truth shall always prevail.

Sunday, September 13, 2009
Language – Java | Type – Concept | Last date 20-Sep-2009 12:00 p.m. IST | Points 3

What is the minimum change (additions only – no deleting or commenting out code) that you need to make to the code so that the program prints true?

This should be easy – but it does highlight another peculiarity to look out for.

package com.twister; public class MyTruth { public static void main(String[] args) { boolean b = Boolean.getBoolean("false"); System.out.println(b); //This should print true } }

Got an answer? Do leave it
here.

17 comments:

TheMalkolm said...

b -> !b

Yauheni said...

Minimal change is one symbol '!':
Either
boolean b = !Boolean.getBoolean("false");
or
System.out.println(!b);

Alper Kose said...

1 package com.twister;
2 public class MyTruth {
3 public static void main(String[] args) {
4 boolean b = Boolean.getBoolean("false");
5 System.out.println(b||true); //This should print true
6 }
7 }

kvf said...

System.out.println(b^!b);

c0dep0et said...

public class MyTruth {

public static void main(String[] args) {
boolean b = !Boolean.getBoolean("false");
System.out.println(b);
}
}

c0dep0et said...

public class MyTruth {

public static void main(String[] args) {
boolean b = !Boolean.getBoolean("false");
System.out.println(!b);
}
}

c0dep0et said...

public class MyTruth {

public static void main(String[] args) {
boolean b = Boolean.getBoolean("false");
System.out.println(!b);
}
}

Anonymous said...

Gerhard Balthasar: simply add ! before Boolean:

public static void main(String[] args) {
boolean b = !Boolean.getBoolean("false");
System.out.println(b); // This should print true
}

Sebastian said...

Actually no change is required to the program since the program asks for a system property named "false" and you can set that system property to "true" and run the program. It will then output "true":
java -Dfalse=true com.twister.MyTruth

However if by Boolean.getBoolean you mean Boolean.valueOf, adding a "!" can be used to negate the outcome, which is a one char addition that actually changes the logic.

Mohamed El-Beltagy said...

We have two solutions (with the assumption that it's required to always print 'true'):
1- (With no modifications at all to the code) Using command line when running the application:
java -Dfalse=true com.twister.MyTruth

2- (with one line addition - 36 characters) Changing the class to be:
package com.twister;
public class MyTruth {
public static void main(String[] args) {
System.setProperty("false", "true");
boolean b = Boolean.getBoolean("false");
System.out.println(b); //This should print true
}
}

tonthatduy said...

Change:
System.out.println(b);
To:
System.out.println(!b);

P/S: How about my solution for Puzzle 51?

nash said...

package com.twister;
public class MyTruth {
public static void main(String[] args) {
boolean b = Boolean.getBoolean("false");
System.out.println(!b);
}
}

// Adding '! ' before b in the println statement solves this puzzle

Taliesen said...

System.out.println(!b);

Chennai Vennai said...

package com.twister;
public class MyTruth {
public static void main(String[] args) {
boolean b = Boolean.getBoolean("false");
System.out.println(!b); //This should print true
}
}

vishwanath said...

1 package com.twister;
2 public class MyTruth {
3 public static void main(String[] args) {
4 boolean b = Boolean.getBoolean("false");
5 System.out.println(b=true); //This should print true
6 }
7 }

Mario said...

boolean b = !Boolean.getBoolean("false");

traveller said...

I can think of two solutions

1) boolean b = !Boolean.getBoolean("false");
Only one character '!' is being used

2) System.out.println(b=true);

Cheers

Post a Comment

Solution for this question?