Intro
I hate majority of Java quizzes. They are just boring.
First category of them forces you to track tricky assignments between several variables spread among crazy switch/case and for instructions. If you know how to find the answer then it's fine. If not - not so bad, majority of code you are working with is more readable. But if you create something like this in your own code, your team mates will kill you.
Second ones are dumb API questions. What interfaces are used in RMI? What interface is necessary to implement to make servlet thread-safe? Rrrrh...
However, there are some questions that are funny but may crack your brains. The ones that developers themselves use to quiz each other. Here is a small collection of my favorite questions that I heard from colleagues and saw in forums.
I have a small conversation with Craig regarding points for correct answers. Actually, I had suggested 10, 20, 30 points for every question correspondingly. Craig doubled amount of points, probably because October is a month of Java on SDN 🙂
You may either mail me your answers to my alternate e-mail address (see my business card) or post it here as comments. First way is preferred, while more then one user may win - take on account time zones, someone probably sleep when you are reading this. SDN is a global thing.
Question 1: Magic Output (20 points)
Let us start with the simplest question. Actually, this is not even advanced Java - only intermediate skills are necessary.
Consider the following Java class:
package com.sap.sdn.jquiz;
public class MagicOutput {
public static void main(final String argv[]) {
System.out.println("Hello, World!");
}
}
While preserving both method main signature (name, number and type of parameters, return type) and implementation (body of method), make this program to output to standard console message "Hello, SDN!" Again, you may not alter a single character within the method main!!! The resulted class must be executable with JRE 1.4; you may not use classes besides public API of JDK 1.4
Question 2: As short as possible but not shorter (40 points)
I found this one quite long time ago. Actually, if you are programming animation or some multithreading applications, or console application that accepts input from screen, then sooner or later you may notice the behavior, necessary to answer this question. It's hardly a hint, just my own observation 🙂
You have the following Java class:
package com.sap.sdn.jquiz;
public class Minimalizmo {
//START
//END
}
Between START and END comments, add the shortest (in number of characters) possible method with return type other then void. Resulted class must be syntactically correct, i.e. it must compiles successfully on JDK1.4; you may not use classes besides public API of JDK 1.4.
Just a notice to those who want to game the rules: if you are using classes outside java.lang package, you must use fully qualified names, i.e. no import statements please.
Question 3: Green Java (60 points)
This is my favorite one, and, frankly it's old as Java. Or as the first Java IDE with syntax highlighting. It's simply a colorized fun! 🙂
You are probably noticed, that popular Java IDE-s like Eclipse-based (Eclipse itself and NetWeaver IDE for example), NetBeans and IntelliJ IDEA tend to display commented out code in green. Ok, IDEA starting from version 5.0 prefers to use grey by default. Anyway, the question is: write a classical program that outputs "Hello, SDN World!" on console but its source is shown by IDE totally in green. Every line and every character is green. Or grey 😉 Like commented out code.
Just a notice to those who want to game the rules: tweaking IDE settings is not a solution.