Sunday, June 7, 2009

Puzzle 31 - Falsify

Language – Java | Type – Concept | Last date 10-June-2009 9:00 p.m. IST | Points 3

The next puzzle is a pretty short one but as usual has a Java principle involved. I think this one is a little tough though I sure you folks will come up with solutions I did not think off.

package com.twisters
class TheTest{
public boolean win(){
return false; //Get this code to always return true.
}
}

We all know that truth always wins. So the method win() should always return true.

Add any amount of code you like but no deletes. Can you tell me the main principle involved in your solution?
Don’t use if, while, do-while and for keywordsYou are not allowed to use any of them for this puzzle!

Added : And before I completely forget no boolean or logical operators either - so no !, &, &&, |, || or any other such operator that works on boolean values! For those have already answered - this clause won't apply!!

Finally if you still haven’t figured this out – my suggestion is try again!!

Got an answer? Do leave it here.

43 comments:

  1. class TheTest{
    public boolean win(){
    return !false; //Get this code to always return true.
    }

    public static void main(String[] args){
    System.out.println(new TheTest().win());
    }
    }

    ReplyDelete
  2. package com.twisters;
    import java.util.EmptyStackException;
    import java.util.Stack;

    public class TheTest {

    public static boolean win() {
    try {
    new StackInteger().pop(); // Add <> on Integer, html wasn't accepted
    return false;
    } catch (EmptyStackException e) {
    return true;
    }
    }
    }

    Popping an empty stack will always generate the exception, therefore will always return true. Was the first exception i could think of xD

    ReplyDelete
  3. Again 2 solutions but the same underlying principle, false is a JAVA keyword but we can append any string to it to make it into a literal.

    So -
    package com.twisters;

    class TheTest{
    public boolean win(){
    boolean false1=true;
    return false1;
    }
    }

    OR

    package com.twisters;

    class TheTest{
    public boolean win(){
    return false_method();
    }
    private boolean false_method(){
    return true;
    }
    }

    ReplyDelete
  4. Hey

    I havent been given points for my answer to solution 30. Also for the earlier questions all the answers by Abhi are from me, so could you please collate all the entries into one and link onto this profile :)

    ReplyDelete
  5. 1. rename "false" to "ffalse" and declare boolean static variable ffalse set to true;
    2. turn "false" into method invocation "false()" and declare false() as method that returns true always.
    3. even can turn "false" into smth like
    "new Boolean(false).equals(false)" or "new Boolean(false).TRUE"
    4. fun but no "switch" is forbidden in post, so its pretty easy to make programm run always with true in result.

    ReplyDelete
  6. Hi!
    I'm not sure... but .. it doe's compile at least ;-)
    http://www.blogtrog.com/code.aspx?id=52ea5ef2-1e76-4d1b-b3c8-0270459feb22

    Best regards,

    ReplyDelete
  7. use switch instead of if:
    public boolean win() {
    switch (1) {
    case 0:
    return false;
    default:
    return true;
    }
    }

    finally allways wins :
    public boolean win() {
    try {
    return false;
    } finally {
    return true;
    }
    }

    From keyword to identifier :
    public boolean win() {
    boolean falser = true;
    return falser;
    }

    Ternary operator :
    public boolean win() {
    return false?true:true;
    }

    ReplyDelete
  8. package com.twisters
    class TheTest{
    public boolean win(){
    try {
    return false; //Get this code to always return true.
    } finally {
    return true;
    }
    }
    }

    ReplyDelete
  9. I forgot to explain why my solution works. In Java, finally always runs. Always. If you throw or change the return value in a finally, anything in the try is overridden.

    package com.twisters
    class TheTest{
    public boolean win(){
    try {
    return false; //Get this code to always return true.
    } finally {
    return true;
    }
    }
    }

    ReplyDelete
  10. I'm not quite sure what the principle is, but I've got a solution!

    class TheTest{
    public static void main(String[] args){
    TheTest testExample = new TheTest();
    System.out.println(testExample.win());
    }


    public boolean win(){
    return (false==false); //Get this code to always return true.
    }
    }

    ReplyDelete
  11. Well if this is not the solution u were expecting u will bang ur head on wall saying y u didnt add rule not to amend existing statements.


    class TheTest{
    public boolean win(){
    return falses();
    }
    public boolean falses(){
    return true;
    }
    }

    public class Sample {
    public static void main(String args[]) {
    boolean result = new TheTest().win();
    System.out.println("Result is :" + result);
    }
    }

    ReplyDelete
  12. Well if this is not the solution u were expecting u will bang ur head on wall saying y u didnt add rule not to amend existing statements.


    class TheTest{
    public boolean win(){
    return falses();
    }
    public boolean falses(){
    return true;
    }
    }

    public class Sample {
    public static void main(String args[]) {
    boolean result = new TheTest().win();
    System.out.println("Result is :" + result);
    }
    }

    so if its not corrent answer dont count, I'm still scratching brains for another things. will mail u if i come up something more b4 deadline.

    ReplyDelete
  13. public class a {
    public boolean win()
    {
    if(false)
    return false;
    else
    return true;
    }
    public static void main(String[] args) {
    boolean a = new a().win();
    System.out.println(a);
    }
    }

    ReplyDelete
  14. class TheTest {

    public static boolean win() {
    return false == false;
    // Get this code to always return true.
    }

    public static void main(String[] args) {
    System.out.println(win());
    }
    }

    ReplyDelete
  15. package com.twisters
    class TheTest{
    public boolean win(){
    switch(0){case 0: return true;}
    return false; //Get this code to always return true.
    }
    }

    ReplyDelete
  16. package com.twisters
    class TheTest{
    public boolean win(){
    try {
    return false; //Get this code to always return true.
    } finally {
    return true;
    }
    }
    }

    ReplyDelete
  17. package com.twisters
    class TheTest{
    public boolean win(){
    return false?false:true;
    }
    }

    ReplyDelete
  18. package com.twisters
    class TheTest{
    public boolean win(){
    boolean false_ = true;
    return false_; //Get this code to always return true.
    }
    }
    }

    ReplyDelete
  19. package com.twisters
    class TheTest{
    public boolean win(){
    try{
    return false; //Get this code to always return true.
    }catch(Exception e){}
    finally{ return true; }
    }
    }

    ReplyDelete
  20. 1 package com.twisters
    2 class TheTest{
    3 public boolean OLDwin(){
    4 return false; //Get this code to always return true.
    5 }
    public boolean win() { return true; }
    6 }

    Note: rules just said no deletions - nothing about renaming. :-)

    or alternatively, just insert lines to change the return value :

    1 package com.twisters
    2 class TheTest{
    3 public boolean win(){
    return true;
    }
    public boolean loose() {
    4 return false; //Get this code to always return true.
    5 }
    6 }

    Line numbers refer to original code.

    ReplyDelete
  21. package com.twisters
    2 class TheTest{
    3 public boolean win(){
    int i =0;
    switch(i){
    case 1:
    return true;
    default:
    4 return false;
    }
    5 }
    6 }

    ReplyDelete
  22. public boolean win(){
    try {
    return false; //Get this code to always return true.
    } finally {
    return true;
    }
    }

    ReplyDelete
  23. ... finally { return true; }

    ReplyDelete
  24. return false ? true : true;

    ReplyDelete
  25. return true ^ false;

    The bitwise ^ operator performs a bitwise exclusive OR operation.

    ReplyDelete
  26. This will also work:

    return false == false;

    ReplyDelete
  27. First I thought of using "!" before the "false", but since it isn't allowed, I would rather use a "return true;" as the first line of the method. If you like, I can also add a leading "//" in front of the line "return false;".
    The principle used is that the first return is always executed.

    Maybe you can add a switch statement to the code, but it will be just fine the way I suggested.

    ReplyDelete
  28. First I thought of using "!" before the "false", but since it isn't allowed, I would rather use a "return true;" as the first line of the method. If you like, I can also add a leading "//" in front of the line "return false;".
    The principle used is that the first return is always executed.

    Maybe you can add a switch statement to the code, but it will be just fine the way I suggested.

    ReplyDelete
  29. Another one:

    try { return true; } catch (Exception e) { return false; }

    ...or a nastier variation:

    try { return false; } finally { return true; }

    ReplyDelete
  30. package com.twisters
    class TheTest{
    public boolean win(){
    try {
    int[] array = new int[1];
    int b = array[1];
    } catch (Exception e) {
    return true;
    }

    return false;
    }
    }

    ReplyDelete
  31. ...and a cheeky one for fun:

    class TheTest{
    private static final boolean notfalse = true;
    public boolean win() {
    return notfalse; // ...
    }
    }

    I'm sure I could think of plenty more but that'll do for now. Looking forward to see what others have come up with.

    ReplyDelete
  32. package com.twisters
    class TheTest{
    public boolean win(){
    int i =0;
    switch (i) {
    case 0:
    return true;
    default:
    break;
    }
    return false; //Get this code to always return true.
    }
    }

    ReplyDelete
  33. package com.twisters
    class TheTest{
    public boolean win(){
    return false==false; //Get this code to always return true.
    }
    }

    ReplyDelete
  34. Not sure if this is against the rules. I didn't delete anything only added code around it :)

    package com.twisters;

    class TheTest {
    public boolean win(){
    return inverse(false); //Get this code to always return true.
    }

    public boolean inverse(boolean value) {
    return true;
    }
    }

    ReplyDelete
  35. class TheTest {
    private TheTest() {
    }

    public static TheTest buildTest() {
    return new TheTest2();
    }

    public boolean win() {
    return false;
    }
    }

    class TheTest2 extends TheTest {
    @override public boolean win() {
    return true;
    }
    }

    ReplyDelete
  36. package com.twisters
    class TheTest{
    public boolean win(){
    return true;
    return false; //Get this code to always return true.
    }
    }

    first return wins :)

    ReplyDelete
  37. try {
    return true;
    } catch(Exception e) {
    return false; //Get this code to always return true.
    }

    ReplyDelete
  38. public class Example {

    public boolean win() {

    return (false) ? true : true; //Get this code to always return true.
    }

    public static void main(String[] args) {
    Example i = new Example();
    System.out.println(i.win());
    }
    }

    ReplyDelete
  39. Nice question to get the mind back on track after a long night in front of the monitor ! :)

    A solution could be to let the API throw an exception, catch it and return the desired value:

    public boolean win() {
    try {
    System.out.println(1 / 0);
    } catch (ArithmeticException ex) {
    return true;
    }
    return false;
    }

    ReplyDelete
  40. package com.twisters
    2 class TheTest{
    3 public boolean win(){
    4 return false; //Get this code to always return true.
    5 }
    6 }

    ReplyDelete
  41. package com.twisters
    class TheTest{
    class SomeInnerClass {
    boolean someMethod() {
    return false;
    }
    }
    return true;
    }

    ReplyDelete

Solution for this question?