Monday, November 4, 2013

How To Answer 10 Common JAVA Questions

1. What does the static keyword mean, and where can it be used?

static can be used in four ways:

    static variables are shared by the entire class, not a specific instance (unlike normal member variables)
    static methods are also shared by the entire class
    static classes are inner classes that aren’t tied to their enclosing classes
    static can be used around a block of code in a class to specify code that runs when the virtual machine is first started up, before instances of the class are created.

2. How do you deal with dependency issues?

This question is purposely ambiguous. It can refer to solving the dependency injection problem (Guice is a standard tool to help). It can also refer to project dependencies — using external, third-party libraries. Tools like Maven and Gradle help manage them. You should consider learning more about Maven as a way to prepare for this question.
3. You want to create a simple class that just has three member variables. Tell me how you’d do this.

This problem seems simple at first, and creating such a simple class is covered in classes like Programming Java for Beginners.

But an experienced programmer will recognize that it’s necessary to know how to correctly override the hashCode() and equals() methods (using, for example, EqualsBuilder and HashCodeBuilder, in the Apache Commons library).
4. What does synchronized do? Tell me how to use it to set a variable just one without any race conditions?

synchronized says that a method has to hold the object’s lock to execute. If used around a block, like synchronized (obj) { ... }, it will grab the lock of obj before executing that block. Classes like Programming Java for Beginners and Java Fundamentals I and II will provide a refresher.
5. What is type erasure?

Type erasure is a JVM phenomenon that means that the runtime has no knowledge of the types of generic objects, like List<Integer> (the runtime sees all List objects as having the same type, List<Object>). The topic of type erasure is covered in Advanced Java Programming.
6. When and why are getters and setters important?

While an advanced Java class covers the topic, the key factor to know for interviews is that setters and getters can be put in interfaces and can hide implementation details, so that you don’t have to make member variables public (which makes your class dangerously brittle).
7. What are the differences between Map, Hashtable, HashMap, TreeMap, ConcurrentHashMap, LinkedHashMap?

    Map is an interface for a key-value map
    HashMap is a Map that uses a hash table for its implementation
    Hashtable is a synchronized version of HashMap
    TreeMap uses a tree to implement a map
    ConcurrentHashMap allows for multiple threads to access it at the same time safely
    LinkedHashMap preserves the iteration order that things were inserted in (others don’t provide a fixed iteration order)

A deeper discussion of the differences can be found in Advanced Java Programming.
8. What are the differences between interfaces, abstract classes, classes, and instances?

    Interfaces are essentially a list of methods that implementations must possess, but have no code or member variables
    Abstract classes cannot be instantiated, but can contain variables, implemented methods, and unimplemented methods
    Classes contain variables and implemented methods only, and can be instantiated
    Instances (or objects) are specific examples of a particular class.

9. If you needed to provide a GUI for your Java program, how would you go about it?

There are a lot of options, from web apps to local applications. Usually, interviewers mean Swing or other GUI toolkits with a question like this. It may be worth going through a course on Java Swing Programming before an interview.
10. How do you test your code?

You should talk about your experience using libraries like JUnit, Mockito, and Selenium. Even if you don’t have extensive knowledge about testing, being able to talk about the libraries is a good first step.

Test-Driven-Development (TDD) is very popular these days, and any experience here would also be good to talk about. There are courses on Test Driven Development in Java which can bring you up to speed.

1 comment:

  1. Thanks for sharing these questions. Would be very helpful in cracking JAVA interviews. I am undergoing Core Java Online Training at All their trainers are industry professionals having live project experience, highly qualified with in-depth experience in JAVA.