## JAVA95

Let's learn java programming language with easy steps

# Top 10 Java Programming Interview Questions and Answers

### Java Programming Interview Questions

Now, Here we will discuss some basic java programming interview questions and answers which are mostly asked in written java programming interviews.

Let's start java coding interview questions and answers step-by-step.

### (1) Write a java program to check prime number

Prime number is a number which is divisible by 1 and itself. Prime number cannot be divisible by other numbers except 1 or itself.

Prime number start from 2, 3, 5, 7, 11.....etc.

0 and 1 are not a prime number.

For example :

{
public static void main(String args[])
{
//check prime number from 2 to 10 number
for(int i =2; i<=10; i++)
{
for(int j = 2; j<=i; j++)
{
if(j==i)
{
System.out.println(i);
}
if(i%j==0)
{
break;
}
}
}
}
}

Output: 2
3
5
7

### (2) Java Program to Print Even and Odd Numbers

An Even number is a number which is divisible by 2 and even numbers start with 2,4,6,8...etc . Odd number is a number which is not divisible by 2 and odd numbers start with 1,3,5,7,9....etc.

For example :

class EvenOddExample
{
public static void main(String args[])
{
//Let's create an array of 10 numbers to check number is even or odd
int a[] = {1,2,3,4,5,6,7,8,9,10};
for(int i = 0; i<a.length; i++)
{
if(a[i]%2==0)//Use modulus(%) operator to check even or odd
{
System.out.println(a[i]+"is even number");
}
else
{
System.out.println(a[i]+"is odd number");
}
}
}
}

Output: 1 is odd number
2 is even number
3 is odd number

4 is even number

5 is odd number

6 is even number

7 is odd number

8 is even number

9 is odd number

10 is even number

### (3) Java Program for Armstrong Number

An Armstrong number is a number that is equal to the sum of cubes of its digits for example :

153 is an Armstrong number because,

1^3+5^3+3^3 = 153 i.e

1+125+27 = 153.

For example :

class ArmstrongNumberExample
{
public static void main(String args[])
{
int a, temp, b = 0;
int c = 153;//Number to be checked for Armstrong
temp = c;
while(c>0)
{
a = c%10;
c = c/10;
b = b+(a*a*a);
}
if(temp==b)
{
System.out.println("Armstrong Number");
}
else
{
System.out.println("Not Armstrong Number");
}
}
}Output: Armstrong Number

### (4) Write a Java Programs to Check Palindrome Number

Palindrome number is a number that remains the same after reverse. for example : 121, 747, 212, 545, 64646, etc.

For example :

class PalindromeExample
{
public static void main(String args[])
{
int r, temp, sum=0;
int n = 747;//number to be checked for palindrome
temp = n;//temporary variable hold the number
while(n>0)
{
r = n%10;
sum = (sum*10)+r;
n = n/10;
}
if(temp==sum)
{
System.out.println("Number is palindrome");
}
else
{
System.out.println("Number is not palindrome");
}
}
}

Output: Number is palindrome

### (5) Write a Java Program to Swap Two Numbers

Java swapping is a very easy program and java swapping can be done with third variable or temporary variable and without third variable or temporary variable. But in this program, we use the third variable or temporary variable.

For example :

class SwappingWithThirdVariable

{
public static void main(String args[])
{
int a = 10;
int b = 20;
System.out.println("before swapping");
System.out.println("value of a "+a);
System.out.println("value of b "+b);
System.out.println();
int temp = a;
a = b;
b = temp;
System.out.println("after swapping");
System.out.println("value of a "+a);
System.out.println("value of b "+b);
}
}

Output: before swapping

value of a 10
value of b 20

after swapping

value of a 20
value of b 10

### (6) Write a Java Program to Swap Two Numbers Without Third Variable

In this program, we can swap two number without using the third variable or temporary variable. For example

class SwappingWithoutThirdVariable

{
public static void main(String args[])
{
int a = 10;
int b = 20;
System.out.println("before swapping");
System.out.println("value of a "+a);
System.out.println("value of b "+b);
System.out.println();
a = a + b;
b = a - b;
a = a - b;
System.out.println("after swapping");
System.out.println("value of a "+a);
System.out.println("value of b "+b);
}
}

Output: before swapping
value of a 10
value of b 20

after swapping
value of a 20
value of b 10

### (7) Find Minimum and Maximum Number in Java Array

In this array program, we will find the largest and smallest number in an array.

class MinAndMaxInArray

{
public static void main(String args[])
{
int n[] = {1,2,5,10,33,47,4,21,81,37,91,102};
//assign first element of an array to largest and smallest
int smallest = n[0];
int largest = n[0];
for(int i = 1; i<n.length; i++)
{
if(n[i]>largest)
largest = n[i];
else if(n[i]<smallest)
smallest = n[i];
}
System.out.println(" Largest Number is "+largest);
System.out.println(" Smallest Number is "+smallest);
}
}

Output: Largest Number is 102
Smallest Number is 1

### (8) Write a Java Programs to Find Duplicate Elements in Array

There are many methods we can use for finding duplicates elements in a java array such as Brute force, HashSet etc. But in this program, we will find the duplicates in an array by using the HashSet concept of the collection.

import java.util.*;
class DuplicatesElementsInArray
{
public static void main(String args[])
{
//taking String array
String d[] = {"eye", "leg", "eye", "ear", "hair"};
HashSet<String> hs = new HashSet<String>();
for(String elements : d)
{
{
System.out.println(elements);
}
}
}
}

Output: eye

### (9) Java Program to Reverse a String

In this program, we will not use reverse method of String. We will reverse the string without using String API.
For example :
class ReverseString
{
public static void main(String args[])
{
String str = "Hello Java";
String reverse = "";
for(int i= str.length()-1; i>=0; --i)
{
reverse+=str.charAt(i);
}
System.out.println(reverse);
}
}

Output: avaJ olleH

### (10) Write a String Palindrome Program in Java

In this program, we will check given string is palindrome or not. Palindrome means remains the same after reverse e.g Madam is palindrome because if we reverse this string it will look the same madam and sir is not a palindrome.

For example :
class PalindromeString
{
public static void main(String args[])
{
String reverse = "";for(int i= str.length()-1; i>=0; --i)
{
reverse+=str.charAt(i);
}
System.out.println(reverse);
if(reverse.equalsIgnoreCase(str))
{
System.out.println("String is palindrome");
}
else
{
System.out.println("String is not palindrome");
}
}
}

Share:

1. Thanks for sharing this interview questions. It is really helpful, continue hsaring more like this.
Angularjs Training in Chennai | Angularjs course in Chennai

2. Thanks for sharing this good blog.It's amazing blogJava Online Course

3. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site. Best Python Training Institute in Bangalore

4. It is a very informative blog for learning AngularJS. Thank you for sharing this wonderful blog.
Angular 4 Training in Chennai | AngularJS Training Chennai | AngularJS Courses in Chennai | Angular Training in Chennai