TestGorilla LogoTestGorilla Logo
Pricing
homeblogsHiring & recruiting
100 common Java coding interview questions

100 common Java coding interview questions

Share

If you’re trying to hire a top Java developer for your team, you need to assess several skills.

Your next new hire should have the right soft skills, technical skills, and values that align with your company culture.

Skills testing and interviewing candidates are two easy ways to evaluate your applicants’ skills. But what if you don’t have the right interview questions yet?

There’s no need to panic – below, you’ll find 100 common Java coding interview questions in this article to assess your applicants’ Java coding skills and knowledge.

Take a look at the list below to build a set of questions.

15 common Java coding interview questions

Determine whether applicants are the right fit for your open role by asking them these 15 common Java interview questions.

15 common Java coding interview questions
  1. Which skills do developers need to code in Java?

  2. Which soft skills do developers need to code in Java?

  3. What are your strongest Java coding skills?

  4. Which Java coding skills do you need to improve?

  5. How do you keep up to date with best coding practices in Java?

  6. Describe your experience with Scrum.

  7. Describe your Java coding experience.

  8. Describe your largest project that involved coding in Java.

  9. What are your favorite Java features?

  10. What are your least favorite Java features?

  11. Explain some advantages of using Java for coding.

  12. Explain what constructors are.

  13. Explain what a class is in Java.

  14. Explain what static loading is in Java.

  15. Explain what dynamic loading is in Java.

5 common Java coding interview questions and answers

Here are sample answers to five of the general Java interview questions to guide you when evaluating interviewees’ responses.

1. Which skills do developers need to code in Java?

Applicants should know that three critical skills they need to code in Java include data-structure knowledge, algorithms-in-Java knowledge, and knowledge of APIs and libraries.

2. Which soft skills do developers need to code in Java?

Do your applicants know that communication, critical thinking, and problem-solving skills are vital soft skills for developers? Can they provide examples of why these soft skills are important?

3. What are your favorite Java features?

Some of the Java features that applicants may mention when responding to this interview question include:

  • Java’s Secure feature to guarantee a virus-free system

  • The fact that Java is an object-oriented programming language

  • The fact that Java is an independent platform, and its virtual machine interprets its code

4. Explain what static loading is in Java.

Your applicants should understand that static loading is an object-and-instance-creation process developers carry out using new keywords.

5. Explain what dynamic loading is in Java.

Your candidates should be able to explain that developers can complete dynamic class loading even when the class’ name is unknown at the time of compilation.

15 common Java coding interview questions about strings

Test your interviewees’ string-related knowledge in Java with the interview questions in this section.

  1. How would you reverse strings in Java?

  2. Explain what an anagram string is.

  3. How would you write a program to check whether two strings are anagrams?

  4. How would you determine whether a string’s characters are all unique?

  5. Which method would you use to learn if a string is a rotation of another string?

  6. Explain how you would find a duplicate character in a string.

  7. Explain how you would find a string’s first non-repeated character.

  8. How would you find all of the substrings of a string?

  9. How would you find out the length of a string in Java?

  10. How would you print all string permutations in Java?

  11. How would you find out if there is a vowel in a string?

  12. How would you determine if a number is a prime number in a string?

  13. Explain what trim() does.

  14. Explain what strip() does.

  15. Explain what whitespaces are in strings.

5 common Java coding interview questions and answers about strings

Here are the answers to five of the above questions about strings. Use them to evaluate your developers’ responses.

1. How would you reverse strings in Java?

Although a reverse() utility method doesn’t exist in the string class, methods your candidates may mention when answering this interview question include:

  • Using a stringBuffer method

  • Using a recursion method

  • Using a for loop method

2. How would you determine whether a string’s characters are all unique?

Two methods your applicants may outline when explaining how to determine if a string’s characters are all unique are:

  • Using an indexOf and lastIndexOf method

  • Using a HashSet method

3. Which method would you use to learn if a string is a rotation of another string?

Applicants should mention a few steps when responding to this interview question. They may first label the two strings involved in the process (i.e., str5 and str6) and then outline the following processes:

  • Make a new string: str7 = str5 + str5

  • Check whether str7 contains str6

  • If str7 does contain str6, str6 is a rotation of str5

  • If str 7 does not contain str 6, str6 is not a rotation of str5

4. Explain what whitespaces are in strings.

Can your candidates explain that whitespaces are characters that represent spaces in strings? They may also explain that whitespace characters can impact a developer’s source-code size.

5. Explain how you would find a string’s first non-repeated character.

Interviewees may explain that developers can find a string’s first non-repeated character using two methods: 

  • The linkedHashMap method

  • The indexOf and lastIndexOf method

