Job preparationJava

Java interview questions and answers

11 questions · 7 min read

The four pillars of OOP, inheritance, polymorphism, threading and garbage collection — the compulsory questions for Java roles.

A Java interview nearly always walks the same path: the four OOP ideas first, then inheritance and polymorphism, then memory and threading at the end.

Knowing the path makes preparing easier. The questions below are in that order.

The questions

What is object-oriented programming in Java and what are its four main features?

What to coverEncapsulation, inheritance, polymorphism and abstraction.

What is the difference between a class and an object in Java?

What to coverA class is a blueprint or template; an object is a realisation of that blueprint.

What is the static keyword used for in Java?

What to coverExplain static variables, static methods and static blocks.

What is inheritance in Java and what kinds are there?

What to coverSingle, multi-level and hierarchical inheritance — and how multiple inheritance is achieved in Java, through interfaces.

What is polymorphism in Java and what kinds are there?

What to coverRuntime polymorphism (method overriding) and compile-time polymorphism (method overloading).

What is the difference between an abstract class and an interface in Java?

What to coverAn abstract class is partly implemented; an interface is fully abstract. A class can implement several interfaces but can extend only one abstract class.

What is exception handling in Java and how is it done?

What to coverWith the try, catch and finally blocks and the throw/throws keywords.

How do memory management and garbage collection work in Java?

What to coverHeap memory, stack memory, and why and how garbage collection runs.

What is multithreading in Java and how does it work?

What to coverTwo ways to create a thread: extend the Thread class, or implement the Runnable interface.

What are constructor overloading and constructor chaining in Java?

What to coverHow several constructors can be defined, and how this() and super() chain one constructor to another.

What is the difference between static and instance methods?

What to coverA static method belongs to the class rather than to any object: you call it as ClassName.method() without creating an object, and it can only touch static variables. An instance method belongs to an object, so you need an object before you can call it, and it can reach that object's own data through this. For example Math.max() is static, while list.add() is an instance method.

Tips for this round

  • Give a small example instead of a memorised definition — start with "say you have a Vehicle class" and the answer comes alive.
  • Abstract class versus interface turns up on almost every board. Prepare that one separately.
  • On garbage collection, do not forget to separate the heap from the stack.

Want to learn this hands-on? Take a look at the related course.

Browse courses