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 keywords – You 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.
class TheTest{
ReplyDeletepublic boolean win(){
return !false; //Get this code to always return true.
}
public static void main(String[] args){
System.out.println(new TheTest().win());
}
}
package com.twisters;
ReplyDeleteimport 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
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.
ReplyDeleteSo -
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;
}
}
Hey
ReplyDeleteI 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 :)
1. rename "false" to "ffalse" and declare boolean static variable ffalse set to true;
ReplyDelete2. 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.
Hi!
ReplyDeleteI'm not sure... but .. it doe's compile at least ;-)
http://www.blogtrog.com/code.aspx?id=52ea5ef2-1e76-4d1b-b3c8-0270459feb22
Best regards,
use switch instead of if:
ReplyDeletepublic 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;
}
package com.twisters
ReplyDeleteclass TheTest{
public boolean win(){
try {
return false; //Get this code to always return true.
} finally {
return true;
}
}
}
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.
ReplyDeletepackage com.twisters
class TheTest{
public boolean win(){
try {
return false; //Get this code to always return true.
} finally {
return true;
}
}
}
I'm not quite sure what the principle is, but I've got a solution!
ReplyDeleteclass 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.
}
}
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.
ReplyDeleteclass 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);
}
}
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.
ReplyDeleteclass 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.
public class a {
ReplyDeletepublic boolean win()
{
if(false)
return false;
else
return true;
}
public static void main(String[] args) {
boolean a = new a().win();
System.out.println(a);
}
}
class TheTest {
ReplyDeletepublic static boolean win() {
return false == false;
// Get this code to always return true.
}
public static void main(String[] args) {
System.out.println(win());
}
}
return new Boolean(false).TRUE;
ReplyDeletepackage com.twisters
ReplyDeleteclass TheTest{
public boolean win(){
switch(0){case 0: return true;}
return false; //Get this code to always return true.
}
}
package com.twisters
ReplyDeleteclass TheTest{
public boolean win(){
try {
return false; //Get this code to always return true.
} finally {
return true;
}
}
}
package com.twisters
ReplyDeleteclass TheTest{
public boolean win(){
return false?false:true;
}
}
package com.twisters
ReplyDeleteclass TheTest{
public boolean win(){
boolean false_ = true;
return false_; //Get this code to always return true.
}
}
}
package com.twisters
ReplyDeleteclass TheTest{
public boolean win(){
try{
return false; //Get this code to always return true.
}catch(Exception e){}
finally{ return true; }
}
}
1 package com.twisters
ReplyDelete2 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.
package com.twisters
ReplyDelete2 class TheTest{
3 public boolean win(){
int i =0;
switch(i){
case 1:
return true;
default:
4 return false;
}
5 }
6 }
public boolean win(){
ReplyDeletetry {
return false; //Get this code to always return true.
} finally {
return true;
}
}
... finally { return true; }
ReplyDeletereturn false ? true : true;
ReplyDeletereturn true ^ false;
ReplyDeleteThe bitwise ^ operator performs a bitwise exclusive OR operation.
This will also work:
ReplyDeletereturn false == false;
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;".
ReplyDeleteThe 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.
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;".
ReplyDeleteThe 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.
Another one:
ReplyDeletetry { return true; } catch (Exception e) { return false; }
...or a nastier variation:
try { return false; } finally { return true; }
package com.twisters
ReplyDeleteclass TheTest{
public boolean win(){
try {
int[] array = new int[1];
int b = array[1];
} catch (Exception e) {
return true;
}
return false;
}
}
...and a cheeky one for fun:
ReplyDeleteclass 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.
package com.twisters
ReplyDeleteclass TheTest{
public boolean win(){
int i =0;
switch (i) {
case 0:
return true;
default:
break;
}
return false; //Get this code to always return true.
}
}
package com.twisters
ReplyDeleteclass TheTest{
public boolean win(){
return false==false; //Get this code to always return true.
}
}
return false ? false : true;
ReplyDeleteNot sure if this is against the rules. I didn't delete anything only added code around it :)
ReplyDeletepackage com.twisters;
class TheTest {
public boolean win(){
return inverse(false); //Get this code to always return true.
}
public boolean inverse(boolean value) {
return true;
}
}
class TheTest {
ReplyDeleteprivate TheTest() {
}
public static TheTest buildTest() {
return new TheTest2();
}
public boolean win() {
return false;
}
}
class TheTest2 extends TheTest {
@override public boolean win() {
return true;
}
}
package com.twisters
ReplyDeleteclass TheTest{
public boolean win(){
return true;
return false; //Get this code to always return true.
}
}
first return wins :)
try {
ReplyDeletereturn true;
} catch(Exception e) {
return false; //Get this code to always return true.
}
public class Example {
ReplyDeletepublic 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());
}
}
Nice question to get the mind back on track after a long night in front of the monitor ! :)
ReplyDeleteA 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;
}
package com.twisters
ReplyDelete2 class TheTest{
3 public boolean win(){
4 return false; //Get this code to always return true.
5 }
6 }
package com.twisters
ReplyDeleteclass TheTest{
class SomeInnerClass {
boolean someMethod() {
return false;
}
}
return true;
}