Now that you have made a quality JD, it can still be tricky to evaluate the skills of your applicants when you hire Java developers. To help you with that, we have created a pool of questions that a good Java developer should be comfortable with. You can check out some related questions here: Java, Hibernate, JPA.
It is important to note that the ability to answer these questions doesn't imply that you have a top quality candidate. But it definitely is a big step in that direction.
To help you navigate through these questions, we’ve categorized the interview questions in 3 parts:
A. Basic concepts: Includes all basic concepts used across languages but we've focused on its significance in Java. This section will give you an understanding of how strong their programming foundation is.
B. Advanced concepts: Includes all concepts that someone with higher expertise should know.
C. DS/Algorithm questions: To test the logical capability of the candidate.
A. Basic concepts
How are whitespaces removed from a string in Java?
The Java strip() is a string method that removes all leading and trailing whitespaces. Strip() uses the Character.isWhitespace() method internally to check for whitespaces.
This method identifies whitespaces using Unicodes and hence it is the recommended method to remove whitespaces.
stripLeading() and stripTrailing() are alternatives in case you are only looking to remove leading or trailing whitespaces respectively.
The code below is an example of the Java strip() method
String s = " flexiple ";
s = s.strip();
System.out.println(s);
Write a code snippet to implement a Fibonacci series using recursion?
The code snippet below implements the Fibonacci series using recursion which is a common Java interview question.
public class FibonacciNumbers {
public static int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
public static void main(String args[]) {
int n = 10;
System.out.println(fibonacci(n));
}
}
B. Advanced concepts
How do you reverse a string in Java without using the reverse() methods?
Java does not come out of the box with the reverse() method although the reverse() method does exist in a few libraries such as StringBuffer or StringBuilder. Hence, reversing an array has become a common interview question.
Following is a simple algorithm that can be used to reverse an array.
public class StringReverse {
public static void main(String[] args) {
String str = "Flexiple";
System.out.println(reverse(str));
}
public static String reverse(String in) {
if (in == null)
throw new IllegalArgumentException("Null is not valid”);
StringBuilder out = new StringBuilder();
char[] chars = in.toCharArray();
for (int i = chars.length - 1; i >= 0; i--)
out.append(chars[i]);
return out.toString();
}
}
What causes a deadlock scenario? Write code to create a deadlock.
A deadlock scenario is caused when two threads require the same locks to execute.
These scenarios occur when both threads have obtained one lock and are waiting to obtain the other lock. However, since both threads wait for the other to execute they block each other causing a deadlock.
Multi Threaded programs suffer from deadlocks because the synchronized keyword is used to make the methods thread-safe. This means that only one thread can lock and use the synchronized method. Other threads have to wait for the current thread to complete.
The code below creates two threads that are in a deadlock.
class Util
{
static void sleep(long millis)
{
try
{
Thread.sleep(millis);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
class Shared
{
synchronized void test1(Shared s2)
{
System.out.println("test1-begin");
Util.sleep(1000);
s2.test2();
System.out.println("test1-end");
}
synchronized void test2()
{
System.out.println("test2-begin");
Util.sleep(1000);
System.out.println("test2-end");
}
}
class Thread1 extends Thread
{
private Shared s1;
private Shared s2;
public Thread1(Shared s1, Shared s2)
{
this.s1 = s1;
this.s2 = s2;
}
@Override
public void run()
{
s1.test1(s2);
}
}
class Thread2 extends Thread
{
private Shared s1;
private Shared s2;
public Thread2(Shared s1, Shared s2)
{
this.s1 = s1;
this.s2 = s2;
}
@Override
public void run()
{
s2.test2(s1);
}
}
public class Deadlock
{
public static void main(String[] args)
{
Shared s1 = new Shared();
Shared s2 = new Shared();
Thread1 t1 = new Thread1(s1, s2);
t1.start();
Thread2 t2 = new Thread2(s1, s2);
t2.start();
Util.sleep(2000);
}
}
C. Data Structure/ Algorithm
1. Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Note that an empty string is also considered valid.
class ValidParenthesesFunc {
func isValid(_ s: String) -> Bool {
var stc = [Character]()
for char in s {
if char == "(" || char == "[" || char == "{" {
stc.append(char)
} else if char == ")" {
guard stc.count != 0 && stc.removeLast() == "(" else {
return false
}
} else if char == "]" {
guard stc.count != 0 && stc.removeLast() == "[" else {
return false
}
} else if char == "}" {
guard stc.count != 0 && stc.removeLast() == "{" else {
return false
}
}
}
return stc.isEmpty
}
}
The above code will input 0(false).
2. Write a program to find whether a string or number is palindrome or not.
import java.util.Scanner;
public class Palindrome {
public static void main (String[] args) {
String original, reverse = "";
Scanner in = new Scanner(System.in);
int length;
System.out.println("Enter a number or a string");
original = in.nextLine();
length = original.length();
for (int i =length -1; i>;=0; i--) {
reverse = reverse + original.charAt(i);
}
System.out.println("The reverse is: " +reverse);
if(original.equals(reverse))
System.out.println("The string is a palindrome");
else
System.out.println("The stringis not a palindrome");
}
}
Output:
For String- Enter a number or a string
sam
The reverse is: mas
The string is not a palindrome
For Number-Enter a number or a string
99
The reverse is: 99
The number is a palindrome
3. What will the output of the following code be?
class Test {
public static void main(String args[])
{
String str1 = new String("Hello World");
String str2 = new String("Hello World");
String str3 = "Hello World";
String str4 = "Hello World";
int a = 0, b = 0, c = 0;
if (str3 == str4)
a = 1;
else
a = 2;
if (str1.equals(str3))
b = 1;
else
b = 2;
if (str1 == str4)
c = 1;
else
c = 2;
System.out.println("a= " + a + " b= " + b + " c= " + c);
}
}
A. a=2 b=1 c=2
B. a=2 b=2 c=2
C. a=1 b=2 c=1
D. a=1 b=1 c=2
D. a=1 b=1 c=2 because a new memory is created when we make an object with the help of the ‘new’ keyword,
and the reference variable contains the memory location.
Here the memory is created with the same string twice, but since we are comparing objects and not strings,
the object will point to a different memory location and so they are not equal.