Top 10 Interview Based Java Programs For Freshers
1.Java program to check whether the Number is Even or Odd.
In this java program, we’re going to check string input by the user will be palindrome or not. Take a String input from the user and store it in a variable called “s”. A palindrome string is one that will be the same if we read from forward and from backward also. Strings like ‘madam’, ‘lol’, ‘pop’, ‘arora’ are examples of palindrome string . We’re storing a reversed string in ‘rev’ variable after that we will compare that original string and the reversed string are same or not using equals() method
5. Java program to find missing Number in array.
Answer: import java.util.Arrays;
class Main
{
// Find the missing number in a given array
public static int getMissingNumber(int[] arr)
{
// get the array's length
int n = arr.length;
// the actual size is `n+1` since a number is missing from the array
int m = n + 1;
// get a sum of integers between 1 and `n+1`
int total = m * (m + 1) / 2;
// get an actual sum of integers in the array
int sum = Arrays.stream(arr).sum();
// the missing number is the difference between the expected sum
// and the actual sum
return total - sum;
}
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 4, 5, 7, 8, 9, 10 };
System.out.println("The missing number is " + getMissingNumber(arr));
}
}
output: The missing Number is 6
6. Java program to check duplicate character in the String.
Answer: public class DuplStr {
public static void main(String argu[]) {
String str = "w3schools";
int cnt = 0;
char[] inp = str.toCharArray();
System.out.println("Duplicate Characters are:");
for (int i = 0; i < str.length(); i++) {
for (int j = i + 1; j < str.length(); j++) {
if (inp[i] == inp[j]) {
System.out.println(inp[j]);
cnt++;
break;
}
}
}
}
}
Output: Duplicate Characters are: s o
Here in this program, a Java class name DuplStr is declared which is having the main() method. All Java program needs one main() function from where it starts executing program. Inside the main(), the String type variable name str is declared and initialized with string w3schools. Next an integer type variable cnt is declared and initialized with value 0. This cnt will count the number of character-duplication found in the given string.
The statement: char [] inp = str.toCharArray(); is used to convert the given string to character array with the name inp using the predefined method toCharArray(). The System.out.println is used to display the message "Duplicate Characters are as given below:". Now the for loop is implemented which will iterate from zero till string length. Another nested for loop has to be implemented which will count from i+1 till length of string.
Inside this two nested structure for loops, you have to use an if condition which will check whether inp[i] is equal to inp[j] or not. If the condition becomes true prints inp[j] using System.out.println() with s single incrementation of variable cnt and then break statement will be encountered which will move the execution out of the loop.
7.Java program to find number of words in a string.
Answer: import java.util.Scanner;
class WordsInaString
{
public static void main(String arg[])
{
Scanner sc= new Scanner(System.in);
System.out.println("enter a string :");
String a = sc.nextLine();
//char ch[]=new char[s.length()];
int count=Length(a);
System.out.println("number of words in a given string is :"+count);
}
static int Length(String s)
{
int c=0;
for (int i = 0;i<=(s.length()-1);i++)
{
if( ( (i>0)&& (s.charAt(i)!=' ') &&(s.charAt(i-1)==' ')) || ((s.charAt(i)!=' ')&&(i==0)) )
c++;
}
return c;
}
Output:enter a string :
NEVER GIVE UP
number of words in a given string is :3
8.Java program to find Factorial of a Number.
In this program, we've used for loop to loop through all numbers between 1 and the given number num (10), and the product of each number till num is stored in a variable factorial.
We've used long instead of int to store large results of factorial. However, it's still not big enough to store the value of bigger numbers (say 100).
For results that cannot be stored in a long variable, we use BigInteger
variable declared in java.math
library.
0 Comments