Greens Technologys Whatsapp
Greens Technologys call
Courses

Interview Questions

Java Programs Interview Questions and Answers


1.Program to check given number is even or odd:

public class CourseDetails {

               public static void main(String[] args) {

                              int a = 10;

                              if (a % 2 == 0) {

                                             System.out.println("Given number is even");

                              } else {

                                             System.out.println("Given number is odd");

                              }

               }

}

2.Print all the Odd numbers betwen 1 to 100:

public class CourseDetails {

               public static void main(String[] args) {

                                                            

                              for (int i = 1; i <= 100; i++) {

              

                                             if (i % 2 == 1) {

                                                            

                                                            System.out.println("odd" + i);

                                             }

                              }

               }

}

3.Count of odd numbers   between 1 to 100

public class CourseDetails {

               public static void main(String[] args) {

                              int count = 0;

                              

                              for (int i = 1; i <= 100; i++) {

                                             if (i % 2 == 1) {

                                                            count = count + 1;

                                                            

                                             }

                              }

                              System.out.println("Number of odd numbers present inbetween 1 to 100:"+count);

               }

}

4.Print all the even numbers betwen 1 to 100:

public class CourseDetails {

               public static void main(String[] args) {

                                                            

                              for (int i = 1; i <= 100; i++) {

              

                                             if (i % 2 == 0) {

                                                            

                                                            System.out.println("even" + i);

                                             }

                              }

               }

}

5.Count of even numbers   between 1 to 100

public class CourseDetails {

               public static void main(String[] args) {

                              int count = 0;

                              

                              for (int i = 1; i <= 100; i++) {

                                             if (i % 2 == 0) {

                                                            count = count + 1;

                                                            

                                             }

                              }

                              System.out.println("Number of even numbers present inbetween 1 to 100:"+count);

               }

}

6.Reverse of the given number:

public class CourseDetails {

               public static void main(String[] args) {

                              int num = 12345;

                              int res = 0;

                              int rem = 0;

                              while (num > 0) {

                                             rem = num % 10;

                                             res = (res * 10) + rem;

                                             num = num / 10;

                              }

                              System.out.println("Reverse of a number: " + res);

               }

}

7.To check the given number is palindrome or not:

public class CourseDetails {

               public static void main(String[] args) {

                              int num = 121;

                              int n=num;

                              int res = 0;

                              int rem = 0;

                              while (num > 0) {

                                             rem = num % 10;

                                             res = (res * 10) + rem;

                                             num = num / 10;

                              }

                              if (n==res) {

                                             System.out.println("Given number is palindrome");

                                             

                              } else {

                                             System.out.println("Given number is not a palindrome");

                              }

                              

                              

               }

}

%10------to get the last digit from a number

*10------to increase a digit in a number

/10---------to remove the last digit from a number

8.To check the given number is Armstrong number:

public class CourseDetails {

               public static void main(String[] args) {

                              int num = 153;

                              int n = num;

                              int res = 0;

                              int rem = 0;

                              while (num > 0) {

                                             rem = num % 10;

                                             res = res + (rem * rem * rem);

                                             num = num / 10;

                              }

                              if (n == res) {

                                             System.out.println("Armstrong number");

                              } else {

                                             System.out.println("Not an Armstrong number");

                              }

               }

}

9.To find the sum of the digits in a given number:

public class CourseDetails {

               public static void main(String[] args) {

                              int num = 153;

                              int res = 0;

                              int rem = 0;

                              while (num > 0) {

                                             rem = num % 10;

                                             res = res + rem;

                                             num = num / 10;

                              }

                              System.out.println("Sum of digits: "+res);

               }

}

10.To find the product of the digits in a given number:

public class CourseDetails {

               public static void main(String[] args) {

                              int num = 153;

                              int res = 1;

                              int rem = 0;

                              while (num > 0) {

                                             rem = num % 10;

                                             res = res * rem;

                                             num = num / 10;

                              }

                              System.out.println("Product of digits: "+res);

               }

}

11.To find the count of the digits in a given number:

public class CourseDetails {

               public static void main(String[] args) {

                              int num = 15345;

                              int count = 0;

                              while (num > 0) {

                                             count++;

                                             num = num / 10;

                                             

                              }

                              System.out.println("Count of digits: " + count);

               }

}

12.Factorial of the given number:

public class CourseDetails {

