Interview Questions for Java Developers

With an estimated 9 million developers using it, Java is one of the most popular programming languages around. In fact, some estimate that Java powers more than 3 billion devices. With so many people claiming expertise, IT managers often ask tricky technical questions to separate the novices from senior developers, says David Bolton, guide of the Dice Java Talent Community. Here are some questions you can expect to hear.

Dice Interview Qs IconWhat is Type Erasure?

  • What Most People Say: “Umm, I’m not sure.”
  • What You Should Say: “Type erasure means the runtime has no knowledge of the types of generic objects.”
  • Why You Should Say It: Most developers aren’t familiar with type erasure, which is an aspect of generics. You can prove that you’re an advanced programmer by demonstrating your understanding of generic objects and how to work around them.

What are the differences and similarities between an abstract class and an interface?

  • What Most People Say: “I’m not familiar with the similarities and differences between abstract classes and interfaces, or when to use each one.”
  • What You Should Say: “As for the differences, interfaces allow a form of multiple inheritance. A class can only extend one other class. And interfaces are limited to public methods and constants with no implementation, while abstract classes can have a partial implementation, protected parts, static methods, etc. Also, a class may implement several interfaces, but a class may extend only one abstract class. Interfaces tend to be slow as there is extra indirection required to find the corresponding method in the actual class, while abstract classes are faster. As for the similarities, neither abstract classes nor interfaces can be instantiated.”
  • Why You Should Say It: Knowing the differences between the two is important because it drives the decision about when to use an abstract class or an interface.

How do you make Java methods virtual?

  • What Most People Say: “I don’t know because I started my programming career in C++.”
  • What You Should Say: “There is no need to make Java methods virtual. They are defined as virtual automatically.”
  • Why You Should Say It: Knowing the answer shows that you’re a more experienced developer.

See interview questions for Android developers here.

What’s the difference between abstraction and encapsulation?

  • What Most People Say: “I always seem to confuse the two.”
  • What You Should Say: “Abstraction hides the implementation details, whereas encapsulation hides the data. Abstraction lets you focus on what the object does instead of how it does it.”
  • Why You Should Say It: Your answer shows that you understand the two concepts and how to apply each one.

What’s the base class for Error and Exception?

  • What Most People Say: “I don’t know.”
  • What You Should Say: “The super class or base class for Exception and Error is Throwable.”
  • Why You Should Say It: It’s the right answer.

What’s difference between throw and throws?

  • What Most People Say: “I don’t understand the difference between throw and throws.”
  • What You Should Say: “Throw is used explicitly to throw an exception, whereas throws is used to declare an exception.”
  • Why You Should Say It: You aren’t any good if you don’t know the difference.

Why is the main() function defined as static?

  • What Most People Say: “I’ve used it. But to be frank, I haven’t thought about why the main() function is defined as static.”
  • What You Should Say: “So it can be called without needing to create an instance.”
  • Why You Should Say It: Most developers have used the function but don’t know why. A more proficient developer understands the purpose. The method is static because otherwise there would be ambiguity about which constructor should be called.

What’s the difference between .equals() and “==”?

  • What Most People Say: “I guess I don’t know this one either.”
  • What You Should Say: “== is used to see if two objects are the same object. For example, to see if it occupies the same area in memory while equals compares values. == is done by comparing references while equals compares the contents. So two different strings with the same value would give true for equals but false for ==.”
  • Why You Should Say It: The correct answer shows that you understand how objects are stored in memory.

Comments

  1. BY Vladimir says:

    In the “abstraction vs. encapsulation” question the entire proposed answer actually belongs to the encapsulation only. The essense of abstraction is to extract essential properties while omitting inessential details. The goal of abstraction is to isolate some entities of the subject area from others based on examination of their important aspects and suppressing unimportant aspects. The result of abstraction is identifing of the separate independent objects in the subject area and their functionality, while encapsulation means hiding the ways how that functionality is going to be internally implemented.

  2. BY Ken says:

    I am not a Java developer, but have read a book on Java coding. That doesn’t get me near the depth of understanding I need to be competent in Java, but I was struck by how similar it is with C#.
    This gives some insight into how different it is. THE base class in C# is always object, it can also be several base classes defined in an inhertance chain. So a Man is a human, a human is a mammal, a mammal is an animal. When you implement an instance of Man the animal characteristic of sex would be Male and not overridable. I would think the mammal would be an abstract class. Classes are inherited one at a time and interfaces can have many inherited at once, but the critical difference between them is an abstract class has inheritable features that may be overridden but interfaces have features that must be impemented.
    If you have two different intances of a custom class and compare them with == they won’t be equal, but you can override the equal() method and == can return true.

  3. BY Kyle Seaton says:

    Thank you very much for this post. I’ve read many books on Java development, but some of the questions posted above, I never knew even existed. Generics weren’t ever really covered in the books I read. I guess I need to buy better written books. Although, it seems that most of these questions are OOP related and not just Java specific. Correct me if I’m wrong.

    P.S. I am most likely going to print this article out so that I can memorize it.

    Thank you,
    Kyle Seaton
    College Student

  4. BY harshit says:

    Thanks for the questions , i also came across Java interview questions . It is a good resource too !!

Post a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>