15 common Java coding interview questions about arrays

Ask your interviewees some of these 15 Java interview questions about arrays to learn whether their skills align with the requirements of your open role.

15 common Java coding interview questions about arrays
  1. Explain what an array is in Java.

  2. How would you find the smallest element in an array?

  3. How would you find the largest element in an array?

  4. How would you find a missing number in an array?

  5. How would you find the minimum element in sorted, rotated arrays?

  6. How would you sort an array in Java?

  7. How would you write a program to determine the sum of elements in integer arrays?

  8. Which method would you use to shuffle arrays in Java?

  9. Explain how arrays are different from ArrayList in Java.

  10. How would you separate 0s and 1s in an array?

  11. How would you separate even and odd numbers in an array?

  12. Explain what a peak element is in an array.

  13. Explain what a leader element is in an array.

  14. Explain what a permutation is in an array.

  15. Explain how you would find local minima in an array in Java.

5 common Java coding interview questions and answers about arrays

Here’s a selection of five of the above Java coding interview questions about arrays, along with sample answers to gauge the depth of your applicants’ knowledge. 

1. Explain what an array is in Java.

Can your applicants explain that arrays are container objects with a certain number of values of a specific type? Do they know that developers can determine the length of an array when they create one, because the length doesn’t change afterwards?

2. How would you sort an array in Java?

Applicants should know that if a developer wants to sort a primitive array, they can implement the arrays.sort() method:

int[] array = { 1, 2, 4, -1, -2, 4 };

Arrays.sort(array);

System.out.println(Arrays.toString(array));

They may explain that when sorting an array of objects, developers must implement a comparable interface.

3. How would you write a program to determine the sum of elements in integer arrays?

Your candidates may provide the following example of a program to work out the sum of elements in integer arrays:

int[] array = { 9, 8, 7, 6, 5 };

int sum = 0;

for (int i : array)

sum += i;

System.out.println(sum);

4. Explain what a permutation of an array is.

Can your applicants explain that a permutation of an array is the rearrangement of sequence members to produce a new sequence? They may give an example to explain in more detail, by using an example similar to the one below:

  • The total number of new sequence permutations of [w, x, y, z] is 24, some of which are: [x, w, z, y], [z, w, x, y], and [w, z, x, y]

5. Explain what a leader element is in an array.

Applicants should know that an element is a leader if the elements on its right side are all lower than it, and in an array, the element on the far-right side is always a leader. 

10 common Java coding interview questions about stacks

Determine whether your candidates’ knowledge of stacks in Java stacks up to your expectations by asking them some of these 10 interview questions.

  1. How would you implement stacks with arrays?

  2. How would you implement linked lists with stacks?

  3. How would you implement stacks with two queues?

  4. Explain what LIFO means in a stack.

  5. Explain what a stack is in Java.

  6. What are the advantages of stacks in Java?

  7. How would you use stacks to determine if parentheses are balanced in Java?

  8. How would you use recursion to sort a stack?

  9. How would you locate an expression’s duplicate parentheses?

  10. Stacks are referred to as recursive data structures. Explain why.

5 common Java coding interview questions and answers about stacks

Here are the answers to five of the Java coding interview questions about stacks. Use them to assess your applicants’ knowledge. 

1. Explain what LIFO means in a stack.

Your applicants may explain that a stack always uses the LIFO principle, which means “Last In First Out,” when developers insert or remove the objects within the container.

2. Explain what a stack is in Java.

Java developers should know that a stack is a data structure that contains objects that programmers insert and remove. They may explain that developers can either insert, delete, or display stack items (known as a push, pop, and peek, respectively).

3. What are the advantages of stacks in Java?

Stacks in Java: 

  • Are easy to learn and implement

  • Don’t come with high hardware demands

  • Enable developers to solve recursion problems easily

  • Help control the allocation and deallocation of memory

  • Enable developers to implement efficient data-management processes

4. How would you implement linked lists with stacks?

Your applicants should know that developers can use two stacks to simulate linked lists. They may explain that the first stack is called the “list” and that developers use the second for temporary storage. Developers can either add, remove, or insert items from the stack after they have completed the following steps:

  • Include the header files that the program uses

  • Declare the user-defined functions

  • Define node structures

  • Define node pointer and set the pointer to NULL

  • Implement main methods by displaying the menu operations list

5. Stacks are referred to as recursive data structures. Explain why.

Your candidates may explain that stacks are recursive data structures because developers use them to implement recursion, and recursion utilizes stack space. They may also mention that recursion refers to a step in which a function fetches data from itself.

15 common Java coding interview questions about linked lists

