Panasonic Youth rob sanheim writes about software, business, ruby, music, stuff and things



Posted
31 August 2005 @ 8am

Tagged
Java, Jobs

Discuss

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.


14 Comments

[...] update 8/31/05: I have written part two with some possible answers. Also, renamed the post. [...]


Posted by
Tom Klaasen
1 September 2005 @ 4am

3 observations:
(1) I like your disclaimer - studying interview questions is not a good idea to prepare for an interview
(2) I’m a bit surprised by your “Advanced” questions. This is all stuff I learned at college. I might appreciate the consequences a bit more now, but the technical explanation hasn’t really changed in my mind.
(3) You might add some more questions to probe for the interests of your interviewee - has she ever heard of [Ruby|J2ME|whatever people are talking about these days]? Did she have a look at something? What does she think of it (with the fact that she has an opinion being far more interesting than what her opinion is).


Posted by
Jim Halberg
2 September 2005 @ 12pm

I like the advanced questions. Tom is very right that you could (and minimally should!) memorize this stuff. The reason I like them though is that getting the answer “right” should be 50% of what you’re looking for. The ensuing 2 minute conversation should let you know if they actually know what it means in addition to what it says in the book. and you can use that 2 minutes to lead into questions and concepts that truly are more advanced.

I also like the second comment from Tom. Add to that: Do you attend JUG meetings? or a question that I’ve heard before (from Mr. Rob Sanheim actually): what is your favorite technical book?


Posted by
Rob
2 September 2005 @ 11pm

Tom:
Thanks for the comments. I agree that the advanced questions could better be renamed intermediate. Like Jim said, the main thing you are looking for is that the candidate can talk about the significance of the topic or give real world examples - or if she just sounds like she is reading direct from a Google search w/o having understanding.

I had some questions along the lines of #3 in the first part, but that could certainly be expaned upon.


Posted by
Tom Roche
6 September 2005 @ 9pm

> 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.

IMHO you answer your second question, but not your first.


Posted by
Mark Ash
7 September 2005 @ 10am

Good questions. However, here’s a small correction:

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

* Actually the protected access modifier has a further caveat such that subclasses outside the base class package may not access a protected method on an object of the base class type - but this is rarely an issue.

Interestingly, what you define in your original answer is what (I believe) the vast majority of programmers _wish_ the access modifiers did! It is a source of endless annoyance that ‘protected’ is actually a more generous level of access than package level. IOW: protected = package + subclasses; whereas better would have been: protected = subclasses only, and package = classes in same package + subclasses.
Ultimately the problem is that the vast majority of developers use Java packages to organise their classes in logical groups (e.g. exceptions, gui, model, etc.) rather than groups of classes that are tightly coupled. Once you do that (which is more intuitive) then the package and protected access modifiers seem incorrect.
Now a discussion of that would form the basis of an advanced Java question!


Posted by
Thamme gowda
8 September 2005 @ 11pm

Hi Iam very happy by seeing this responses. But i have still doubt in passby value can u give an example. Iam Thamme gowda working as corporate trainee iam teaching java and c++.
my contact no:9886127835

Thanks a lot


Posted by
D.satyendra Kumar
2 February 2006 @ 3am

what is concrete interface?an interfaces cannot have any concrete method why? where abstract class may have concrete method


Posted by
kash
30 June 2006 @ 12am

Hi,

An excellent read. I was asked to conduct java technical interviews and your breadth of questions covering all the major topics is invaluable. Now i have various subjects to discuss in detail with my candidates.

I particularly liked your final section for general questions like the Monopoly etc. They started me off thinking very seriously ….

I would greatly appreciate if you can point me to more resources.

Thanks Again


Posted by
Lee
31 March 2007 @ 10pm

More java interview questions can be found at http://www.technical-interview.com


Posted by
venkata subbaiah
17 July 2007 @ 7am

1>what is the difference between core java,advance java and j2ee?


Posted by
pavan singh
30 July 2007 @ 9am

hi
its good to prepare for a interview br these type of question specially when you are doing self study.


Posted by
Dana Hata
21 March 2008 @ 5pm

out of memory error is not an exception, it’s an error (java.lang.Error). exceptions and errors are considered different beasts in java


Posted by
Kulandai
21 May 2008 @ 3am

Similar to these, more quality collection of Java Interview Questions can be found at http://javapapers.com


Leave a Comment

Behold! A link-post! mildy amusing: Java Rehab Clinic T-Shirt