Top 65 Java Programming Interview Questions and Answers

This post is dedicated to all the java programming interview questions and answers that are getting asked in the internship interview or full time entry level graduate interview (freshers). 

Q1. How to reverse a String in java? [Solution]

Write a java program to reverse the String. There are 6 ways to reverse a String in java. You should know at least how to reverse a String without using reverse() method in java.

Q2. How to find the first non repeated character in the String? [Solution]

This is the starting question for the product companies, so make sure you go through it. Write a java program to find the first non repeated character in the String.

Q3. How to find intersection of two arrays in java? [Solution]

Write a java program to find common elements between the two given arrays.
 Suppose given two arrays array1= {1,4,7, 9, 2} arrray2 = {1,7,3,4,5} the answer should be {1,4,7}
[Solution]

Q4 Find out if String has all Unique Characters? [Solution]

Write a java program to find out if the given String has all Unique Characters. There are 5 ways to determine String has all Unique Characters.

Q5 How to Count number of words in the String? [Solution]

This is an important phone interview coding question. Write a java program to count number of words in the String.



Q6 How to remove all the white-spaces in the String? [Solution]

Write a java program to remove all the white-spaces in the String.

Q7 Armstrong number? [Solution]

Write a java program for the Armstrong number . A number is called Armstrong number if it is equal to the sum of the cube of each digit.for example 371 is an Armstrong number because
371 = 27+343+1 which is equal to 3^3+7^3+1^3.

Q8 How to create a deadlock between two threads? [Solution]


Write a java program to create deadlock between two threads. This is one of the tricky question of multi-threading concept. A deadlock can occur in java when the synchronized keyword causes the executing thread to block while waiting for the lock or monitor associated with the specified object.

Q9 Anagram program in java? [Solution]

Write a java program to check whether two given strings are anagram. If two strings contain same set of characters but in different order then the two strings are called anagram.

Q10 Factorial ?

Write a java program to find factorial of a given number. This is one of the easiest program you can expect in the interview. Sometimes an interviewer might ask to convert it to an iterative solution or vice versa. [Solution]

Q11 How to Count occurrences of each character in a String in java?[Solution]

Write a java program to count occurrences of each character in String in java. If the String is  
"Java Hungry" then the answer should be
{ =1, a=2, r=1, u=1, v=1, g=1, H=1, y=1, J=1, n=1}


Q12 Printing Patterns in Java ? [Solution]

There are 19 patterns shared in the post. Write a java program for each pattern shared in the post.[Solution]

Q13 Square root of number in java?

Write a java program to find out the square root of given number ? [Solution]

Q14 Merge sort in java? 

Write a java program to implement merge sort. You can give the iterative or recursive answer to this question. You should have the understanding of calculating time complexity and space complexity of the coding answer. [Solution]

Q15 Print all permutations of the String ?

Write a java program to find all the permutations of the given String. Permutation is the all possible combinations of the Strings possible for any word. [Solution]

Q16 Palindrome of String Verification ?
Write a java program to check whether input string is palindrome of the given string or not.[Solution]

Q17 Convert Lowercase to Uppercase in java and Uppercase to Lowercase without using built in method ?

Write  a java program to convert Lowercase to Uppercase and vice versa in a given String. [Solution]

Q18 Find all possible combinations of String?

Write a java program to find all possible combinations of String. This question can be asked in the phone interview, so make sure you go through it.[Solution]

Q19 Find the maximum value in Array without using Collection?

Suppose there is an array {1,7,91,12,57} , then the answer should be 91 as it is the maximum value in the given array.[Solution]

Q20 How to remove specific characters in the String? [Solution]


To remove specific characters in the String .For example,

If the original string is "Alive is awesome"   and the user inputs string to remove "alwsr"  then it should print  "ive i eome" as output .

If the original string is "Learning never stops"   and the user inputs string to remove "estp"  then the it should print   "Larning nvr o" as output .


Q21 Find the range of maximum and minimum value of data type?[Solution]

Every primitive data type have the MAX_VALUE and MIN_VALUE static field in their  APIs( except boolean ). You need to simply call MAX_VALUE or MIN_VALUE on the data type.  

Q22 How to calculate total number of characters in the String?

  Write a java program to calculate total number of characters in the String .[Solution]

Q23 How to Convert Signed Integer to String? [Solution]


Here to convert signed int to string in java, two case arises :

1. If number is positive (+ve)   :
   
    Input : 537   Output :  "537"

2. If number is negative (-ve)
    Input : -537  Output : "-537"

Q24 How to Convert Math number to equivalent readable word in java?[Solution]

The format for writing numbers in words is like this

Unit,ten,hundred,thousand,million,billion,trillion,quadrillion,quintillion.

For example :

Input   :   123456789
  
