Home/Blog/Interview Prep/Java interview prep: 15 Java interview questions
Home/Blog/Interview Prep/Java interview prep: 15 Java interview questions

Java interview prep: 15 Java interview questions

Jun 05, 2019 - 11 min read
Cameron Wilson
Java Interview Questions for Interview Prep
Java Interview Questions for Interview Prep

Not all interviews will focus on algorithms and data structures — often times an interview will focus solely on the language or technology that you have claimed to be an expert in. In interviews like these, there usually aren’t any “gotcha” questions, instead they require you to draw on memory and your experience with the language — in other words, they test your knowledge of the programming language.

However, it can be easy to forget all the ins and outs of a language like Java, because simply put, we don’t deal with questions like “What kind of memory does the JVM manage?” and “Describe polymorphism with an example.” on a daily basis.

Today we’ll help you brush up by reviewing interview questions on:

Answer any Java interview problem by learning the patterns behind common questions.

Cover
Grokking Coding Interview Patterns in Java

With thousands of potential questions to account for, preparing for the coding interview can feel like an impossible challenge. Yet with a strategic approach, coding interview prep doesn’t have to take more than a few weeks. Stop drilling endless sets of practice problems, and prepare more efficiently by learning coding interview patterns. This course teaches you the underlying patterns behind common coding interview questions. By learning these essential patterns, you will be able to unpack and answer any problem the right way — just by assessing the problem statement. This approach was created by FAANG hiring managers to help you prepare for the typical rounds of interviews at major tech companies like Apple, Google, Meta, Microsoft, and Amazon. Before long, you will have the skills you need to unlock even the most challenging questions, grok the coding interview, and level up your career with confidence. This course is also available in JavaScript, Python, Go, and C++ — with more coming soon!

85hrs
Intermediate
214 Challenges
215 Quizzes

Q1: What makes Java platform independent?

Java works on the principle of write once and run anywhere. Once a Java program is written, it is compiled into what is known as byte code, which can then be run on any Java Virtual Machine or JVM for short.

widget

Compilation to bytecode is the magic behind Java’s interoperability. Different operating systems and hardware architectures have JVMs custom designed for themselves and all JVMs can run the same bytecode. Therefore, if you write a Java program on Linux, it will run seamlessly on a JVM designed for Windows operating system, making code agnostic to the underlying hardware and OS.


Q2: Explain the concepts of JRE, JDK, and JVM

  • JRE (Java Runtime Environment) includes the Java Virtual Machine and the standard Java APIs (core classes and supporting files.). The JRE contains just enough to execute a Java application, but not enough to compile it.

  • JDK (Java Development Kit) is the JRE plus the Java compiler, and a set of other tools to compile and debug code. JRE consists of Java platform libraries, Java Virtual Machine (JVM), Java Plugin and Java Web Start to run Java applications. JRE as a stand-alone does not contain compilers and debugging tools. If you need to develop Java programs you need the full Java SDK. The JRE is not enough for program development. Only the full Java SDK contains the Java compiler which turns your .java source files into bytecode .class files.

  • JVM (Java Virtual Machine) is an implementation of a specification, detailing the behavior expected of a JVM. Any implementation that conforms to the JVM specification should be able to run code compiled into Java bytecode irrespective of the language in which the code was originally written. In the Java programming language, all source code is first written in plain text files ending with the .java extension. Those source files are then compiled into .class files by the javac compiler. A .class file does not contain code that is native to your processor; it instead contains bytecodes — the machine language of the Java Virtual Machine. The java launcher tool then runs your application with an instance of the Java Virtual Machine.


Q3: How would you mark an entity package private in Java?

There’s no explicit modifier for package private. In the absence of any modifier the class or member variables are package private. A member marked package private is only visible within its own package. Consider the class below.

widget

Package private is a slightly wider form of private. One nice thing about package-private is that you can use it to give access to methods you would otherwise consider private to unit test classes.

So, if you use helper classes which have no other use but to help your public classes do something clients need, it makes sense to make them package private as you want to keep things as simple as possible for users of the library.


Q4: Why should you avoid the finalize() method in the Object class? What are some alternatives?

The Object class provides a callback method, finalize(), that may be invoked on an object when it becomes garbage. Object’s implementation of finalize() does nothing — you can override finalize() to do cleanup, such as freeing up resources.

The finalize() method may be called automatically by the system, but when it is called, or even if it is called, is uncertain. Therefore, you should not rely on this method to do your cleanup for you. For example, if you don’t close file descriptors in your code after performing I/O and you expect finalize() to close them for you, you may run out of file descriptors.

Here are some alternatives:

  • The try-with-resources idiom can be used to clean up objects. This requires implementing the AutoCloseable interface.
  • Using a PhantomReference to perform cleanup when an object is garbage collected
  • Using Cleaner class to perform cleanup actions.
  • Implement a close() method, which does the cleanup and document that the method be called.