               public static void main(String[] args) {

                              int n = 10;

                              int fact = 1;

                              

                              for (int i = 1; i <= n; i++) {

                                             fact = fact * i;

                              }

                              System.out.println("Factorial: " + fact);

               }

}

13.Fibbonacci series for the given number:

public class CourseDetails {

               public static void main(String[] args) {

                              int a = 0;

                              int b = 1;

                              System.out.println(a);

                              System.out.println(b);

                              for (int i = 1; i <= 10; i++) {

                                             int c = a + b;

                                             System.out.println(c);

                                             a = b;

                                             b = c;

                              }

               }

}

14.Find the reverse of the given string:

public class A {

               public static void main(String[] args) throws IOException {

                              String s = "Welcome to java class";

                              String res = "";

                              for (int i = s.length() - 1; i >= 0; i--) {

                                             char c = s.charAt(i);

                                             res = res + c;

                              }

                              System.out.println("Reverse of string: " + res);

               }

}

14.Check whether the given string is palindrome or not

public class A {

               public static void main(String[] args) throws IOException {

                              String s = "Welcome to java class";

                              String res = "";

                              for (int i = s.length() - 1; i >= 0; i--) {

                                             char c = s.charAt(i);

                                             res = res + c;

                              }

                              System.out.println("Reverse of string: " + res);

                              if (s.equals(res)) {

                                             System.out.println("String is paindrome");

                              } else {

                                             System.out.println("Not palindrome");

                              }

               }

}

15.To find the reverse of each word:

public class A {

               public static void main(String[] args) throws IOException {

                              String str = "Welcome to java class";

                              String res = "";

                              String[] sp = str.split(" ");

                              for (String s : sp) {

                                             System.out.println();

                                             String rev = "";

                                             for (int i = s.length() - 1; i >= 0; i--) {

                                                            char c = s.charAt(i);

                                                            rev = rev + c;

                                             }

                                             res = res + rev + " ";

                              }

                              System.out.println(res);

               }

}

16.To Change each word's first letter to upper case:

public class A {

               public static void main(String[] args) throws IOException {

                              String str = "welcome to java class";

                              String[] sp = str.split(" ");

                              String res = "";

                              for (String s : sp) {

                                             char c = s.charAt(0);

                                             String sub = s.substring(1);

                                             res = res + Character.toUpperCase(c) + sub + " ";

                              }

                              System.out.println(res);

               }

}

17.To remove character duplicates from String:

public class A {

               public static void main(String[] args) throws IOException {

                              String str = "raining here...";

                              String res = "";

                              LinkedHashSet<Character> l = new LinkedHashSet<>();

                              for (int i = 0; i < str.length(); i++) {

                                             char c = str.charAt(i);

                                             l.add(c);

                              }

                              for (Character c : l) {

                                             res = res + c;

                              }

                              

                              System.out.println(res);

               }

}

18.To remove word duplicates from a String:

public class A {

               public static void main(String[] args) throws IOException {

                              String str = "java sql java python java python";

                              String res = "";

                              LinkedHashSet<String> l = new LinkedHashSet<>();

                              String[] sp = str.split(" ");

                              for (String s : sp) {

                                             l.add(s);

                              }

                              for (String x : l) {

                                             res = res + x+" ";

                              }

                              

                              System.out.println(res);

               }

}

19.To remove the duplicates and to sort elements from Array:

public class A {

               public static void main(String[] args) throws IOException {

                              int[] a = { 55, 39, 26, 78, 55, 99, 30 };

                              TreeSet<Integer> t = new TreeSet<>();

                              for (int i = 0; i < a.length; i++) {

                                             t.add(a[i]);

                              }

                              System.out.println(t);

               }

}

20.To sort numbers   in ascending order:

public class CourseDetails {

               public static void main(String[] args) {

                              int[] a = { 55, 20, 60, 90, 15, 35 };

                              

                              System.out.println("Before sorting");

                              for (int i : a) {

                                             System.out.println(i);

                              }

                              

                              for (int i = 0; i < a.length; i++) {

                                             for (int j = i + 1; j < a.length; j++) {

                                                            // Ascending order

                                                            if (a[i] > a[j]) {

                                                                           int temp = a[i];

                                                                           a[i] = a[j];

                                                                           a[j] = temp;

                                                            }

                                             }

                              }

                              System.out.println("After sorting in ascending order");

                              for (int i : a) {

                                             System.out.println(i);

                              }

               }

}

