Puzzle 55 – The No constructor dilemma.

Monday, October 12, 2009
Language – Java | Type – Concept | Last date 18-Oct-2009 12:00 p.m. IST | Points 3

This week’s puzzle is pretty simple and the question self explanatory (I hope).

package com.twisters;
public class NoConstructor {

boolean isConstructor = false; //No changes permitted to this line

/* No Code may be added or changed in main*/
public static void main(String[] args) {
NoConstructor noConstructor
= new NoConstructor();
System.out.println(noConstructor.isConstructor);
//Prints true
}
}

The NoConstructor class must not have any explicit constructor (that is - don’t use the word NoConstructor any more times in your solution). That's all - everything else about the puzzle should be self explanatory from the comments!

Got an answer? Leave one here

8 comments:

radu said...

package com.twisters;
public class NoConstructor {

boolean isConstructor = false; //No changes permitted to this line

boolean isTrue = isConstructor = true;

/* No Code may be added or changed in main*/
public static void main(String[] args) {
NoConstructor noConstructor = new NoConstructor();
System.out.println(noConstructor.isConstructor); //Prints true
}
}

c0dep0et said...

package com.twisters;

public class NoConstructor {


boolean isConstructor = false; // No changes permitted to this line

{
isConstructor = true;
}


/* No Code may be added or changed in main */
public static void main(String[] args) {
NoConstructor noConstructor = new NoConstructor();
System.out.println(noConstructor.isConstructor); // Prints true
}
}

TheMalkolm said...

use instance initializer

Anonymous said...

public class NoConstructor {
boolean isConstructor = false; //No changes permitted to this line

{isConstructor = true;}

/* No Code may be added or changed in main*/
public static void main(String[] args) {
NoConstructor noConstructor = new NoConstructor();
System.out.println(noConstructor.isConstructor); //Prints true
}
}

Don't think I can get much shorter than one line...

$ javac NoConstructor.java
$ java NoConstructor
true

Eric Jablow said...

package com.twisters;
public class NoConstructor {

boolean isConstructor = false; //No changes permitted to this line
{isConstructor = true;} // Instance initializer.

/* No Code may be added or changed in main*/
public static void main(String[] args) {
NoConstructor noConstructor = new NoConstructor();
System.out.println(noConstructor.isConstructor); //Prints true
}
}

IBBoard said...

The "take the comments very literally" solution:

package com.twisters;
public class NoConstructor {
/*
boolean isConstructor = false; //No changes permitted to this line
*/
boolean isConstructor = true;

/* No Code may be added or changed in main*/
public static void main(String[] args) {
NoConstructor noConstructor = new NoConstructor();
System.out.println(noConstructor.isConstructor); //Prints true
}
}

Just because the line can't be changed doesn't mean that you're not allowed to change the line before and after it ;)

alaindefrance said...

public class NoConstructor {
boolean isConstructor = false;
{isConstructor = true;}
// or : {isConstructor ^= !isConstructor;}
public static void main(String[] args){
NoConstructor noConstructor = new NoConstructor();
System.out.println(noConstructor.isConstructor);
}
}

Anonymous said...

boolean isConstructor = false; //No changes permitted to this line

{
isConstructor = true;
}

/* No Code may be added or changed in main*/
public static void main(String[] args) {
Twister noConstructor = new Twister();
System.out.println(noConstructor.isConstructor); //Prints true
}

Post a Comment

Solution for this question?