Output: One hundred twenty three million four hundred fifty six thousand seven hundred eighty nine


Q25 How to calculate total number of vowels in String?[Solution]

Write a java program to calculate total number of vowels in String .
for example :
if the original string : " Alive is awesome "
then the number of vowels is : 8

Q26 Convert Uppercase to Lowercase in java without using built in method?


Write a java program to convert Uppercase characters into Lowercase characters without using built in method.

Q27 Find the maximum value in Array using Collection?  [Solution]

Q28 Quick sort in java?[Solution]

Write a java program to implement Quick sort in java.

Q29 Insertion sort in java?[Solution]

Write a java program to implement Insertion sort in java.

Q30 Selection sort in java?[Solution]

Write a java program to implement selection sort in java.

Q31 Bubble sort in java?[Solution]

Write a java program to implement Bubble sort in java.

Q32 Singleton design pattern in java?[Solution]

Write a java program to implement Singleton Design Pattern.

 Q33 String concatenation in java?[Solution]

Write different ways for String concatenation in java?
Input: “Hello” and “World”
Output: “HelloWorld”


Input: “1234” and “5678”
Output: “12345678”
 

 Q34 How to get Thread ID in java?[Solution]

Q35 How to reverse an int Array in java?[Solution]

Let understand the question through the example :

Input    : {3, 8, 5, 7, 4}
Output : {4, 7, 5, 8, 3}

Input     : {10, 54, 23, 89, 97, 2}
Output  : {2, 97, 89, 23, 54, 10}

Q36 How to reverse ArrayList in java?[Solution]

Let understand the question through the example :
ArrayList Before Reverse :
[Apple, Amazon, Facebook, Google, IBM, Tesla]
ArrayList After Reverse :
[Tesla, IBM, Google, Facebook, Amazon, Apple]


Q37 How to sort an ArrayList in java?[Solution]

Q38 How to find length/size of ArrayList in java?[Solution]

Write a java program to  find length/size of ArrayList in java.

Q39 How to iterate over ArrayList in java?[Solution]

Write a java program to iterate over the ArrayList in java?

Q40 How to add() element in the ArrayList in java?[Solution]

Q41 How to remove element at the specified index in java?[Solution]

Q42 How to convert LinkedList to ArrayList in java?[Solution]

We are using ArrayList constructor to convert LinkedList to ArrayList.
Syntax is new ArrayList(Collection c)

Q43 How to convert HashSet to ArrayList(List) in java?[Solution]

Write a java program to convert HashSet to ArrayList(List) in java.

Q44 How to convert ArrayList to String array in java?[Solution]

Write a java program to convert ArrayList to String array in java.

Q45 How to convert Array to ArrayList in java?[Solution]

Write a java program to convert Array to ArrayList(List) in java.

Q46  How to sort ArrayList in Descending order in java?[Solution]

Q47 How to remove key value mapping from HashMap?[Solution]

Q48 How to sort HashMap in java by keys and values?[Solution]

Sort by Keys example
Before Sorting:
33: Z
99: I
22: A
55: B
88: X
44: M
After Sorting:
22: A
33: Z
44: M
55: B
88: X
99: I

Q49 How to iterate or loop over HashMap ?[Solution]

I have shared 4 ways to iterate over HashMap in java.

Q50 How to iterate through the HashSet/Set?[Solution]

 Write a java program to iterate over HashSet in java

Q51 How to convert Array to TreeSet in java?[Solution]

Q52 How to convert HashSet to Array in java?[Solution]

Write a java program to convert HashSet to Array in java.

Q53 How to convert HashSet to List in java?[Solution]

Write a java program to convert HashSet to List in java.

Q54 How to iterate TreeMap in java?[Solution]

TreeMap automatically sorts keys in the ascending order. Write a java program to iterate over TreeMap in java.

Q55 How to read text file in java ?[Solution]

Q56 Java HashSet class example ?[Solution]

Q57 Java TreeMap class example ?[Solution]

Q58 Java TreeSet class example ?[Solution]

Q59 Java LinkedList class example?[Solution]

Q60 Java ArrayList class example?[Solution]

Q61 How to create a new file in java ?[Solution]

There are 3 ways to create a new file in java . Please make sure you need at least one method to create a new file in java.
     
Q62 Java Annotation example ?[Solution]

Q63 Prime number Program  [Solution]

Prime number can be defined as :
The number which is divisible only by itself and 1 . That is other than two numbers (itself and 1) no other number can give remainder 0

Q64 Fibonacci Series [Solution]

 The next number in the series is the sum of previous two numbers .


Fibonacci Series is as follow :

0,1,1,2,3,5,8,13,21.34,55,89,144,233,377,610 .........

Q65 Java HashMap class example [Solution]

Please mention in the comments if you have faced any other java program coding question during the interview process.

0 Comments