21.To sort elements in descending order:

public class CourseDetails {

               public static void main(String[] args) {

                              int[] a = { 55, 20, 60, 90, 15, 35 };

                              

                              System.out.println("Before sorting");

                              for (int i : a) {

                                             System.out.println(i);

                              }

                              

                              for (int i = 0; i < a.length; i++) {

                                             for (int j = i + 1; j < a.length; j++) {

                                                            // Descending order

                                                            if (a[i] < a[j]) {

                                                                           int temp = a[i];

                                                                           a[i] = a[j];

                                                                           a[j] = temp;

                                                            }

                                             }

                              }

                              System.out.println("After sorting in descending order");

                              for (int i : a) {

                                             System.out.println(i);

                              }

               }

}

22.To find count of each character in string(Count of duplicates of each character):

public class CourseDetails {

               public static void main(String[] args) {

                              String s = "raining here...";

                              char[] c = s.toCharArray();

                              LinkedHashMap<Character, Integer> mp = new LinkedHashMap<>();

                              for (char d : c) {

                                             if (mp.containsKey(d)) {

                                                            Integer i = mp.get(d);

                                                            mp.put(d, i + 1);

                                             }

                                             

                                             else {

                                                            mp.put(d, 1);

                                             }

                              }

                              

                              System.out.println(mp);

               }

}

22.To add each odd and even pair in an array:

public class A {

               public static void main(String[] args) throws IOException {

                              

                              int[] a= {10,20,30,40,50};

                              

                              try {

                                             for (int i = 0; i < a.length; i=i+2) {

                                                            System.out.println(a[i]+a[i+1]);

                                             }

                              }

                              catch (Exception e) {

                                             System.out.println(a[a.length-1]);

                              }                             

               }

}

23.To find count of each word in string(Count of duplicates of each word):

public class A {

               public static void main(String[] args) throws IOException {

                              String s = "java sql java python ";

                              String[] sp = s.split(" ");

                              LinkedHashMap<String, Integer> mp = new LinkedHashMap<>();

                              for (String st : sp) {

                                             if (mp.containsKey(st)) {

                                                            Integer i = mp.get(st);

                                                            mp.put(st, i + 1);

                                             } else {

                                                            mp.put(st, 1);

                                             }

                              }

                              System.out.println(mp);

               }

}

22.To find count of each character in string(Count of duplicates of each character) using normal for loop:

public class A {

               public static void main(String[] args) throws IOException {

                              String s = "raining here...";

                              LinkedHashMap<Character, Integer> mp = new LinkedHashMap<>();

                              for (int i = 0; i < s.length(); i++) {

                                             char c = s.charAt(i);

                                             if (mp.containsKey(c)) {

                                                            Integer in = mp.get(c);

                                                            mp.put(c, in + 1);

                                             } else {

                                                            mp.put(c, 1);

                                             }

                              }

                              System.out.println(mp);

               }

}

23.To find the vowels and consonants in the given String:

public class A {

               public static void main(String[] args) throws IOException {

                              String str = "All the best";

                              String s = str.replace(" ", "");

                              String vow = "";

                              int v = 0;

                              String con = "";

                              int co = 0;

                              for (int i = 0; i < s.length(); i++) {

                                             char c = s.charAt(i);

                                             if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O'

                                                                           || c == 'U') {

                                                            vow = vow + c;

                                                            v++;

                                             } else {

                                                            con = con + c;

                                                            co++;

                                             }

                              }

                              System.out.println("Vowels: " + vow);

                              System.out.println("vowels count " + v);

                              System.out.println("Consonants: " + con);

                              System.out.println("Consonants count " + co);

               }

}

ASCII Values:

---------------------

65-90-------------upper case alphabets

97-122------------lower case alphabets

48-57-------------digits

special charcter

24.To find the uppercase,lowercase,digits and special characters in the given String:

public class A {

