Explore essential Java programs commonly asked in interviews, offering valuable coding insights and practice.
Frequently Asked Java Program
public class DuplicateWordFinder {
public static void main(String[] args) {
String input = "Tata,Swift,Audi,Mercedes,Tata,Renault";
String[] word = input.split(",");
Set<String> uniqWord = new HashSet<>();
for (String words : word) {
uniqWord.add(words.trim());
}
for (String words : uniqWord) {
System.out.println("Uniq Words are : " + words);
}
}
}
2. Even Letter Print From String
import java.util.Scanner;
public class EvenLetterPrintFromString {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Words: ");
String input = sc.nextLine();
for (int i = 1; i <= input.length() - 1; i += 2) {
System.out.println(input.charAt(i));
}
}
}
3. Remove special characters from string in java
import java.util.Scanner;
public class ExtractSpecialCharacterFromString {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Word with Special Characters: ");
String input = sc.nextLine();
String rm = input.replaceAll("[^a-zA-Z]", "");
System.out.println("After replacing the Spacial Character is: " + rm);
}
}
4. Factorial number in java
public class FactorialNumbers {
public static void main(String[] args) {
int num = 9;
long factorial = 1;
for (int i = 1; i <= num; i++) {
factorial = factorial * i;
}
System.out.println("The Factorial of " + num + " is " + factorial);
}
}
5. Fibonacci series in java
public class FibonnacciSeries {
public static void main(String[] args) {
int n1 = 0, n2 = 1, sum;
int num = 18;
System.out.println(n1 + " " + n2);
for (int i = 2; i <= num; i++) {
sum = n1 + n2;
n1 = n2;
n2 = sum;
System.out.println(sum);
}
}
}
6. Find the duplicate number in an array
public class FindTheDuplicateNumber {
public static void main(String[] args) {
int ar[] = { 133, 2, 134, 53554, 747, 747, 43634, 747, 86, 346 };
String input = "India Delhi".toLowerCase();
Set<Integer> duplicate = new HashSet<>();
Set<Integer> uniq = new HashSet<>();
for (int num : ar) {
if (!uniq.add(num)) {
duplicate.add(num);
}
}
System.out.println("duplicate number is : " + duplicate);
Set<Character> uniqs = new HashSet<>();
Set<Character> duplicates = new HashSet<>();
for (char c : input.toCharArray()) {
if (!uniqs.add(c)) {
duplicates.add(c);
}
}
System.out.println("duplicate charater is : " + duplicates);
}
}
7. Find the Greater & Smallest num in an array
public class GreaterNumberInArray {
public static void main(String[] args) {
int ar[] = { 1, 2, 4, 6, 8, 2, 33, 3345, 55 };
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int num : ar) {
if (max < num) {
max = num;
}
}
for (int element : ar) {
if (min > element) {
min = element;
}
}
System.out.println("greater number is : " + max);
System.out.println("smallest number is : " + min);
}
}
8. Find index of alphabet in string
public class IndexPositionOFAlphabet {
public static void main(String[] args) {
String sc = "masumrazabasantpur";
char index = 'm';
int indexPosition = sc.indexOf(index);
System.out.print("Index positions of '" + index + "' are: ");
for (int i = 0; i < sc.length(); i++) {
if (sc.charAt(i) == index) {
System.out.print(i + " ");
}
}
}
}
9. Find the leap year non leap year in an array
public class LeapYearNonLeapYearInArray {
public static void main(String[] args) {
int ar[] = { 2022, 2015, 2016, 2014, 2024, 2030, 2028 };
for (int year : ar) {
if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
System.out.println(year + " is leap year");
} else {
System.out.println(year + " is non leap year");
}
}
}
}
10. Find non repeating character in a string in java
public class NonRepeatingCharacter {
public static void main(String arg[]) {
String input = "masum raza delhi bihar";
for (char c : input.toCharArray()) {
if (input.indexOf(c) == (input.lastIndexOf(c))) {
System.out.println("non repeating character is : " + c);
}
}
// repeating character
String sc = "bittumasum";
for (char d : sc.toCharArray()) {
if (sc.indexOf(d) != (sc.lastIndexOf(d))) {
System.out.println("repeating character is: " + d);
}
}
}
}
11. Find the palindrome number in java
public class PalindromNumber {
public static void main(String[] args) {
int num = 12321;
int rev = 0;
int original = num;
while (num != 0) {
rev = rev * 10 + num % 10;
num = num / 10;
}
if (original == rev) {
System.out.println(original + " number is palidrome");
} else {
System.out.println(rev + " num is not palidrome");
}
}
}
12. Find the reverse number in java
public class ReverseNumber {
public static void main(String[] args) {
int num = 1213;
int rev = 0;
while (num != 0) {
rev = rev * 10 + num % 10;
num = num / 10;
}
System.out.println("reverse num is : " + rev);
}
}
13. Find the reverse string in java
public class ReversString {
public static void main(String[] args) {
String input = "delhi";
String rev = "";
int len = input.length();
for (int i = len - 1; i >= 0; i--) {
rev = rev + input.charAt(i);
}
System.out.println("reverse string is : " + rev);
}
}
14. Find the String palindrome in java
public class StringPalindromeChecker {
public static void main(String[] args) {
String input = "jahaj";
String orignal = input;
String rev = "";
int len = input.length();
for (int i = len - 1; i >= 0; i--) {
rev = rev + input.charAt(i);
}
if (orignal.equalsIgnoreCase(rev)) {
System.out.println("String is palindrome: " + orignal);
} else {
System.out.println("String is not palidrome: " + rev);
}
}
}
15. Find the uppercase count in string
public class UpperCaseCountInString {
public static void main(String[] args) {
String input = "MaDhUBani";
int count = 0;
for (int i = 0; i <= input.length() - 1; i++) {
char c = input.charAt(i);
if (Character.isUpperCase(c)) {
count++;
System.out.println("uper character is : " + c);
}
}
System.out.println("Total Upper case is : " + count);
}
}
16. Find the vowel letter count in string
public class VowelLetterCount {
public static void main(String[] args) {
String input = "rajuiUmbrela";
String vowel = "AEIOUaeiou";
int count = 0;
for (char c : input.toCharArray()) {
if (vowel.indexOf(c) != -1) {
count++;
System.out.println(c);
}
}
System.out.println(count);
}
}
17. Find the whitespace count in java
public class WhiteSpaceCount {
public static void main(String[] args) {
String input = "India is a big country";
int count = 0;
for (int i = 0; i <= input.length() - 1; i++) {
if (input.charAt(i) == ' ') {
count++;
}
}
System.out.println(count);
}
}
18. Find the word count in String
public class WordCountInString {
public static void main(String[] args) {
String input = "i like to travel";
int count = 1;
for (int i = 1; i <= input.length() - 1; i++) {
if ((input.charAt(i) == ' ') && (input.charAt(i + 1) != ' ')) {
count++;
}
}
System.out.println(count);
}
}
19. Find the Count characters using HashMap in string
import java.util.HashMap;
public class CharacterCount {
public static void main(String[] args) {
String str = "masum";
HashMap<Character, Integer> map = new HashMap<>();
for (char c : str.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
System.out.println(map);
}
}
20. Find the Second largest Num from array:
public class SecondHigherNumInArray {
public static void main(String[] args) {
int arr[] = { 112, 34, 553, 35, 23, 678, 876, 366, 53, 757, 3425, 4444 };
int first = Integer.MIN_VALUE;
int second = Integer.MIN_VALUE;
for(int num:arr) {
if(num>first) {
second=first;
first=num;
}else if (num>second && num!=first) {
num=second;
}
}
System.out.println(second);
}
}
21. Find the Prime Num from array:
public class PrimeChecker {
public static void main(String[] args) {
int[] arr = { 25, 568, 584, 26, 45, 85 };
for (int num : arr) {
System.out.println(num + " is " + (isPrime(num) ? "Prime" : "Not Prime"));
}
}
private static boolean isPrime(int num) {
if (num <= 1)
return false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0)
return false;
}
return true;
}
}
22.Find Occurrences of Each String in a Sentence Using HashMap
import java.io.*;
import java.util.*;
class GFG {
public static void main (String[] args) {
String sentence = "Interview with aditya CGO Interview";
String[] words = sentence.toLowerCase().split(" ");
HashMap<String, Integer> wordCount = new HashMap<>();
for (String word : words) {
wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
}
for (String word : wordCount.keySet()) {
System.out.println(word + ": " + wordCount.get(word));
}
}
}
O/P with: 1 cgo: 1 interview: 2 aditya: 1
Comments
Post a Comment