Commonly Asked Java Programming Interview Questions And Answer | Top 20 Java Interview Programs for Programming and Coding | Java Interview Programs | Core java programs -
Commonly Asked Java Programming Interview Questions And Answer
1.BinaryToDecimalExample1 Program in java.
public class BinaryToDecimalExample1
{
public static void main(String args[]){
String binaryString="1010";
int decimal=Integer.parseInt(binaryString,2);
System.out.println(decimal);
}
}
2.PalindromeExample Program in java.
class PalindromeExample{
public static void main(String args[]){
int r,sum=0,temp;
int n=454;//It is the number variable to be checked for palindrome
temp=n;
while(n>0){
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}
}
public class RemoveDuplicateInArrayExample2{
public static int removeDuplicateElements(int arr[], int n){
if (n==0 || n==1){
return n;
}
int j = 0;//for next element
for (int i=0; i < n-1; i++){
if (arr[i] != arr[i+1]){
arr[j++] = arr[i];
}
}
arr[j++] = arr[n-1];
return j;
}
public static void main (String[] args) {
int arr[] = {10,20,20,30,30,40,50,50};
int length = arr.length;
length = removeDuplicateElements(arr, length);
//printing array elements
for (int i=0; i<length; i++)
System.out.print(arr[i]+" ");
}
}
1.
public class EvenNumbers {
public static void main(String[] args) {
//define limit
int limit = 50;
System.out.println("Printing Even numbers between 1 and " + limit);
for(int i=1; i <= limit; i++){
// if the number is divisible by 2 then it is even
if( i % 2 == 0){
System.out.print(i + " ");
}
}
}
}
class OddNumber
{
public static void main(String args[]) {
System.out.println("The Odd Numbers are:");
for (int i = 1; i <= 100; i++) {
if (i % 2 != 0) {
System.out.print(i + " ");
}
}
}
}
public class primenumber
{
public static void main(String[] args) {
int num = 9;
boolean flag = false;
for(int i = 2; i <= num/2; ++i)
{
// condition for nonprime number
if(num % i == 0)
{
flag = true;
break;
}
}
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}
8.Reverse String Program in java
import java.io.*;
class rev //Reverse String Program
{
public static void main(String args[])
{
String input="hiiiiiiiiiii";
char[] t=input.toCharArray();
for(int i=t.length-1;i>=0;i--)
System.out.print(t[i]);
}
}
9.Java program for implementation of Bubble Sort in java
class BubbleSort
{
void bubbleSort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[])
{
BubbleSort ob = new BubbleSort();
int arr[] = {64, 34, 25, 12, 22, 11, 90};
ob.bubbleSort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
}
}
10.Check_Leap_Year Program in java
import java.util.Scanner;public class Check_Leap_Year{public static void main(String args[]){Scanner s = new Scanner(System.in);System.out.print("Enter any year:");int year = s.nextInt();boolean flag = false;if(year % 400 == 0){flag = true;}else if (year % 100 == 0){flag = false;}else if(year % 4 == 0){flag = true;}else{flag = false;}if(flag){System.out.println("Year "+year+" is a Leap Year");}else{System.out.println("Year "+year+" is not a Leap Year");}}}Output:$ javac Check_Leap_Year.java
$ java Check_Leap_Year
Enter any year:1800
Year 1800 is not a Leap Year
Enter any year:2000
Year 2000 is a Leap Year
11.Find small and large number using Array in java
Class smallandlarge
{p.s.v.m(){
Int no[]={1,3,4,5,7,8,9};
Int small=no[];
Int large=no[];
For(int i=1;i<no.length;i++)
{
If(no[i]>large)
Large=no[i];
Else
If(no[i]<small)
Small=no[i];
}
s.o.p(large);
s.o.p(small);
}
}
12.swapping program
import java.util.*;
class pattern
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int number[30],n[]={50,40,60,30,10,55};
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;++j)
{
if(number[i]<number[j])
{
int temp=number[i];
number[i]=number[j];
number[j]=temp;
}
for(i=0;i<n;i++)
{
System.out.print("");
}}
}
13.Find sum of number in java
/*int n=123,n1,sum=0;
while(n>0)
{
n1=n%10;
n=n/10;
sum=sum+n1;
System.out.println(""+sum);
}*/
//System.out.println("Enter Number ");
//int n=sc.nextInt();
/*
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.print(""+fact);
*/
15.Find fibonacci program in java
/*int n1=0,n2=1,n3,count=30;
System.out.println(""+n1);
System.out.println(""+n2);
for(int i=2;i<=count;i++)
{
n3=n1+n2;
n1=n2;
n2=n3;
System.out.println(""+n3);
}*/
16.Find Reverse Number in java
/*
int n=13242560,q,r;
for(int i=0;i<8;i++)
{
q=n/10;
r=n%10;
System.out.println(r);
n=q;
}*/
/*
int fact=1;
int a,c=0,temp,n=153;
temp=n;
while(n>0)
{
a=n%10;
n=n/10;
c=c+(a*a*a);
}
if(temp==c)
{
System.out.print("Armstrong");
}
else
{
System.out.print("not");
}
*/
/*
for(int i=1;i<=a;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}*/
}}
Pattern programs in java : Pattern 1
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
//Close the resources
sc.close();
}
}
Pattern programs in java : Pattern 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
//Printing upper half of the pattern
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
//Printing lower half of the pattern
for (int i = rows-1; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
//Closing the resources
sc.close();
}
}
Pattern programs in java : Pattern 3
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i+" ");
}
System.out.println();
}
//Close the resources
sc.close();
}
}
Pattern programs in java : Pattern 4
7 6 5 4 3 2 1
7 6 5 4 3 2
7 6 5 4 3
7 6 5 4
7 6 5
7 6
7
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j >= i; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
//Closing the resources
sc.close();
}
}
Pattern programs in java : Pattern 5
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
//Closing the resources
sc.close();
}
}
Pattern programs in java : Pattern 6
7 6 5 4 3 2 1
6 5 4 3 2 1
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
for (int i = rows; i >= 1; i--)
{
for (int j = i; j >= 1; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
//Closing the resources
sc.close();
}
}
Pattern programs in java : Pattern 7
7
7 6
7 6 5
7 6 5 4
7 6 5 4 3
7 6 5 4 3 2
7 6 5 4 3 2 1
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
for (int i = rows; i >= 1; i--)
{
for (int j = rows; j >= i; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
//Closing the resources
sc.close();
}
}
Pattern programs in java : Pattern 8
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
for (int i = 1; i <= rows; i++)
{
//Printing first half of the row
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
//Printing second half of the row
for (int j = i-1; j >= 1; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
//Closing the resources
sc.close();
}
}
Pattern programs in java : Pattern 9
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
//Printing upper half of the pattern
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
//Printing lower half of the pattern
for (int i = 2; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
//Closing the resources
sc.close();
}
}
Pattern programs in java : Pattern 10
1234567
234567
34567
4567
567
67
7
67
567
4567
34567
234567
1234567
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
//Printing upper half of the pattern
for (int i = 1; i <= rows; i++)
{
//Printing i spaces at the beginning of each row
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
//Printing i to rows value at the end of each row
for (int j = i; j <= rows; j++)
{
System.out.print(j);
}
System.out.println();
}
//Printing lower half of the pattern
for (int i = rows-1; i >= 1; i--)
{
//Printing i spaces at the beginning of each row
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
//Printing i to rows value at the end of each row
for (int j = i; j <= rows; j++)
{
System.out.print(j);
}
System.out.println();
}
//Closing the resources
sc.close();
}
}
Pattern programs in java : Pattern 11
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
for (int i = 1; i <= rows; i++)
{
for (int j = i; j >= 1; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
//Close the resources
sc.close();
}
}
Pattern programs in java : Pattern 12
1
10
101
1010
10101
101010
1010101
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
if(j%2 == 0)
{
System.out.print(0);
}
else
{
System.out.print(1);
}
}
System.out.println();
}
sc.close();
}
}
Pattern programs in java : Pattern 13
1 2 3 4 5 6 7
2 3 4 5 6 7
3 4 5 6 7
4 5 6 7
5 6 7
6 7
7
6 7
5 6 7
4 5 6 7
3 4 5 6 7
2 3 4 5 6 7
1 2 3 4 5 6 7
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
//Printing upper half of the pattern
for (int i = 1; i <= rows; i++)
{
//Printing i spaces at the beginning of each row
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
//Printing i to rows value at the end of each row
for (int j = i; j <= rows; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
//Printing lower half of the pattern
for (int i = rows-1; i >= 1; i--)
{
//Printing i spaces at the beginning of each row
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
//Printing i to rows value at the end of each row
for (int j = i; j <= rows; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
//Closing the resources
sc.close();
}
}
Pattern programs in java : Pattern 14
1111111
1111122
1111333
1114444
1155555
1666666
7777777
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= rows-i; j++)
{
System.out.print(1);
}
for (int j = 1; j <= i; j++)
{
System.out.print(i);
}
System.out.println();
}
sc.close();
}
}
Pattern programs in java : Pattern 15
1010101
0101010
1010101
0101010
1010101
0101010
1010101
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
for (int i = 1; i <= rows; i++)
{
int num;
if(i%2 == 0)
{
num = 0;
for (int j = 1; j <= rows; j++)
{
System.out.print(num);
num = (num == 0)? 1 : 0;
}
}
else
{
num = 1;
for (int j = 1; j <= rows; j++)
{
System.out.print(num);
num = (num == 0)? 1 : 0;
}
}
System.out.println();
}
sc.close();
}
}
Pattern programs in java : Pattern 16
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
for (int i = 1; i <= rows; i++)
{
int num = i;
for (int j = 1; j <= i; j++)
{
System.out.print(num+" ");
num = num+rows-j;
}
System.out.println();
}
sc.close();
}
}
Pattern programs in java : Pattern 17
0000000
0100000
0020000
0003000
0000400
0000050
0000006
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
for (int i = 0; i <= rows; i++)
{
for (int j = 0; j <= rows; j++)
{
if (i == j)
System.out.print(i);
else
System.out.print(0);
}
System.out.println();
}
sc.close();
}
}
Pattern programs in java : Pattern 18
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
System.out.println("How many rows you want in this pattern?");
Scanner sc = new Scanner(System.in);
int noOfRows = sc.nextInt();
int value = 1;
System.out.println("Here is your pattern :");
for (int i = 1; i <= noOfRows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(value+"\t");
value++;
}
System.out.println();
}
}
}
Pattern programs in java : Pattern 19
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
System.out.println("How many rows you want in this pattern");
Scanner sc = new Scanner(System.in);
int noOfRows = sc.nextInt();
for (int i = 1; i <= noOfRows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i+" ");
}
System.out.println();
}
}
}
// Java program to Reverse a String by
// converting string to characters one
// by one
import
java.lang.*;
import
java.io.*;
import
java.util.*;
// Class of
ReverseString
class
ReverseString
{
public
static
void
main(String[] args)
{
String
input = "GeeksForGeeks";
//
convert String to character array
//
by using toCharArray
char[]
try1 = input.toCharArray();
for
(int
i = try1.length-1;
i>=0; i--)
System.out.print(try1[i]);
}
}
20.JAVA Code to Find a pair with maximum product in array of Integers
// JAVA Code to Find a pair with maximum
// product in array of Integers
import java.util.*;
class GFG {
// Function to find maximum product pair
// in arr[0..n-1]
static void maxProduct(int arr[], int n)
{
if (n < 2)
{
System.out.println("No pairs exists");
return;
}
// Initialize max product pair
int a = arr[0], b = arr[1];
// Traverse through every possible pair
// and keep track of max product
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (arr[i] * arr[j] > a * b){
a = arr[i];
b = arr[j];
}
System.out.println("Max product pair is
{" + a + ", " + b + "}"); }
/* Driver program to test above function */
public static void main(String[] args)
{
int arr[] = {1, 4, 3, 6, 7, 0};
int n = arr.length;
maxProduct(arr, n);
}
}
// This code is contributed by Arnav Kr. Mandal.
|
Output :
Max product pair is {6, 7}
21.JAVA program to check whether two strings are anagrams of each other
// JAVA program to check whether two strings
// are anagrams of each other
import java.io.*;
import java.util.Arrays;
import java.util.Collections;
class GFG {
/* function to check whether two strings are
anagram of each other */
static boolean areAnagram(char[] str1, char[] str2)
{
// Get lenghts of both strings
int n1 = str1.length;
int n2 = str2.length;
// If length of both strings is not same,
// then they cannot be anagram
if (n1 != n2)
return false;
// Sort both strings
Arrays.sort(str1);
Arrays.sort(str2);
// Compare sorted strings
for (int i = 0; i < n1; i++)
if (str1[i] != str2[i])
return false;
return true;
}
/* Driver program to test to print printDups*/
public static void main(String args[])
{
char str1[] = { 't', 'e', 's', 't' };
char str2[] = { 't', 't', 'e', 'w' };
if (areAnagram(str1, str2))
System.out.println("The two strings are"
+ " anagram of each other");
else
System.out.println("The two strings are not"
+ " anagram of each other");
}
}
// Java program to delete a node from doubly linked list
class LinkedList {
static Node head, head1, head2;
static class Node {
int data;
Node next, prev;
Node(int d) {
data = d;
next = prev = null;
}
}
/* Function to split a list (starting with head) into two lists.
head1_ref and head2_ref are references to head nodes of
the two resultant linked lists */
void splitList() {
Node slow_ptr = head;
Node fast_ptr = head;
if (head == null) {
return;
}
/* If there are odd nodes in the circular list then
fast_ptr->next becomes head and for even nodes
fast_ptr->next->next becomes head */
while (fast_ptr.next != head
&& fast_ptr.next.next != head) {
fast_ptr = fast_ptr.next.next;
slow_ptr = slow_ptr.next;
}
/* If there are even elements in list then move fast_ptr */
if (fast_ptr.next.next == head) {
fast_ptr = fast_ptr.next;
}
/* Set the head pointer of first half */
head1 = head;
/* Set the head pointer of second half */
if (head.next != head) {
head2 = slow_ptr.next;
}
/* Make second half circular */
fast_ptr.next = slow_ptr.next;
/* Make first half circular */
slow_ptr.next = head;
}
/* Function to print nodes in a given singly linked list */
void printList(Node node) {
Node temp = node;
if (node != null) {
do {
System.out.print(temp.data + " ");
temp = temp.next;
} while (temp != node);
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
//Created linked list will be 12->56->2->11
list.head = new Node(12);
list.head.next = new Node(56);
list.head.next.next = new Node(2);
list.head.next.next.next = new Node(11);
list.head.next.next.next.next = list.head;
System.out.println("Original Circular Linked list ");
list.printList(head);
// Split the list
list.splitList();
System.out.println("");
System.out.println("First Circular List ");
list.printList(head1);
System.out.println("");
System.out.println("Second Circular List ");
list.printList(head2);
}
}
// This code has been contributed by Mayank Jaiswal
Output:
Original Circular Linked List
11 2 56 12
First Circular Linked List
11 2
Second Circular Linked List
56 12