               public static void main(String[] args) throws IOException {

                              String str = "RenuDev1235@gmail.com";

                              String s = str.replace(" ", "");

                              String cap = "";

                              int ca = 0;

                              String small = "";

                              int sm = 0;

                              String digit = "";

                              int di = 0;

                              String spec = "";

                              int sp = 0;

                              for (int i = 0; i < s.length(); i++) {

                                             char c = s.charAt(i);

                                             int x = c;

                                             if (x >= 65 && x <= 90) {

                                                            // capital letter

                                                            cap = cap + c;

                                                            ca++;

                                             } else if (x >= 97 && x <= 122) {

                                                            // Small letter

                                                            small = small + c;

                                                            sm++;

                                             } else if (x >= 48 && x <= 57) {

                                                            // digit

                                                            digit = digit + c;

                                                            di++;

                                             } else {

                                                            // Special character

                                                            spec = spec + c;

                                                            sp++;

                                             }

                              }

                              System.out.println("Small letters: " + small);

                              System.out.println("Small letters count " + sm);

                              System.out.println("Capital letters: " + cap);

                              System.out.println("Capital letters count:  " + ca);

                              System.out.println("Digits: " + digit);

                              System.out.println("Digits count: " + di);

                              System.out.println("Special characters : " + spec);

                              System.out.println("Special characters count:  " + sp);

               }

}

25.To find the uppercase,lowercase,digits and special characters in the given String:

public class A {

               public static void main(String[] args) throws IOException {

                              String str = "RenuDev1235@gmail.com";

                              String s = str.replace(" ", "");

                              String cap = "";

                              int ca = 0;

                              String small = "";

                              int sm = 0;

                              String digit = "";

                              int di = 0;

                              String spec = "";

                              int sp = 0;

                              for (int i = 0; i < s.length(); i++) {

                                             char x = s.charAt(i);

                              

                                             if (x >='A' && x <= 'Z') {

                                                            // capital letter

                                                            cap = cap + x;

                                                            ca++;

                                             } else if (x >= 'a' && x <= 'z') {

                                                            // Small letter

                                                            small = small + x;

                                                            sm++;

                                             } else if (x >='0' && x <= '9') {

                                                            // digit

                                                            digit = digit + x;

                                                            di++;

                                                            

                                             } else {

                                                            // Special character

                                                            spec = spec + x;

                                                            sp++;

                                             }

                              }

                              System.out.println("Small letters: " + small);

                              System.out.println("Small letters count " + sm);

                              System.out.println("Capital letters: " + cap);

                              System.out.println("Capital letters count:  " + ca);

                              System.out.println("Digits: " + digit);

                              System.out.println("Digits count: " + di);

                              System.out.println("Special characters : " + spec);

                              System.out.println("Special characters count:  " + sp);

               }

}

26.To find the uppercase,lowercase,digits and special characters in the given String:

public class A {

               public static void main(String[] args) throws IOException {

                              String str = "RenuDev1235@gmail.com";

                              String s = str.replace(" ", "");

                              String cap = "";

                              int ca = 0;

                              String small = "";

                              int sm = 0;

                              String digit = "";

                              int di = 0;

                              String spec = "";

                              int sp = 0;

                              for (int i = 0; i < s.length(); i++) {

                                             char x = s.charAt(i);

                                             if (Character.isUpperCase(x)) {

                                                            // capital letter

                                                            cap = cap + x;

                                                            ca++;

                                             } else if (Character.isLowerCase(x)) {

                                                            // Small letter

                                                            small = small + x;

                                                            sm++;

                                             } else if (Character.isDigit(x)) {

                                                            // digit

                                                            digit = digit + x;

                                                            di++;

                                             } else {

                                                            // Special character

                                                            spec = spec + x;

                                                            sp++;

                                             }

                              }

                              System.out.println("Small letters: " + small);

                              System.out.println("Small letters count " + sm);

                              System.out.println("Capital letters: " + cap);

                              System.out.println("Capital letters count:  " + ca);

                              System.out.println("Digits: " + digit);

                              System.out.println("Digits count: " + di);

                              System.out.println("Special characters : " + spec);

                              System.out.println("Special characters count:  " + sp);

               }

}

27.To sort the characters in the given String:

public class A {

               public static void main(String[] args) {

                              String s = "goodday";

                              char[] a = s.toCharArray();

                              System.out.println("Before sorting");

                              for (int i = 0; i < a.length; i++) {

                                             System.out.print(a[i]);

                              }

                              for (int i = 0; i < a.length; i++) {

                                             for (int j = i + 1; j < a.length; j++) {

                                                            if (a[i] > a[j]) {

                                                                           char temp = a[i];

                                                                           a[i] = a[j];

                                                                           a[j] = temp;

                                                            }

                                             }

                              }

                              System.out.println();

                              System.out.println("After sorting in descending order");

                              for (int i = 0; i < a.length; i++) {

                                             System.out.print(a[i]);

                              }

               }

}

