Java Interview Programming Questions and Answers

java-programs thumbnail

Java is an object-oriented programming language and computing platform first released by Sun Microsystems in 1995.

During an interview it does not matter whatever framework you know, the initial stage of the interview is always based on the programming sense, coding skills, and problem-solving capabilities. To test these, all the interviewers start interview from core programming problems and most of these programming problems are from core java concepts. Here, in this article, I have tried to put some initial level of Java programming questions to get through an interview process. These programs are helpful to both freshers and experienced Java professionals.

Keep an eye on this page as I will be regularly adding and updating more and more interview questions.

Write a Java program to find the largest sum of the contiguous subarray in a given Array

By Dhiraj 20 June,2020 7K

Write a Java program to find the largest sum of the contiguous subarray in a given Array. The given array might contain negative elements too and hence we need to find out a contiguous sub-array whose sum would be maximum....

Read now➤

Write a Java program to rotate a matrix

By Dhiraj 10 May,2020 28K

Given a 2D matrix of N X N. Write a Java program to rotate the matrix in a clockwise direction by 90 degrees. The 0th row of the given matrix will be transformed to the nth column, the 1st row will be transformed to the n-1 column, and so on. Below is its representation....

Read now➤

Writing a Java program to rotate an array by d elements.

By Dhiraj 09 May,2020 6K

For any given array of length n, rotating it by elements d means moving its first d elements either at the end of the array or moving the last d elements to the beginning of the array....

Read now➤

Java Program to Find Union and Intersection of Arrays in Java

By Dhiraj Ray 17 February,2020 9K

Writing a Java program to find first non-repeated character in a String is a common programming interview question to test the usage of Set interface provided in Java collection framework....

Read now➤

Java Program to Find First non Repeated Character in a String

By Dhiraj Ray 16 February,2020 4K

Writing a Java program to find first non-repeated character in a String is a common programming interview question. For example, the first non-repeated character in the String 'devglan for developers' is 'g'....

Read now➤

Write a Java Program to Find missing Number in an Array

By Dhiraj Ray 15 February,2020 11K

Write a java program to find a missing number in an Array of length N-1 containing elements from 1 to N. The trick to find the missing number is using the mathematical formula of Sum of Series....

Read now➤

Write a Java Program to Compare Files in Java

By Peter Hill 20 November,2018 11K

When you are learning Java, network programming can be a great source of practicing Java. In this program, we will see how we can compare two different files in Java. Comparing files in java also can help you to differentiate between the local and the remote files. You can quickly identify the duplicate lines, which allows you to remove the file entirely. First, we will be comparing the files using BufferedReader but this way of comparing files is not memory efficient and requires more execution time. Hence, we will be using a highly advanced technique called memory mapping using RandomAccessFile from java.io package....

Read now➤

Write a Java Program to Print 1 To 10 Without Using Loop

By Dhiraj Ray 10 March,2018 55K

Write a java program to print 1 to 10 without using any loop.This can be achieved by using recursion in Java.Following is the sample code....

Read now➤

Write a Java Program to Reverse an Array in Place Without Using Any Second Array

By Dhiraj Ray 10 March,2018 35K

Write a java program to reverse an array in place without using any second array.Here, we can loop till the middle index of the array and swap the first element with last element, swap the second element with second last element until we reach the middle of the array....

Read now➤

Write a Java Program to Find the Longest Palindrome Present in a Given String

By Dhiraj Ray 10 March,2018 22K

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as madam. Write a java program to find the longest palindrome present in a given string. For example, in the string abcba, the longest palindrome is abcba and similarly in abcmadamcbamadam, the longest palindrome is abcmadamcba....

Read now➤

Write a Java Program to Find the Second Largest Number in an Array

By Dhiraj Ray 10 March,2018 11K

Write a java program to find the second largest number in an array.There are multiple ways to achieve this. We can sort any given array in a descending order and pick the second index element.The main concept here is to sort the given array.This can be achieved via Arrays.sort() or Collection.sort() and once the given array is sorted the second largest number can be easily found....

Read now➤

Write a Java Program to Remove Common Characters From Given Strings

By Dhiraj Ray 10 March,2018 43K

In many java interviews, it is asked this question to compare two strings and remove the common character from the given strings to check the programming aptitude.For example, suppose there are two string, s1 = "abcfgh" and s2 = "aasdf" and after removal of common character the value of s1 and s2 becomes bcgh and sd respectivly. Following is the java program to remove the common characters from any given strings....

Read now➤

Java Program To Check If A Given Number is A Perfect Number

By Dhiraj Ray 06 March,2018 11K

A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.Write a java program to check if a given number is a perfect number or not....

Read now➤

Check Given String is Rotation of Another String

By Dhiraj Ray 06 March,2018 8K

Given two string s1 and s2 then write a java program to check if s1 is the rotation of another string s2.Here rotation means, each character of s2 must be the same character of s1 but only thing is that the character in s2 can be present at any random position. Following is the java program to check if a given string is a rotation of another string....

Read now➤

Java Program to Find LCM of a Two Given Number

By Dhiraj Ray 14 January,2018 20K

Write a Java program to find LCM of a given two numbers.LCM stands for Lowest Common Multiple. LCM of a two given number a and b is the smallest positive integer that is divisible by both a and b.For example the LCM of 4 and 6 is 12. Following is the java program to find LCM using while and for loop....

Read now➤

Write a Java Program to Test Given Words are Anagram or Not

By Dhiraj Ray 10 January,2018 78K

Two words are said to be anagrams, if both the words contain same set of characters with all original letters exactly once. For example, the word program can be re-arranged as grampor and these both words form an anagram. Following is a java program to check if a string is anagram or not. There are two ways for this test - One is using equals() method provided in Arrays class and another by comparing each character of he words....