How well do your candidates understand linked lists in Java? Ask them some of these 15 common Java coding interview questions below to find out.

  1. Explain what a linked list is in Java.

  2. How would you reverse a linked list?

  3. How would you find a linked list’s middle element?

  4. How would you find a linked list’s nth element from its end?

  5. How would you detect loops in linked lists?

  6. How would you find out whether a linked list is a palindrome?

  7. Explain what a loop is in a linked list.

  8. Explain what a single-linked list is.

  9. Explain what a doubly-linked list is.

  10. How would you find a linked list’s start node?

  11. How would you determine if a list of integers only has odd numbers?

  12. What is the merge sort algorithm?

  13. Which method would you use to merge two lists?

  14. How would you implement a doubly-linked list?

  15. How would you find two linked lists’ intersections?

5 common Java coding interview questions and answers about linked lists

Here are the answers to five of the questions about linked lists. Use them to assess your applicants’ responses and hire a skilled software developer.

1. Explain what a linked list is in Java.

A linked list is a set of data elements that are ordered and feature a link to successors and, on occasion, predecessors. Your applicants may explain that singly- and doubly-linked lists are two examples of linked lists in Java.

2. Explain what a loop is in a linked list.

If a linked list doesn’t have an end, developers label this a loop. Your candidates may mention that developers can use hashing to remove loops from linked lists, which works through traversing linked lists and entering found nodes into a hash set. If developers find that the node is already in the hash set, this means that it has already been seen.

3. Explain what a single-linked list is.

Expert developers should know that single-linked lists are unidirectional. They may explain that developers can traverse this list type only in one direction, from the head to the tail.

4. Explain what a doubly-linked list is.

Candidates familiar with linked lists should know that developers can navigate doubly-linked lists in both directions, i.e. backward and forward.

5. How would you reverse a linked list?

Can your applicants explain that a programmer may write a recursive solution specifically for the purposes of linked list reversal? Applicants may mention that using decendingIterator() will return an iterator that developers can use to create a different linked list with reverse ordered elements.

15 common Java coding interview questions about binary trees

Learn whether your applicants have comprehensive Java binary-tree knowledge by asking them some of the interview questions below.

15 common Java coding interview questions about binary trees
  1. What is a binary tree?

  2. Explain what binary tree traversal means.

  3. What is pre-order binary tree traversal?

  4. What is in-order binary tree traversal?

  5. What is post-order binary tree traversal?

  6. What is binary tree spiral level order traversal?

  7. How would you print a binary tree’s leaf nodes?

  8. How would you count a binary tree’s leaf nodes?

  9. How would you print every path from the root to the leaf in a binary tree?

  10. How would you find a node’s level in a binary tree?

  11. Which method would you use to find a binary tree’s maximum element?

  12. Which method would you use to find a binary tree’s lowest common ancestor?

  13. Explain what a lowest common ancestor is.

  14. Which method would you use to do binary tree boundary traversal?

  15. How would you print a binary tree’s vertical sum?

5 common Java coding interview questions and answers about binary trees

Below, you’ll find the answers to five of the common Java coding interview questions about binary trees to help you assess your interviewees’ responses.

1. Explain what binary tree traversal means.

Skilled developers should know that the traversal of binary trees involves visiting each tree node systematically and only once. They may explain that programmers traverse binary trees to display every element or perform an operation on every element.

2. Explain what a lowest common ancestor is.

Applicants should know that the root of two nodes on a binary tree is called the lowest common ancestor. They may also explain that there are paths between two nodes and the root node on a binary tree.

3. What is pre-order binary tree traversal?

Can your applicants explain that developers process the nodes before the sub-trees in pre-order binary tree traversal? In other words, developers visit nodes before the nodes’ children during traversal.

4. What is in-order binary tree traversal?

Can your candidates explain that in in-order binary tree traversal, developers process nodes using the Left Root Right method? Do they know that developers first traverse the left subtree before focusing on the root node, and finally the right subtree in in-order tree traversal?

5. What is post-order binary tree traversal?

Applicants should know that in post-order binary tree traversal, developers process nodes by visiting the left subtree, the right subtree, and then the node.

15 common Java coding interview questions about sorting

Ask your interviewees some of these 15 interview questions about sorting in Java to assess their knowledge in depth.

  1. How would you implement a bubble sort algorithm?

  2. What does bubble sort mean in Java?

  3. How would you implement an insertion sort algorithm?

  4. What does insertion sort mean in Java?

  5. How would you implement a selection sort algorithm?

  6. What does selection sort mean in Java?

  7. Which method would you use to implement heap sort in Java?

  8. What does heap sort mean in Java?

  9. Which method would you use to implement quick sort in Java?

  10. What does quick sort mean?

  11. Which method would you use to implement shell sort?

  12. What does shell sort mean?

  13. Which method would you use to implement counting sort?

  14. What does counting sort mean?

  15. Which method would you use to locate an element in a sorted array?