28.To check the given two Strings are Anagram or not:

public class A {

               public static void main(String[] args) {

                              String s1 = "adbc";

                              String s2 = "dbca";

                              if (s1.length() == s2.length()) {

                                             // To convert String into Character array

                                             char[] c1 = s1.toCharArray();

                                             char[] c2 = s2.toCharArray();

                                             // Arrays--utility class,u can use those predefined mwthods in Array variable

                                             Arrays.sort(c1);

                                             Arrays.sort(c2);

                                             // ValueOf meThod here it is converting char array into String

                                             String a = String.valueOf(c1);

                                             String b = String.valueOf(c2);

                                             if (a.equals(b)) {

                                                            System.out.println("Anagram");

                                             } else {

                                                            System.out.println("Not anagram-Exactly characters are not matching");

                                             }

                              }

                              else {

                                             System.out.println("Not Anagram-Length not equal");

                              }

               }

}

29.To check the given two Strings are Anagram or not:

public class A {

               public static void main(String[] args) {

                              String s1 = "adbc";

                              String s2 = "dbca";

                              if (s1.length() == s2.length()) {

                                             // To convert String into Character array

                                             char[] c1 = s1.toCharArray();

                                             char[] c2 = s2.toCharArray();

                                             // Arrays--utility class,u can use those predefined methods in Array variable

                                             Arrays.sort(c1);

                                             Arrays.sort(c2);

                                             

                                             boolean x = Arrays.equals(c1, c2);

               

                                             if (x==true) {

                                                            System.out.println("Anagram");

                                             } else {

                                                            System.out.println("Not anagram-Exactly characters are not matching");

                                             }

                              }

                              else {

                                             System.out.println("Not Anagram-Length not equal");

                              }

               }

}

30.To check the given number is prime or not:

public class A {

               public static void main(String[] args) {

                              int flag = 0;

                              int n = 1;

                              if (n == 0 || n == 1) {

                                             System.out.println("0 and 1 not considered as prime number");

                              } else {

                                             for (int i = 2; i <= n / 2; i++) {

                                                            if (n % i == 0) {

                                                                           flag = 1;

                                                            }

                                             }

                                             if (flag == 0) {

                                                            System.out.println("Given number is prime");

                                             } else {

                                                            System.out.println("Not prime");

                                             }

                              }

               }

}

31.Generate the given pattern:

*

* *

* * *

public class A {

               public static void main(String[] args) {

                              for (int i = 1; i <= 3; i++) {

                                             for (int j = 0; j < i; j++) {

                                                            System.out.print("* ");

                                             }

                                             System.out.println();

                              }

               }

}

32.Generate the given pattern:

1

2 2

3 3 3

public class A {

               public static void main(String[] args) {

                              for (int i = 1; i <= 3; i++) {

                                             for (int j = 0; j < i; j++) {

                                                            System.out.print(i+" ");

                                             }

                                             System.out.println();

                              }

               }

}

33.Generate the given pattern:

1

1 2

1 2 3

public class A {

               public static void main(String[] args) {

                              for (int i = 1; i <= 3; i++) {

                                             for (int j = 0; j < i; j++) {

                                                            System.out.print(j+" ");

                                             }

                                             System.out.println();

                              }

               }

}

34.Generate the given pattern:

    *

  *  *

*  *  *

public class A {

               public static void main(String[] args) {

                              for (int i = 1; i <= 3; i++) {

                                             for (int k = 3 - i; k > 0; k--) {

                                                            System.out.print(" ");

                                             }

                                             for (int j = 0; j < i; j++) {

                                                            System.out.print("* ");

                                             }

                                             System.out.println();

                              }

               }

}

35.Generate the given pattern:

    *

  **

***

public class A {

               public static void main(String[] args) {

                              for (int i = 1; i <= 3; i++) {

                                             for (int k = 3 - i; k > 0; k--) {

                                                            System.out.print(" ");

                                             }

                                             for (int j = 0; j < i; j++) {

                                                            System.out.print("*");

                                             }

                                             System.out.println();

                              }

               }

}