Are You a Java Expert? Test Your Knowledge With These Four Tricky Questions

Even though Java was designed to be an easy-to-use programming language, it still has many traps and surprises for the unwary.

In this article I ask grill you on four slightly lesser-known aspects of Java that may trip you up if you've migrated to Java from C++, you taught yourself or your course was chaired by a man with a greasy comb-over who mostly seemed to be rambling to himself about mutators and random access memory.

This test won't cover obscure aspects of the API or the virtual machine; we'll stick here to the quirks of the core programming language.

If you pass all four questions, you're either a proficient advanced Java programmer or you're just very lucky. After all, you've got a 1 in 8 chance of getting them right purely by chance. If you get them wrong, well you've learned something new ....

Question 1: Strings.



What does the following code print out?

public class Application {
    public static void main(String[] args) {
        
        String one = "Hello";
        String two = "Hello";
        
        if(one == two) {
            System.out.println("one == two");
        }
        else {
            System.out.println("one != two");
        }
    }
}






Question 2. Interfaces



Does the following code compile or not?

Application.java:

interface IFruit
{
    public String TYPE = "Apple";
}

class Fruit implements IFruit
{

}

public class Application {
    public static void main(String[] args) {
        System.out.println(Fruit.TYPE);
    }
}






Question 3. Access Modifiers



Does the following code compile or not?

Application.java:


class Fruit {
    protected static String name = "Sue";
}

class Apple extends Fruit {
    
}

public class Application {
    public static void main(String[] args) {
        System.out.println(Apple.name);
    }
}







Question 4. Unusual Class Declarations



Does the following code compile or not?


public class Application {
    public static void main(String[] args) {
        
        final class Constants {
            public static String name = "PI";
        }
        
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                System.out.println(Constants.name);
            }
            
        });
        
        thread.start();
    }
}







How Did You Do?



If you got all the answers right and for the right reasons, you're pretty damn good at Java.

Otherwise, well don't worry -- these were tricky questions!