Java Developers Group


Here you will see some featured Java Code Puzzles our members have shared over time. Enjoy considering them then discuss them on the Java Developer group on Linkedin.com.
If you have new ones you would like to share you can just post them on our LinkedIn Group.

Java Coding Puzzle 1

If you like this, visit our LinkedIn Group - Java Developers

Try compiling and running the code below - then uncomment for loop compile and run.

Why does this program have an error when for loop is commented out?

public class JavaMemoryPuzzlePolite {

private final int dataSize = (int)(Runtime.getRuntime().maxMemory()* 0.6);

public void f(){

{

System.out.println(dataSize);

byte[] data = new byte[dataSize];

}

/*for(int i = 0; i < 10; i++){

System.out.println("Please be so kind and release memory");

}*/

System.out.println(dataSize);

byte[] data2 = new byte[dataSize];

}

public static void main(String []args){

JavaMemoryPuzzlePolite jmp = new JavaMemoryPuzzlePolite();

jmp.f();

}

}

Java Coding Puzzle 2

Java Puzzle: 2 threads - 1 variable

Submitted by:

Victor Perepelitsky - member of Java Developers Group on LinkedIn.

If you like this, visit our LinkedIn Group - Java Developers
Hi all,

I've lately encountered with a puzzle I loved very much.
Sharing it with you:

Given a program:

public class ThreadsLimits extends Thread {
static volatile int x;

public void run() {
for (int i = 0; i < 10; i++){
int temp = x;
temp++;
x = temp;
}
}

public static void main(String[] args) {
Thread t1 = new ThreadsLimits();
Thread t2 = new ThreadsLimits();

t1.start();
t2.start();

}

}

What can you say about the value of x by the end of the program? (Assume both threads ended its execution)

Enjoy
If you like this, visit our LinkedIn Group - Java Developers

Java Coding Puzzle 3


What does the following print (assuming it compiles, which it does):
public class StrungOut {
public static void main(String[] args) {
String s = new String("Hello world");
System.out.println(s);
}
}

class String{
private final java.lang.String s;
public String(java.lang.String s){
this.s = s;
}
public java.lang.String toString(){
return s;
}
}
If you like this, visit our LinkedIn Group - Java Developers

Java Coding Puzzle 4

Ok here is another entertaining one: If you run this program 1000 times, what will it print most(if not all) of the times?
import java.util.Random;
public class Rhymes {
private static Random rnd = new Random();
public static void main(String[] args) { StringBuffer word = null;
switch(rnd.nextInt(2)){ case 1: word = new StringBuffer('P'); case 2: word = new StringBuffer('G'); default: word = new StringBuffer('M'); } word.append('a'); word.append('i'); word.append('n'); System.out.println(word); }
}
If you like this, visit our LinkedIn Group - Java Developers

Java Coding Puzzle 5

Here’s one: what can you put in the blank in line 3 to get this to compile? 2: public class Test { 3: _____ int integerField = 10; 4: byte byteField = integerField; 5: }
If you like this, visit our LinkedIn Group - Java Developers
What does this program print?
/* MIND THE GAP */
import java.io.*;
public class Gap {
private static final int GAP_SIZE = 10 * 1024;
public static void main(String[] args) throws Exception{
File tmp = File.createTempFile("gap", ".txt");
FileOutputStream out = new FileOutputStream(tmp);
out.write(1);
out.write(new byte[GAP_SIZE]);
out.write(2);
out.close();
InputStream in = new BufferedInputStream(new FileInputStream(tmp));
int first = in.read();
in.skip(GAP_SIZE);
int last = in.read();
System.out.println(first * last);
}

}
Java Developer Group Logo
Java Developer Group Logo
Featured Homes
Click picture for details
Featured Home