Top Java Developer Interviews Questions and Answers-8 (coding questions, Core Java, Spring, Microservices)

In this article, you will find many coding problems which are essential to clear the technical interview. You should be able to write on given problems and you should have good database queries to maximize your chance.

Ajay Rathod
5 min readMay 22, 2022

Are you preparing for a job interview as a Java developer?

Find my book Guide To Clear Java Developer Interview here Gumroad (PDF Format) and Amazon (Kindle eBook).

Guide To Clear Spring-Boot Microservice Interview here Gumroad (PDF Format) and Amazon (Kindle eBook).

Download the sample copy here: Guide To Clear Java Developer Interview[Free Sample Copy]

Guide To Clear Spring-Boot Microservice Interview[Free Sample Copy]

Coding Round Questions

Find the output of below code snippet. What will print and why?

public class Test3 {
public static void main(String[] args) {
String x = new String("ab");
change(x);
System.out.println(x);
}
public static void change(String x) {
x = "cd";
}
}

Write a program to find the occurrence of each word in a given string.

String str = “Fear leads to anger; anger leads to hatred; hatred leads to conflict; conflict leads to suffering.”

public class Test {
public static void main(String[] args) {
String str ="Fear leads to anger; anger leads to hatred; hatred leads to conflict; conflict leads to suffering.";

// Splitting to find the word
String arr[]=str.split(" ");

int count =1;
HashMap<String,Integer> hm = new HashMap<>();
for (int i = 0; i <arr.length ; i++) {

if(hm.containsKey(arr[i]))
{
hm.put(arr[i],hm.get(arr[i])+1);
}else{
hm.put(arr[i],1);
}

}
// Loop to iterate over the
// elements of the map
for(Map.Entry<String,Integer> entry:
hm.entrySet())
{
System.out.println(entry.getKey()+
" - "+entry.getValue());
}
}
}

Write a program to find the sum of the entire array result using java 8 streams.

List<Integer> ls = Arrays.asList(1, 2, -3, 4, 5, 6, -7, 8, 9, 10);

Criteria are -> Sum of numbers, if odd, multiply by 2, if even keep same, if negative don’t consider.

public class Test2 {
public static void main(String[] args) {
int[] arr = {1, 2, -3, 4, 5, 6, -7, 8, 9, 10};
List<Integer> ls = Arrays.asList(1, 2, -3, 4, 5, 6, -7, 8, 9, 10);
System.out.println(ls.stream().map(x-> {
if(x < 0 || x % 2 == 0){
return x;
}else if(x %2==1){
return x*2;
}else if(x < 0){
return 0;
}
return null;
}).collect(Collectors.summingInt(Integer::intValue)));
}
}

Write a program to find even numbers from a list of integers and multiply with 2 using stream java 8.

public class FindEvenNumerMultiplyBy2 {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
List<Integer> ans = list.stream().filter(x -> x % 2 == 0).map(x -> x*2).collect(Collectors.toList());
System.out.println(and);
}
}

Mockito cannot mock this class exception from the Mockito unit testing framework, how to resolve that?

Mockito can only mock non-private & non-final classes

Write a program for valid parenthesis in java.

input is — “{()}” so this is a valid parenthesis

String str — “{)(}” this is an invalid parenthesis

package leetcode;

import java.util.HashMap;
import java.util.Stack;

class ParanthesisTest {

// Hash table that takes care of the mappings.
private HashMap<Character, Character> mappings;

// Initialize hash map with mappings. This simply makes the code easier to read.
public ParanthesisTest() {
this.mappings = new HashMap<Character, Character>();
this.mappings.put(')', '(');
this.mappings.put('}', '{');
this.mappings.put(']', '[');
}

public boolean isValid(String s) {

// Initialize a stack to be used in the algorithm.
Stack<Character> stack = new Stack<Character>();

for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);

// If the current character is a closing bracket.
if (this.mappings.containsKey(c)) {

// Get the top element of the stack. If the stack is empty, set a dummy value of '#'
char topElement = stack.empty() ? '#' : stack.pop();

// If the mapping for this bracket doesn't match the stack's top element, return false.
if (topElement != this.mappings.get(c)) {
return false;
}
} else {
// If it was an opening bracket, push to the stack.
stack.push(c);
}
}

// If the stack still contains elements, then it is an invalid expression.
return stack.isEmpty();
}
}

Write a program for the below String.

String str = “wwwwaaadexxxxxxwww”;

output — “w4a3d1e1x6w3”

Find the missing number from array.

class Solution {
public int missingNumber(int[] nums) {
Set<Integer> numSet = new HashSet<Integer>();
for (int num : nums) numSet.add(num);

int expectedNumCount = nums.length + 1;
for (int number = 0; number < expectedNumCount; number++) {
if (!numSet.contains(number)) {
return number;
}
}
return -1;
}
}

Microservices and Spring Boot

Why microservices are stateless?

Recursion vs for loop? which one is best and why?

Recursion works at the method or the function level, whereas looping is applied at the instruction level. We use iteration by repeatedly executing a set of instructions until the terminating condition is hit

How to resolve the circular dependency problem in spring?

A simple way to break the cycle is by telling Spring to initialize one of the beans lazily. So, instead of fully initializing the bean, it will create a proxy to inject it into the other bean. The injected bean will only be fully created when it’s first needed

Using the lookup method and @Lazy annotation we can resolve that

Core Java

Which exception can be thrown from the threads run method?

The reason is that exception is thrown back to the caller. The caller of the run() method is not your code. It is the Thread itself. So even if run() throws an exception the program cannot catch it.

You should put the thread execution result to some class-level variable and then read it from there. Or alternatively use a new API: executors and interface Callable that declares method call() that returns the future result of the thread execution.

Abstract vs interface and use of static vs default methods?

Internal implementation of Concurrent HashMap?

Thanks for reading

  • 👏 Please clap for the story and follow me 👉
  • 📰 Read more content on my Medium (21 stories on Java Developer interview)

Find my books here:

--

--

Ajay Rathod

Software Engineer @Cisco | Java Programmer | AWS Certified | Writer | Find My Books on Java Interview here - https://rathodajay10.gumroad.com/