Category Archives: Java Interview Questions

There are many lists of interview questions. Often with wrong answers. I’ll try to explain why they are so wrong.

Note: In the category “Java Misnomers” are some more posts that could be helpful while preparing for job interviews.

import static java.lang.System.out;

Simple two-liner:

static void f(int a, int z) {
	out.println(a % 15 < 1 ? "FizzBuzz" : a % 3 < 1 ? "Fizz" : a % 5 < 1 ? "Buzz" : a);
	if (a < z) f(1 + a, z);
}

Without using the literal “FizzBuzz”:

static void g(int a, int z) {
	int x = 0;
	if (a % 3 < 1 && ++x > 0) out.print("Fizz");
	if (a % 5 < 1 && ++x > 0) out.print("Buzz");
	if (x < 1) out.print(a);
	out.println();
	if (a < z) g(1 + a, z);
}

Note that I use x<1 instead of x==0 to save a single character.

Advertisements