5 common Java coding interview questions and answers about sorting

Here are five common Java coding interview questions and answers related to sorting in Java. Check out the answers to evaluate your applicants’ responses.

1. What does bubble sort mean in Java?

Can your applicants explain that a bubble sort is a type of algorithm used to compare an array’s adjacent elements? Do they know that a bubble sort will swap the elements when the right-hand side element is smaller than the one on the left-hand side?

2. What does insertion sort mean in Java?

Your interviewees should know that an insertion sort is an algorithm used to sort an array’s elements one at a time. They may explain that insertion sort works using an in-place sorting method, where developers modify the original array and do not require temporary structures.

3. What does selection sort mean in Java?

Applicants should be able to explain that a selection sort involves sorting elements in an array by switching the smallest-sized element with the first element and then the second smallest element with the second element.

4, What does heap sort mean in Java?

Interviewees should know that heap sorting is a sorting method that developers use. It involves finding the minimum element and placing it at the beginning. The process removes the elements at the list’s “heap” part and moves them to the list’s “sorted” part.

5. What does counting sort mean?

Applicants should know that a counting sort is a type of algorithm developers use to sort elements in an array. The algorithm works by counting the unique elements’ occurrences in the array.

Which other methods can you use to assess Java skills?

Before proceeding to interviews where you can ask candidates some of the Java interview questions above, we advise you to assess their Java skills with the help of a skills assessment (a combination of skills tests). 

A pre-employment skills assessment will help you assess applicants’ soft skills, technical skills, and values all at once.

Each skills test is graded automatically and at the end you get objective and reliable information on each of your candidates’ skills. You can then compare them easily and invite the most skilled developers to an interview to further assess their knowledge.

It’s important to use skills assessments early on in the hiring process, before you invite applicants to an interview, because this helps you make a selection and only interview your best applicants.

Recommended reading: 29 essential hard and soft skills for Java developers

Hire top talent using the right Java coding interview questions

There are many skills you need to evaluate to hire top talent for a Java developer role, but don’t panic.

All you need is a top skills assessment platform like TestGorilla to build your skills assessment and the Java coding interview questions in this article.

Hire exceptional talent using the right Java interview questions and go to TestGorilla’s test library to find the most reliable, expert-crafted skills tests. Hiring doesn’t have to be difficult!

Get started for free today and start making better hiring decisions, faster and bias-free.

Share

You've scrolled this far

Why not try TestGorilla for free, and see what happens when you put skills first.

The best insights on HR and recruitment, delivered to your inbox.

Biweekly updates. No spam. Unsubscribe any time.

TestGorilla Logo

Skills tests to hire the best

Our screening tests identify the best candidates and make your hiring decisions faster, easier, and bias-free.

Free resources

Skills-based hiring handbook cover image
Ebook
The skills-based hiring handbook

This handbook provides actionable insights, use cases, data, and tools to help you implement skills-based hiring for optimal success

Ebook
How to elevate employee onboarding

A comprehensive guide packed with detailed strategies, timelines, and best practices — to help you build a seamless onboarding plan.

Top talent assessment platforms comparison guide - carousel image
Ebook
Top talent assessment platforms: A detailed guide

A comprehensive guide with in-depth comparisons, key features, and pricing details to help you choose the best talent assessment platform.

The blueprint for boosting your recruitment ROI cover image
Ebook
The blueprint for boosting your recruitment ROI

This in-depth guide includes tools, metrics, and a step-by-step plan for tracking and boosting your recruitment ROI.

Skills-based hiring checklist cover image
Checklist
The skills-based hiring checklist

A step-by-step blueprint that will help you maximize the benefits of skills-based hiring from faster time-to-hire to improved employee retention.

Onboarding email templates cover image
Checklist
Essential onboarding email templates

With our onboarding email templates, you'll reduce first-day jitters, boost confidence, and create a seamless experience for your new hires.

HR cheat sheet cover image
Checklist
The HR cheat sheet

Get all the essentials of HR in one place! This cheat sheet covers KPIs, roles, talent acquisition, compliance, performance management, and more to boost your HR expertise.

Employee onboarding checklist cover
Checklist
Employee onboarding checklist

Onboarding employees can be a challenge. This checklist provides detailed best practices broken down by days, weeks, and months after joining.

Key hiring metrics cheat sheet cover image
Checklist
Key hiring metrics cheat sheet

Track all the critical calculations that contribute to your recruitment process and find out how to optimize them with this cheat sheet.