Advertisement

Responsive Ads Here

Saturday, October 25, 2025

Top 10 Java programming Interview questions Part 1

Reverse a string in Java?

public class StringPrograms {

 public static void main(String[] args) {

  String str = "123";

  System.out.println(reverse(str));

 }

 public static String reverse(String in) {

  if (in == null)

   throw new IllegalArgumentException("Null is not valid input");

  StringBuilder out = new StringBuilder();

  char[] chars = in.toCharArray();

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

   out.append(chars[i]);

  return out.toString();

 }

}


Swap two numbers without using a third variable in Java.

Swapping numbers without using a third variable is a three-step process that’s better visualized in code:

b = b + a; // now b is sum of both the numbers
a = b - a; // b - a = (b + a) - a = b (a is swapped)
b = b - a; // (b + a) - b = a (b is swapped)

The following example code shows one way to implement the number swap method:

public class SwapNumbers {
 
public static void main(String[] args) {
 int a = 10;
 int b = 20;
 
    System.out.println("a is " + a + " and b is " + b);
 
 a = a + b;
 b = a - b;
 a = a - b;
 
    System.out.println("After swapping, a is " + a + " and b is " + b);
    }
 
}

The output shows that the integer values are swapped:

Output
a is 10 and b is 20
After swapping, a is 20 and b is 10


Java program to check if a vowel is present in a string

The following example code shows how to use a regular expression to check whether the string contains vowels:

public class StringContainsVowels {
 
 public static void main(String[] args) {
  System.out.println(stringContainsVowels("Hello")); // true
  System.out.println(stringContainsVowels("TV")); // false
 }
 
 public static boolean stringContainsVowels(String input) {
  return input.toLowerCase().matches(".*[aeiou].*");
 }
 
}



Java program to check if the given number is a prime number

You can write a program to divide the given number n, by a number from 2 to n/2 and check the remainder. If the remainder is 0, then it’s not a prime number. The following example code shows one way to check if a given number is a Prime number:

public class PrimeNumberCheck {
 
 public static void main(String[] args) {
  System.out.println(isPrime(19)); // true
  System.out.println(isPrime(49)); // false
 }
 
 public static boolean isPrime(int n) {
  if (n == 0 || n == 1) {
   return false;
  }
  if (n == 2) {
   return true;
  }
  for (int i = 2; i <= n / 2; i++) {
   if (n % i == 0) {
    return false;
   }
  }
 
  return true;
 }
 
}

Although this program works, it’s not very memory and time-efficient. Consider that, for a given number N, if there is a prime number M between 2 to √N (square root of N) that evenly divides it, then N is not a prime number.



Fibonacci series in Java


import java.util.Scanner;


public class FibonacciSeries {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);


        System.out.print("Enter the number of terms for the Fibonacci series: ");

        int n = input.nextInt();


        // Initialize the first two terms of the Fibonacci series

        int firstTerm = 0;

        int secondTerm = 1;


        System.out.println("Fibonacci Series up to " + n + " terms:");


        // Handle the cases for 0, 1, or more terms

        if (n <= 0) {

            System.out.println("Please enter a positive number of terms.");

        } else if (n == 1) {

            System.out.print(firstTerm);

        } else {

            System.out.print(firstTerm + ", " + secondTerm); // Print the first two terms


            // Generate and print the remaining terms

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

                int nextTerm = firstTerm + secondTerm;

                System.out.print(", " + nextTerm);

                firstTerm = secondTerm;

                secondTerm = nextTerm;

            }

        }

        System.out.println(); // New line for better formatting

        input.close();

    }

}


Remove space from a String in Java


String removeWhiteSpaces(String input) 

{

 StringBuilder output = new StringBuilder();

 char[] charArray = input.toCharArray();

 for (char c : charArray) 

{

  if (!Character.isWhitespace(c))

   output.append(c);

 }

 return output.toString();

}


Program to remove the duplicate characters from the given String=banaans


import java.util.Scanner;

public class RemoveDuplicate {

public static String removeDuplicateChar(String str) {

String result="";

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

int count=0;

if(str.charAt(i)!=' ') {

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

if(str.charAt(i)==str.charAt(j) &&(i!=j)) {

count++;

}

}

if(count==0) {

result += str.charAt(i);

}

}

}

return result;

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter String: ");

String str=sc.nextLine();

System.out.println(removeDuplicateChar(str));

sc.close();

}

}



OUTPUT

bans



program to print all duplicate character and their count form the given String=Banana

Code

import java.util.Scanner;

public class PrintDupAndCount {

public static String printDuplicateCharAndCount(String str) {

String result="";

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

int count=1;

if(str.charAt(i)!=' ') {

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

if(str.charAt(i)==str.charAt(j) &&(i!=j)) {

count++;

}

}

if(count>1) {

result += str.charAt(i)+" : "+count+"\n";

}

}

}

return result;

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter String: ");

String str=sc.nextLine();

System.out.println(printDuplicateCharAndCount(str));

sc.close();

}

}



Output
:
 a: 3  n: 2


No comments:

Post a Comment