Q5: Can you change the contents of a final array as shown in the code snippet below?

final int[] array = new int[5];
array[0] = 1;

It may appear counterintuitive, but we can actually change the contents of the array even though it is marked as final. The array variable points to a particular start location in the memory where the contents of the array are placed.

The location or the memory address can’t be changed. For example, the following code will not compile:

final int[] array = new int [5]
array = new int[10];

However, the following code will work.

public class FinalArrayExample {
final int[] array = new int[5];
// allowed
void changeArrayContents(int i, int val) {
array[i] = val;
}
// not allowed and will not compile
/*
void changeArray() {
array = new int [10]
}*/
}

Ace your Java coding interview with real-world examples

Cover
Decode the Coding Interview in Java: Real-World Examples

Preparing for coding interviews has become a daunting task. Trying to solve enough practice questions and remember solutions can feel impossible. Fortunately, the best way to be successful in the coding interview is not to just grind through problems. The best way is to develop the skills to break down a new problem and deploy the right tools to come up with a solution. That’s why in this course, you’ll prepare for coding interviews by tackling real world problems faced by tech companies. When you solve real problems related to real projects (for example, paginating attendees in a Zoom call), not only will you have more fun preparing, but you’ll also have an easier time remembering what you’ve learned. After each project, we’ll also show you what kinds of interview problems you’ll now be able to solve using the techniques you just applied, so that your knowledge is adaptable to new problems. (This course is also available in Rust, C++, C#, Go, Python, Ruby, Elixir, Scala, Swift, Kotlin, and JavaScript.)

18hrs
Beginner
195 Challenges
1418 Illustrations

Q6: Explain the difference between an interface and an abstract class? When should you use one or the other?

An abstract class can’t be instantiated, but it can be subclassed. An abstract class usually contains abstract and non-abstract methods that subclasses are forced to provide an implementation for.

An interface is a completely “abstract class” that is used to group related methods with empty bodies.

Each allow for advanced OOP constructions in Java.

Following are four main differences between abstract classes and interfaces:

  • An abstract class can have final variables, static variables, or class member variables whereas an interface can only have variables that are final and static by default.

  • An abstract class can have static, abstract, or non-abstract methods. An interface can have static, abstract, or default methods.

  • Members of an abstract class can have varying visibility of private, protected, or public. Whereas, in an interface all methods and constants are public.

  • A class can only extend another class, but it can implement multiple interfaces. Similarly, an interface can extend multiple interfaces. An interface never implements a class or an interface.

Use an abstract class when subclasses share state or use common functionality. Or you require to declare non-static, non-final fields or need access modifiers other than public.

Use an interface if you expect unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes. Interfaces are also used in instances where multiple inheritance of type is desired.


Q7: What is polymorphism? Can you give an example?

Polymorphism is the ability in object-oriented programming to present the same interface for differing underlying forms or data types. Polymorphism is when you can treat an object as a generic version of something, but when you access it, the code determines which exact type it is and calls the associated code. What this means is that polymorphism allows your code to work with different classes without needing to know which class it’s using.

Polymorphism is used to make applications more modular and extensible. Instead of messy conditional statements describing different courses of action, you create interchangeable objects that you select based on your needs. That is the basic goal of polymorphism.

The classic example of polymorphism is a Shape class. We derive Circle, Triangle, and Rectangle classes from the parent class Shape, which exposes an abstract method draw(). The derived classes provide their custom implementations for the draw() method. Now it is very easy to render the different types of shapes all contained within the same array by calling the draw() method on each object. This saves us from creating separate draw methods for each shape e.g. drawTriangle(), drawCircle() etc.

widget

Q8: Can the main method be overloaded?

Yes, the main method, which is a static method, can be overloaded. But only public static void main(String[] args) will be used when your class is launched by the JVM even if you specify one or two command-line arguments. However, programmatically one can invoke the overloaded versions of the main method.


Q9: How can you pass multiple arguments to a method on each invocation call?

We can pass variable number of arguments to a method using varargs feature. Below is an example of passing multiple arguments of the same type to a method.

public void childrenNames(string... names) {
for(int i= 0; i < names.length; i++)
system.out.println(names[i]);
}
  • The type name is followed by three dots, a space, and then the variable name.
  • The varargs variable is treated like an array.
  • The varargs variable must appear at the last in the method signature.
  • As a consequence of the above, there can only be a single varargs in a method signature.

The above method can be invoked as follows:

Invoking Varargs Method

childrenNames();
childrenNames("Jane");
childrenNames("Jane", "Tom", "Peter");

Answer any Java interview problem by learning the patterns behind common questions.

Cover
Grokking Coding Interview Patterns in Java

With thousands of potential questions to account for, preparing for the coding interview can feel like an impossible challenge. Yet with a strategic approach, coding interview prep doesn’t have to take more than a few weeks. Stop drilling endless sets of practice problems, and prepare more efficiently by learning coding interview patterns. This course teaches you the underlying patterns behind common coding interview questions. By learning these essential patterns, you will be able to unpack and answer any problem the right way — just by assessing the problem statement. This approach was created by FAANG hiring managers to help you prepare for the typical rounds of interviews at major tech companies like Apple, Google, Meta, Microsoft, and Amazon. Before long, you will have the skills you need to unlock even the most challenging questions, grok the coding interview, and level up your career with confidence. This course is also available in JavaScript, Python, Go, and C++ — with more coming soon!

85hrs
Intermediate
214 Challenges
215 Quizzes

Q10: Can a semaphore act as a mutex?

A semaphore can potentially act as a mutex if the number of permits it can give out is set to 1. However, the most important difference between the two is that in the case of a mutex, the same thread must call acquire and subsequent release on the mutex whereas in the case of a binary semaphore, different threads can call acquire and release on the semaphore.

This leads us to the concept of “ownership”. A mutex is owned by the thread acquiring it, till the point, it releases it, whereas for a semaphore there’s no notion of ownership.


Q11: Explain the Externalizable interface

The Serializable interface gets us automatic serialization capability for objects of our class. On the other hand the Externalizable interface provides a way to implement a custom serialization mechanism. A class that implements the Externalizable interface is responsible to save and restore the contents of its own instances.

The Externalizable interface extends the Serializable interface and provides two methods to serialize and deserialize an object, writeExternal() and readExternal().


Q12: If a code block throws more than one exception, how can it be handled?

Multiple types of exceptions thrown by a snippet of code can be handled by multiple catch block clauses followed by the try block. An example snippet of exception handling appears below:

void process(int val) {
try {
if (val == 1)
//checked exception
throw new FileNotFoundException();
if (val == 2)
// runtime exception
throw new NullPointerExxception();
if (val == 3)
// error exception
throw new StackOverflowError
} catch (RuntimeException re) {
// catches all unchecked exceptions
} catch (Exception e) {
// catches all checked exceptions
} catch (Error err) {
// catches all errors
}
}

Q13: If you were to use a set, how would you determine between a HashSet and a TreeSet?

Initially, you may want to use a HashSet data structure as it will give you a better time complexity, but it makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.

So if you are wanting to maintain the order it’s best to use a TreeSet as it stores keys in ascending order rather than in their insertion order. It’s not thread-safe. However, keep in mind that TreeSet is not thread-safe whereas a HashSet is.


Q14: What are a few ways you can improve the memory footprint of a Java application?

Here are three key steps you can take to improve the memory footprint:

  • Limiting the scope of local variables. Each time the top scope from the stack is popped up, the references from that scope are lost, and this could make objects eligible for garbage collection.
  • Explicitly set variable references to null when not needed. This will make objects eligible for garbage collection.
  • Avoid finalizers. They slow down program performance and do not guarantee anything.

Q15: What is the best way to implement a singleton class?

The best way to implement a singleton as per Josh Bloch is to use an enum type for the singleton. Because Java ensures that only a single instance of an enum is ever created, the singleton class implemented via enums is safe from reflection and serialization attacks.

class Demonstration {
public static void main( String args[] ) {
Superman superman = Superman.INSTANCE;
superman.fly();
}
}
enum Superman {
INSTANCE;
private final String name = "Clark Kent";
private String residence = "USA";
public void fly() {
System.out.println("I am flyyyyinggggg ...");
}
}

What to learn next

There has been a lot covered in this post about the Java programming language, ranging from the Java ecosystem to multi-threading and exceptions. These are the types of Java interview questions you can expect.

Test your knowledge now

Cover
The Java Interview Handbook: 300+ Interview Questions

It’s very common to have interviews testing your knowledge of a programming language you claim to be an expert in. If you've got an interview coming up on Java soon and need to get up to speed, this is the ideal course for you. You'll refresh your memory on everything from the basics to more advanced functionality you’re likely to be asked about. Even if you're using Java every day, you may not have been exposed to parts of it in some time, so it's always useful to make sure you're updated. This course features more than 300 of the most commonly asked core Java interview questions that you're likely to face as an experienced software engineer. You'll get to explore questions by topic, as well as see detailed answers for each one, and plenty of live code examples where relevant. Get started today.

15hrs
Intermediate
147 Playgrounds
55 Quizzes

However, the material here just scratches the surface. There are many more concepts to revisit or explore.

If you’d like to take a deep dive into Java and explore hundreds of more questions about the topics mentioned above then Grokking Coding Interview Patterns in Java is going to be a great resource to get you refreshed on the core java principles — everything from the basics to more advanced functionality.

Happy learning!


Continue reading about Java


WRITTEN BYCameron Wilson