Read now➤

Write a Java Program to Add Two 2D Matrix

By Dhiraj Ray 10 January,2018 6K

A matrix is a rectangular array of numbers or expressions arranged in rows and columns.Below is the java program to add two 2D matrices.We can change the dimension variables accordingly for 3D and 4D matrix....

Read now➤

Write a Java Program to Perform Binary Search in a Sorted Array

By Dhiraj Ray 10 January,2018 4K

Binary search is one of the famous and fundamental algorithm in computer science which solves a large number of problems in developing applications and APIs. It uses divide and conquer approach to find out the element.Here, we will be writing a sample program in java that implements binary search.The given array is a sorted array of n elements and we have to search the position a given element inside the array....

Read now➤

Write a Java Program to find Factorial of a Given Number

By Dhiraj Ray 02 January,2018 7K

Write a program to find factorial of a given number is a common java program asked in any interview to freshers.The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example factorial of 4 is 4*3*2 = 24.There are 2 ways to find a factorial of a given number - One by using for loop and the other using recursion.Following java program uses for loop to find factorial of a given number....

Read now➤

Java Program to Reverse a Given String without using Predefined Functions

By Dhiraj Ray 01 January,2018 6K

Write a java program to reverse a given string using recursion without using any predefined function.This program checks the understanding of recursion in programming language and also checks the programming sense.Here, the method reverseString() is called recursively to revrse a given string.Following is the complete program....

Read now➤

Java program to Find Permutations of a Given String

By Dhiraj Ray 01 January,2018 20K

Write a java program to find all the permutations of any given string.Permutation is the each of several possible ways in which a set or number of things can be ordered or arranged.Order matters in case of Permutation.For example, the permutation of ab will be ab and ba.Following is the java program to find permutation of a given string....

Read now➤

Java program to reverse a given number

By Dhiraj Ray 01 January,2018 8K

Write a java program to reverse a given number by using only numeric operators.Suppose, if you are given an input of 4567 then the output should be 7654.In the program below, we have only used the modulus or remainder and / operator.Following is the complete program....

Read now➤

Java program to find sum of prime numbers which are less than a given number

By Dhiraj Ray 01 January,2018 23K

Write a java program to find the sum of all the prime numbers less than a given natural number N. The main purpose of this interview question is to check the programming sense and capabilities to check how good you are to convert existing logic into code. The question is mostly asked to freshers.The only main logic in this program is to find the prime numbers.Prime number is a number that is greater than 1 and divided by 1 or itself.For reminder, 2 is also a prime number....

Read now➤

Java program to find max repeated words from a file

By Dhiraj Ray 01 January,2018 11K

In java interview, this program can be asked in a multiple ways such as write program to find max repeated words or duplicate words or the count of each duplicate words.Whatever the question, the main programming concept is the same to count the occurrence of each word in a .txt file. To solve this programmatically, we can use Map implementation in Java that does not allow any duplicate key and at the end of iteration we can find out the count.Following is the complete program.Here, we are using java 8 Lambda operator during sorting....

Read now➤

Java Program to find max two numbers in an array

By Dhiraj Ray 01 January,2018 31K

In many java interviews especially for freshers, it is asked to write program to find max two numbers from a given array.This kind of program is good to check the programming sense as this program does not use any inbuilt java sorting functions or predefined data structures or collections.It simply uses java iterations and programming sense to swap in between the numbers and find the solution.Following is the implementation....

Read now➤

Java Program to Find line with max character length in Descending Order

By Dhiraj Ray 01 January,2018 7K

This java interview program is about finding lines with max character from a file in descending order.In this case, we will be using buffered reader to read a file and use java provided data structure TreeSet to hold the line and it's character length as it automatically maintains ascending order.Following is the program to find two lines with max characters in descending order....

Read now➤

Java Program to Find middle index of array where both ends sum is equal

By Dhiraj Ray 01 January,2018 26K

In the java interview, you will be asked to find the middle index or position of a given array where sum of numbers preceding the index is equals to sum of numbers succeeding the index.There are two ways to solve this problem.One is to use 2 for loops - one starting from the last index to the middle index and another starting from start index to middle index. Another way to solve it by using while loop - the while loop should stop when the start index crosses the end index. Following is the program to achieve this using while loop....

Read now➤

Java Program to Find Duplicate Character from a String and Count of repetition

By Dhiraj ray 31 December,2017 11K

While dealing with string, many of the time it is required to find or remove duplicate character from a string.Following is the java program to find duplicate or repeated characters from a given string.The program also results the count of the duplicate characters....

Read now➤

Java Program to Find Distinct Word List From a File

By Dhiraj Ray 31 December,2017 13K

Java program to find distinct words from file is a very common question in java interview.In the following program, we will be using BufferedReader to read a file and then retain distinct words from it. To achieve this, we will be using Set to store all the words from a file and since, set dos not allow duplicates, we can easily find the distinct words.Following is the complete java program for this....

Read now➤

Java Program to test if a given number is Fibonacci or not

By Dhiraj Ray 31 December,2017 12K

Fibonacci numbers are the numbers in which each number is the sum of the two preceding numbers.For example 1, 1, 2, 3, 5, 8, 13, 21, 34, ...The first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence.In most of the java interview, itapos;s a common programming question to check a given number is fibonacci number or not....

Read now➤

Java Program to test if a given number is Armstrong Number or not

By Dhiraj Ray 31 December,2017 9K

Armstrong Number is a number that is the sum of its own digits each raised to the power of the number of digits.For example 371 is an Armstrong number as 3^3+7^3+1^3 = 371.It is also sometimes called as narcissistic number or pluperfect digital invariant (PPDI). Following is the java program to test if a given number is an armstrong number or not....

Read now➤