Panasonic Youth

Java technical interview guide: part two - some answers

In July I posted a set of questions for a Java technical evaluation based on an eval I did for my employer. This is part two where I add some answers to those questions.

Interviewers: many of these questions are very open ended and this post only lists one of many right answers. This is only a starting point for conducting a decent Java interview, its no replacement for face to face time, live coding, etc.

Developers/interviewees: if you are relying soley on this post and others like it on to get through interviews or, heaven forbid, to get a job, you are only hurting yourself in the long run. =)

Object Oriented Principles

  • Define object oriented programming (quite a loaded question...). What are the key OO principles?

    A fairly standard definition is the idea of breaking up a program into abstract objects. An object is a unit capable of receiving and sending messages. The three typical OO attributes are polymorphism, encapsulation, and inheritance, though not all OO languages have inheritance.

  • Describe polymorphism.

    Poly = many, morph = form, ie "many forms". Polymorphism allows objects to take on different behavior depending on the runtime type, which the client need not be aware of. A typical concrete Java example is where you have an interface Shape defined, with a area method. Any concrete types that implement shape can return their area using their own algorithm, which clients do not have to be aware of.

  • What is coupling? Why do we want loose coupling in our systems?

Java Basics

Some very fundamental questions I think any “non-beginner” level dev should know. If there are a lot of problems with these it may not be worth continuing.

  • What is the difference between abstract classes and interfaces?

    An abstract class defines the contract and optionally default implementation for its methods - an interface only defines the contract (ie method signatures) - no implementation. Abstract classes can have public or protected members, an interface can only have public members..

  • Explain the access modifiers in Java.

    Access modifier specify where a method or attribute can be used. Public - accessible from anywhere/anything. Protected - accessible from the type where its defined or any subclasses. Package - accessible from the class or subclass, but only within the same package. Private - only accessible from within the class.

  • Talk about method overriding and method overloading and their differences.

    A method is overrode when you define a method with the same signature as an accessible method in a superclass. Method overloading is actually creating an entirely unrelated method with a different signature.

  • Why does Java have exceptions? What is the difference between checked and unchecked exceptions?

    Exceptions handle unexpected conditions. If an exception is checked, it must be declared as part of the method signature using throws or it must be handled using try...catch. Checked exceptions are typically for things that can be recovered from, such as a database record lock or bad user input. Unchecked exceptions do not have to be explicitly handled by the programmer, and are for things that cannot be recovered from, such as programmer error or out of memory errors.

  • What are constructors? Are they inherited?

    Constructors are special methods used to create objects. They are not inherited, but superclass constructors can be called using the super keyword. If super is called it must be the first line in a constructor.

  • What is the static modifier used for in regards to classes and methods?

    A static method is available on the class and is not associated with any specific instance. Since a static method is at the class level, it cannot access instance data and can called without creating any instances of the class.

  • What are the equals() and hashcode() ? Describe their contracts.

    Refer to Effective Java Effective Java Programming Language Guide, Bloch is the man on this type of thing.

  • When do you use String and when do you use StringBuffer/StringBuilder

    To over generalize, use StringBuffer/Builder where you want to construct or process a String in some sort of expensive process, such as a long-running loop. Use Strings for any other case.

Java Intermediate/Advanced

  • Explain the following statement: Java is always pass-by-value.

    When assigning an object to a variable, you are actually assigning the memory address of that object to the variable, therefore allowing calls to the variable to interact with the actual object. So the value being passed is actually the memory location of the object, but to the programmer it looks as if you are passing around references to the object. This results in object aliasing, meaning you can have many variables referring to the same object on the heap.

  • How do you create an immutable type? What are the advantages of immutability?

    Immutability means an object cannot be modified after it has been initialized. This has many benefits for simplifying your objects and your program in general, since immutable objects are always in a valid state.

  • What is a static inner class? Can you provide an example from the Java API?

    A static inner class is a class tightly associated with its enclosing class but not tied to any specific instance. Static inner classes can be instantiated independent of the enclosing class like any top level class. A common example is the Map.Entry class, which provides the standard contract for accessing maps in the Collections api.

  • What are the implications of implementing serializable in an object?

    A serializable object can be converted to a byte stream and stored on disk, allowing such things as session replication or storage in a database. When you implement serializable, you must take into account the fact that changes to the object's members in the future may break compatibility with old versions.

  • Why would you declare a private constructor?

    To control creation of an object, as an object with a private constructor cannot be instantiated outside of the instance. A typical use case in with singletons.

  • Can you explain how Strings are "interned" in Java?

    Strings are created once and stored ("interned") for later use, so String blah = "foo"; and String blah2 = "foo" point to the same object. This can be done because Strings are immutable. The exception is where a new String is explicitly created.

More later as time permits…as always, comments and corrections are greatly appreciated.